mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-07-12 13:23:26 +00:00
Use switch expressions to simplify case branches.
Original Pull Request #2795 Closes #2794
This commit is contained in:
parent
72e8f41de5
commit
1d6a1b0f2f
@ -100,84 +100,72 @@ public class ElasticsearchQueryCreator extends AbstractQueryCreator<CriteriaQuer
|
|||||||
|
|
||||||
Part.Type type = part.getType();
|
Part.Type type = part.getType();
|
||||||
|
|
||||||
switch (type) {
|
return switch (type) {
|
||||||
case TRUE:
|
case TRUE -> criteria.is(true);
|
||||||
return criteria.is(true);
|
case FALSE -> criteria.is(false);
|
||||||
case FALSE:
|
case NEGATING_SIMPLE_PROPERTY -> criteria.is(parameters.next()).not();
|
||||||
return criteria.is(false);
|
case REGEX -> criteria.expression(parameters.next().toString());
|
||||||
case NEGATING_SIMPLE_PROPERTY:
|
case LIKE, STARTING_WITH -> criteria.startsWith(parameters.next().toString());
|
||||||
return criteria.is(parameters.next()).not();
|
case ENDING_WITH -> criteria.endsWith(parameters.next().toString());
|
||||||
case REGEX:
|
case CONTAINING -> criteria.contains(parameters.next().toString());
|
||||||
return criteria.expression(parameters.next().toString());
|
case GREATER_THAN -> criteria.greaterThan(parameters.next());
|
||||||
case LIKE, STARTING_WITH:
|
case AFTER, GREATER_THAN_EQUAL -> criteria.greaterThanEqual(parameters.next());
|
||||||
return criteria.startsWith(parameters.next().toString());
|
case LESS_THAN -> criteria.lessThan(parameters.next());
|
||||||
case ENDING_WITH:
|
case BEFORE, LESS_THAN_EQUAL -> criteria.lessThanEqual(parameters.next());
|
||||||
return criteria.endsWith(parameters.next().toString());
|
case BETWEEN -> criteria.between(parameters.next(), parameters.next());
|
||||||
case CONTAINING:
|
case IN -> criteria.in(asArray(parameters.next()));
|
||||||
return criteria.contains(parameters.next().toString());
|
case NOT_IN -> criteria.notIn(asArray(parameters.next()));
|
||||||
case GREATER_THAN:
|
case SIMPLE_PROPERTY, WITHIN -> this.within(part, criteria, parameters);
|
||||||
return criteria.greaterThan(parameters.next());
|
case NEAR -> this.near(criteria, parameters);
|
||||||
case AFTER, GREATER_THAN_EQUAL:
|
case EXISTS, IS_NOT_NULL -> criteria.exists();
|
||||||
return criteria.greaterThanEqual(parameters.next());
|
case IS_NULL -> criteria.not().exists();
|
||||||
case LESS_THAN:
|
case IS_EMPTY -> criteria.empty();
|
||||||
return criteria.lessThan(parameters.next());
|
case IS_NOT_EMPTY -> criteria.notEmpty();
|
||||||
case BEFORE, LESS_THAN_EQUAL:
|
default -> throw new InvalidDataAccessApiUsageException("Illegal criteria found '" + type + "'.");
|
||||||
return criteria.lessThanEqual(parameters.next());
|
};
|
||||||
case BETWEEN:
|
}
|
||||||
return criteria.between(parameters.next(), parameters.next());
|
|
||||||
case IN:
|
private Criteria within(Part part, Criteria criteria, Iterator<?> parameters) {
|
||||||
return criteria.in(asArray(parameters.next()));
|
|
||||||
case NOT_IN:
|
Object firstParameter = parameters.next();
|
||||||
return criteria.notIn(asArray(parameters.next()));
|
Object secondParameter;
|
||||||
case SIMPLE_PROPERTY, WITHIN: {
|
|
||||||
Object firstParameter = parameters.next();
|
if (part.getType() == Part.Type.SIMPLE_PROPERTY) {
|
||||||
Object secondParameter = null;
|
if (part.getProperty().getType() != GeoPoint.class) {
|
||||||
if (type == Part.Type.SIMPLE_PROPERTY) {
|
if (firstParameter != null) {
|
||||||
if (part.getProperty().getType() != GeoPoint.class) {
|
return criteria.is(firstParameter);
|
||||||
if (firstParameter != null) {
|
|
||||||
return criteria.is(firstParameter);
|
|
||||||
} else {
|
|
||||||
// searching for null is a must_not (exists)
|
|
||||||
return criteria.exists().not();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// it means it's a simple find with exact geopoint matching (e.g. findByLocation)
|
|
||||||
// and because Elasticsearch does not have any kind of query with just a geopoint
|
|
||||||
// as argument we use a "geo distance" query with a distance of one meter.
|
|
||||||
secondParameter = ".001km";
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
secondParameter = parameters.next();
|
// searching for null is a must_not (exists)
|
||||||
|
return criteria.exists().not();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
return doWithinIfPossible(criteria, firstParameter, secondParameter);
|
// it means it's a simple find with exact geopoint matching (e.g. findByLocation)
|
||||||
|
// and because Elasticsearch does not have any kind of query with just a geopoint
|
||||||
|
// as argument we use a "geo distance" query with a distance of one meter.
|
||||||
|
secondParameter = ".001km";
|
||||||
}
|
}
|
||||||
case NEAR: {
|
} else {
|
||||||
Object firstParameter = parameters.next();
|
secondParameter = parameters.next();
|
||||||
|
|
||||||
if (firstParameter instanceof GeoBox geoBox) {
|
|
||||||
return criteria.boundedBy(geoBox);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (firstParameter instanceof Box box) {
|
|
||||||
return criteria.boundedBy(GeoBox.fromBox(box));
|
|
||||||
}
|
|
||||||
|
|
||||||
Object secondParameter = parameters.next();
|
|
||||||
|
|
||||||
return doWithinIfPossible(criteria, firstParameter, secondParameter);
|
|
||||||
}
|
|
||||||
case EXISTS, IS_NOT_NULL:
|
|
||||||
return criteria.exists();
|
|
||||||
case IS_NULL:
|
|
||||||
return criteria.not().exists();
|
|
||||||
case IS_EMPTY:
|
|
||||||
return criteria.empty();
|
|
||||||
case IS_NOT_EMPTY:
|
|
||||||
return criteria.notEmpty();
|
|
||||||
default:
|
|
||||||
throw new InvalidDataAccessApiUsageException("Illegal criteria found '" + type + "'.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return doWithinIfPossible(criteria, firstParameter, secondParameter);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Criteria near(Criteria criteria, Iterator<?> parameters) {
|
||||||
|
|
||||||
|
Object firstParameter = parameters.next();
|
||||||
|
|
||||||
|
if (firstParameter instanceof GeoBox geoBox) {
|
||||||
|
return criteria.boundedBy(geoBox);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (firstParameter instanceof Box box) {
|
||||||
|
return criteria.boundedBy(GeoBox.fromBox(box));
|
||||||
|
}
|
||||||
|
|
||||||
|
Object secondParameter = parameters.next();
|
||||||
|
|
||||||
|
return doWithinIfPossible(criteria, firstParameter, secondParameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user