From 1d6a1b0f2fd9f272121cafdac1439a767fdd847d Mon Sep 17 00:00:00 2001 From: Junghoon Ban Date: Wed, 13 Dec 2023 02:45:03 +0900 Subject: [PATCH] Use switch expressions to simplify case branches. Original Pull Request #2795 Closes #2794 --- .../parser/ElasticsearchQueryCreator.java | 136 ++++++++---------- 1 file changed, 62 insertions(+), 74 deletions(-) diff --git a/src/main/java/org/springframework/data/elasticsearch/repository/query/parser/ElasticsearchQueryCreator.java b/src/main/java/org/springframework/data/elasticsearch/repository/query/parser/ElasticsearchQueryCreator.java index 600e02a8a..d132dd117 100644 --- a/src/main/java/org/springframework/data/elasticsearch/repository/query/parser/ElasticsearchQueryCreator.java +++ b/src/main/java/org/springframework/data/elasticsearch/repository/query/parser/ElasticsearchQueryCreator.java @@ -100,84 +100,72 @@ public class ElasticsearchQueryCreator extends AbstractQueryCreator criteria.is(true); + case FALSE -> criteria.is(false); + case NEGATING_SIMPLE_PROPERTY -> criteria.is(parameters.next()).not(); + case REGEX -> criteria.expression(parameters.next().toString()); + case LIKE, STARTING_WITH -> criteria.startsWith(parameters.next().toString()); + case ENDING_WITH -> criteria.endsWith(parameters.next().toString()); + case CONTAINING -> criteria.contains(parameters.next().toString()); + case GREATER_THAN -> criteria.greaterThan(parameters.next()); + case AFTER, GREATER_THAN_EQUAL -> criteria.greaterThanEqual(parameters.next()); + case LESS_THAN -> criteria.lessThan(parameters.next()); + case BEFORE, LESS_THAN_EQUAL -> criteria.lessThanEqual(parameters.next()); + case BETWEEN -> criteria.between(parameters.next(), parameters.next()); + case IN -> criteria.in(asArray(parameters.next())); + case NOT_IN -> criteria.notIn(asArray(parameters.next())); + case SIMPLE_PROPERTY, WITHIN -> this.within(part, criteria, parameters); + case NEAR -> this.near(criteria, parameters); + case EXISTS, IS_NOT_NULL -> criteria.exists(); + case IS_NULL -> criteria.not().exists(); + case IS_EMPTY -> criteria.empty(); + case IS_NOT_EMPTY -> criteria.notEmpty(); + default -> throw new InvalidDataAccessApiUsageException("Illegal criteria found '" + type + "'."); + }; + } + + private Criteria within(Part part, Criteria criteria, Iterator parameters) { + + Object firstParameter = parameters.next(); + Object secondParameter; + + if (part.getType() == Part.Type.SIMPLE_PROPERTY) { + if (part.getProperty().getType() != GeoPoint.class) { + if (firstParameter != null) { + return criteria.is(firstParameter); } else { - secondParameter = parameters.next(); + // searching for null is a must_not (exists) + return criteria.exists().not(); } - - return doWithinIfPossible(criteria, firstParameter, secondParameter); + } 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"; } - case NEAR: { - 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); - } - 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 + "'."); + } else { + secondParameter = parameters.next(); } + + 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); } /**