mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-26 05:42:26 +00:00
Use pattern matching instead of type casting.
Original Pull Request #2784 Closes #2785
This commit is contained in:
parent
415d5e0385
commit
8613eb26e0
@ -50,6 +50,7 @@ import org.springframework.util.Assert;
|
||||
* filter.
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Junghoon Ban
|
||||
* @since 4.4
|
||||
*/
|
||||
class CriteriaFilterProcessor {
|
||||
@ -169,7 +170,7 @@ class CriteriaFilterProcessor {
|
||||
Assert.isTrue(values[1] instanceof String || values[1] instanceof Distance,
|
||||
"Second element of a geo distance filter must be a text or a Distance");
|
||||
|
||||
String dist = (values[1] instanceof Distance) ? extractDistanceString((Distance) values[1]) : (String) values[1];
|
||||
String dist = (values[1] instanceof Distance distance) ? extractDistanceString(distance) : (String) values[1];
|
||||
|
||||
return QueryBuilders.geoDistance() //
|
||||
.field(fieldName) //
|
||||
@ -178,8 +179,8 @@ class CriteriaFilterProcessor {
|
||||
.location(location -> {
|
||||
if (values[0]instanceof GeoPoint loc) {
|
||||
location.latlon(latlon -> latlon.lat(loc.getLat()).lon(loc.getLon()));
|
||||
} else if (values[0] instanceof Point) {
|
||||
GeoPoint loc = GeoPoint.fromPoint((Point) values[0]);
|
||||
} else if (values[0] instanceof Point point) {
|
||||
GeoPoint loc = GeoPoint.fromPoint(point);
|
||||
location.latlon(latlon -> latlon.lat(loc.getLat()).lon(loc.getLon()));
|
||||
} else {
|
||||
String loc = (String) values[0];
|
||||
@ -220,8 +221,8 @@ class CriteriaFilterProcessor {
|
||||
"single-element of boundedBy filter must be type of GeoBox or Box");
|
||||
|
||||
GeoBox geoBBox;
|
||||
if (value instanceof Box) {
|
||||
geoBBox = GeoBox.fromBox((Box) value);
|
||||
if (value instanceof Box box) {
|
||||
geoBBox = GeoBox.fromBox(box);
|
||||
} else {
|
||||
geoBBox = (GeoBox) value;
|
||||
}
|
||||
|
@ -40,6 +40,7 @@ import org.springframework.data.elasticsearch.VersionConflictException;
|
||||
* appropriate: any other exception may have resulted from user code, and should not be translated.
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Junghoon Ban
|
||||
* @since 4.4
|
||||
*/
|
||||
public class ElasticsearchExceptionTranslator implements PersistenceExceptionTranslator {
|
||||
@ -59,7 +60,7 @@ public class ElasticsearchExceptionTranslator implements PersistenceExceptionTra
|
||||
*/
|
||||
public RuntimeException translateException(Throwable throwable) {
|
||||
|
||||
RuntimeException runtimeException = throwable instanceof RuntimeException ? (RuntimeException) throwable
|
||||
RuntimeException runtimeException = throwable instanceof RuntimeException ex ? ex
|
||||
: new RuntimeException(throwable.getMessage(), throwable);
|
||||
RuntimeException potentiallyTranslatedException = translateExceptionIfPossible(runtimeException);
|
||||
|
||||
|
@ -79,6 +79,7 @@ import org.springframework.util.StringUtils;
|
||||
*
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Illia Ulianov
|
||||
* @author Junghoon Ban
|
||||
* @since 4.4
|
||||
*/
|
||||
public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearchTemplate {
|
||||
@ -645,7 +646,7 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch
|
||||
*/
|
||||
private RuntimeException translateException(Throwable throwable) {
|
||||
|
||||
RuntimeException runtimeException = throwable instanceof RuntimeException ? (RuntimeException) throwable
|
||||
RuntimeException runtimeException = throwable instanceof RuntimeException ex ? ex
|
||||
: new RuntimeException(throwable.getMessage(), throwable);
|
||||
RuntimeException potentiallyTranslatedException = exceptionTranslator
|
||||
.translateExceptionIfPossible(runtimeException);
|
||||
|
@ -80,6 +80,7 @@ import org.springframework.util.ObjectUtils;
|
||||
* @author Marc Vanbrabant
|
||||
* @author Anton Naydenov
|
||||
* @author vdisk
|
||||
* @author Junghoon Ban
|
||||
* @since 3.2
|
||||
*/
|
||||
public class MappingElasticsearchConverter
|
||||
@ -503,16 +504,16 @@ public class MappingElasticsearchConverter
|
||||
private Object propertyConverterRead(ElasticsearchPersistentProperty property, Object source) {
|
||||
PropertyValueConverter propertyValueConverter = Objects.requireNonNull(property.getPropertyValueConverter());
|
||||
|
||||
if (source instanceof String[]) {
|
||||
if (source instanceof String[] strings) {
|
||||
// convert to a List
|
||||
source = Arrays.asList((String[]) source);
|
||||
source = Arrays.asList(strings);
|
||||
}
|
||||
|
||||
if (source instanceof List) {
|
||||
source = ((List<?>) source).stream().map(it -> convertOnRead(propertyValueConverter, it))
|
||||
if (source instanceof List<?> list) {
|
||||
source = list.stream().map(it -> convertOnRead(propertyValueConverter, it))
|
||||
.collect(Collectors.toList());
|
||||
} else if (source instanceof Set) {
|
||||
source = ((Set<?>) source).stream().map(it -> convertOnRead(propertyValueConverter, it))
|
||||
} else if (source instanceof Set<?> set) {
|
||||
source = set.stream().map(it -> convertOnRead(propertyValueConverter, it))
|
||||
.collect(Collectors.toSet());
|
||||
} else {
|
||||
source = convertOnRead(propertyValueConverter, source);
|
||||
|
@ -17,7 +17,6 @@ package org.springframework.data.elasticsearch.repository.query.parser;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.elasticsearch.core.geo.GeoBox;
|
||||
@ -44,6 +43,7 @@ import org.springframework.lang.Nullable;
|
||||
* @author Franck Marchand
|
||||
* @author Artur Konczak
|
||||
* @author Peter-Josef Meisch
|
||||
* @author Junghoon Ban
|
||||
*/
|
||||
public class ElasticsearchQueryCreator extends AbstractQueryCreator<CriteriaQuery, CriteriaQuery> {
|
||||
|
||||
@ -62,8 +62,8 @@ public class ElasticsearchQueryCreator extends AbstractQueryCreator<CriteriaQuer
|
||||
|
||||
@Override
|
||||
protected CriteriaQuery create(Part part, Iterator<Object> iterator) {
|
||||
PersistentPropertyPath<ElasticsearchPersistentProperty> path = context
|
||||
.getPersistentPropertyPath(part.getProperty());
|
||||
PersistentPropertyPath<ElasticsearchPersistentProperty> path = context.getPersistentPropertyPath(
|
||||
part.getProperty());
|
||||
return new CriteriaQuery(from(part,
|
||||
new Criteria(path.toDotPath(ElasticsearchPersistentProperty.QueryPropertyToFieldNameConverter.INSTANCE)),
|
||||
iterator));
|
||||
@ -74,8 +74,8 @@ public class ElasticsearchQueryCreator extends AbstractQueryCreator<CriteriaQuer
|
||||
if (base == null) {
|
||||
return create(part, iterator);
|
||||
}
|
||||
PersistentPropertyPath<ElasticsearchPersistentProperty> path = context
|
||||
.getPersistentPropertyPath(part.getProperty());
|
||||
PersistentPropertyPath<ElasticsearchPersistentProperty> path = context.getPersistentPropertyPath(
|
||||
part.getProperty());
|
||||
return base.addCriteria(from(part,
|
||||
new Criteria(path.toDotPath(ElasticsearchPersistentProperty.QueryPropertyToFieldNameConverter.INSTANCE)),
|
||||
iterator));
|
||||
@ -109,8 +109,7 @@ public class ElasticsearchQueryCreator extends AbstractQueryCreator<CriteriaQuer
|
||||
return criteria.is(parameters.next()).not();
|
||||
case REGEX:
|
||||
return criteria.expression(parameters.next().toString());
|
||||
case LIKE:
|
||||
case STARTING_WITH:
|
||||
case LIKE, STARTING_WITH:
|
||||
return criteria.startsWith(parameters.next().toString());
|
||||
case ENDING_WITH:
|
||||
return criteria.endsWith(parameters.next().toString());
|
||||
@ -118,13 +117,11 @@ public class ElasticsearchQueryCreator extends AbstractQueryCreator<CriteriaQuer
|
||||
return criteria.contains(parameters.next().toString());
|
||||
case GREATER_THAN:
|
||||
return criteria.greaterThan(parameters.next());
|
||||
case AFTER:
|
||||
case GREATER_THAN_EQUAL:
|
||||
case AFTER, GREATER_THAN_EQUAL:
|
||||
return criteria.greaterThanEqual(parameters.next());
|
||||
case LESS_THAN:
|
||||
return criteria.lessThan(parameters.next());
|
||||
case BEFORE:
|
||||
case LESS_THAN_EQUAL:
|
||||
case BEFORE, LESS_THAN_EQUAL:
|
||||
return criteria.lessThanEqual(parameters.next());
|
||||
case BETWEEN:
|
||||
return criteria.between(parameters.next(), parameters.next());
|
||||
@ -132,8 +129,7 @@ public class ElasticsearchQueryCreator extends AbstractQueryCreator<CriteriaQuer
|
||||
return criteria.in(asArray(parameters.next()));
|
||||
case NOT_IN:
|
||||
return criteria.notIn(asArray(parameters.next()));
|
||||
case SIMPLE_PROPERTY:
|
||||
case WITHIN: {
|
||||
case SIMPLE_PROPERTY, WITHIN: {
|
||||
Object firstParameter = parameters.next();
|
||||
Object secondParameter = null;
|
||||
if (type == Part.Type.SIMPLE_PROPERTY) {
|
||||
@ -154,40 +150,24 @@ public class ElasticsearchQueryCreator extends AbstractQueryCreator<CriteriaQuer
|
||||
secondParameter = parameters.next();
|
||||
}
|
||||
|
||||
if (firstParameter instanceof GeoPoint && secondParameter instanceof String)
|
||||
return criteria.within((GeoPoint) firstParameter, (String) secondParameter);
|
||||
|
||||
if (firstParameter instanceof Point && secondParameter instanceof Distance)
|
||||
return criteria.within((Point) firstParameter, (Distance) secondParameter);
|
||||
|
||||
if (firstParameter instanceof String && secondParameter instanceof String)
|
||||
return criteria.within((String) firstParameter, (String) secondParameter);
|
||||
return doWithinIfPossible(criteria, firstParameter, secondParameter);
|
||||
}
|
||||
case NEAR: {
|
||||
Object firstParameter = parameters.next();
|
||||
|
||||
if (firstParameter instanceof GeoBox) {
|
||||
return criteria.boundedBy((GeoBox) firstParameter);
|
||||
if (firstParameter instanceof GeoBox geoBox) {
|
||||
return criteria.boundedBy(geoBox);
|
||||
}
|
||||
|
||||
if (firstParameter instanceof Box) {
|
||||
return criteria.boundedBy(GeoBox.fromBox((Box) firstParameter));
|
||||
if (firstParameter instanceof Box box) {
|
||||
return criteria.boundedBy(GeoBox.fromBox(box));
|
||||
}
|
||||
|
||||
Object secondParameter = parameters.next();
|
||||
|
||||
// "near" query can be the same query as the "within" query
|
||||
if (firstParameter instanceof GeoPoint && secondParameter instanceof String)
|
||||
return criteria.within((GeoPoint) firstParameter, (String) secondParameter);
|
||||
|
||||
if (firstParameter instanceof Point && secondParameter instanceof Distance)
|
||||
return criteria.within((Point) firstParameter, (Distance) secondParameter);
|
||||
|
||||
if (firstParameter instanceof String && secondParameter instanceof String)
|
||||
return criteria.within((String) firstParameter, (String) secondParameter);
|
||||
return doWithinIfPossible(criteria, firstParameter, secondParameter);
|
||||
}
|
||||
case EXISTS:
|
||||
case IS_NOT_NULL:
|
||||
case EXISTS, IS_NOT_NULL:
|
||||
return criteria.exists();
|
||||
case IS_NULL:
|
||||
return criteria.not().exists();
|
||||
@ -200,6 +180,32 @@ public class ElasticsearchQueryCreator extends AbstractQueryCreator<CriteriaQuer
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Do a within query if possible, otherwise return the criteria unchanged.
|
||||
*
|
||||
* @param criteria must not be {@literal null}
|
||||
* @param firstParameter must not be {@literal null}
|
||||
* @param secondParameter must not be {@literal null}
|
||||
* @return the criteria with the within query applied if possible.
|
||||
* @author Junghoon Ban
|
||||
*/
|
||||
private Criteria doWithinIfPossible(Criteria criteria, Object firstParameter, Object secondParameter) {
|
||||
|
||||
if (firstParameter instanceof GeoPoint geoPoint && secondParameter instanceof String string) {
|
||||
return criteria.within(geoPoint, string);
|
||||
}
|
||||
|
||||
if (firstParameter instanceof Point point && secondParameter instanceof Distance distance) {
|
||||
return criteria.within(point, distance);
|
||||
}
|
||||
|
||||
if (firstParameter instanceof String firstString && secondParameter instanceof String secondString) {
|
||||
return criteria.within(firstString, secondString);
|
||||
}
|
||||
|
||||
return criteria;
|
||||
}
|
||||
|
||||
private Object[] asArray(Object o) {
|
||||
if (o instanceof Collection) {
|
||||
return ((Collection<?>) o).toArray();
|
||||
|
Loading…
x
Reference in New Issue
Block a user