Skip to content

Commit 6390aaa

Browse files
committed
Polishing.
1 parent b391a4e commit 6390aaa

File tree

8 files changed

+44
-18
lines changed

8 files changed

+44
-18
lines changed

src/main/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchStringQuery.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class ElasticsearchStringQuery extends AbstractElasticsearchRepositoryQue
4242
public ElasticsearchStringQuery(ElasticsearchQueryMethod queryMethod, ElasticsearchOperations elasticsearchOperations,
4343
String queryString, QueryMethodEvaluationContextProvider evaluationContextProvider) {
4444
super(queryMethod, elasticsearchOperations);
45+
4546
Assert.notNull(queryString, "Query cannot be empty");
4647
Assert.notNull(evaluationContextProvider, "ExpressionEvaluationContextProvider must not be null");
4748

@@ -65,11 +66,11 @@ protected boolean isExistsQuery() {
6566
}
6667

6768
protected BaseQuery createQuery(ElasticsearchParametersParameterAccessor parameterAccessor) {
68-
ConversionService conversionService = elasticsearchOperations.getElasticsearchConverter().getConversionService();
69-
String queryString = new StringQueryUtil(conversionService)
70-
.replacePlaceholders(this.queryString, parameterAccessor);
7169

72-
QueryStringSpELEvaluator evaluator = new QueryStringSpELEvaluator(queryString, parameterAccessor, queryMethod,
70+
ConversionService conversionService = elasticsearchOperations.getElasticsearchConverter().getConversionService();
71+
var replacedString = new StringQueryUtil(conversionService).replacePlaceholders(this.queryString,
72+
parameterAccessor);
73+
var evaluator = new QueryStringSpELEvaluator(replacedString, parameterAccessor, queryMethod,
7374
evaluationContextProvider, conversionService);
7475
var query = new StringQuery(evaluator.evaluate());
7576
query.addSort(parameterAccessor.getSort());

src/main/java/org/springframework/data/elasticsearch/repository/query/ReactiveElasticsearchStringQuery.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.data.elasticsearch.repository.support.spel.QueryStringSpELEvaluator;
2424
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
2525
import org.springframework.expression.spel.standard.SpelExpressionParser;
26+
import org.springframework.util.Assert;
2627

2728
/**
2829
* @author Christoph Strobl
@@ -45,8 +46,11 @@ public ReactiveElasticsearchStringQuery(ReactiveElasticsearchQueryMethod queryMe
4546
public ReactiveElasticsearchStringQuery(String query, ReactiveElasticsearchQueryMethod queryMethod,
4647
ReactiveElasticsearchOperations operations, SpelExpressionParser expressionParser,
4748
QueryMethodEvaluationContextProvider evaluationContextProvider) {
48-
4949
super(queryMethod, operations);
50+
51+
Assert.notNull(query, "query must not be null");
52+
Assert.notNull(evaluationContextProvider, "evaluationContextProvider must not be null");
53+
5054
this.query = query;
5155
this.evaluationContextProvider = evaluationContextProvider;
5256
}

src/main/java/org/springframework/data/elasticsearch/repository/support/StringQueryUtil.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.data.elasticsearch.core.convert.ConversionException;
2323
import org.springframework.data.elasticsearch.repository.support.value.ElasticsearchQueryValueConversionService;
2424
import org.springframework.data.repository.query.ParameterAccessor;
25+
import org.springframework.util.Assert;
2526
import org.springframework.util.NumberUtils;
2627

2728
/**
@@ -36,15 +37,18 @@ final public class StringQueryUtil {
3637
private final ConversionService conversionService;
3738

3839
public StringQueryUtil(ConversionService conversionService) {
40+
41+
Assert.notNull(conversionService, "conversionService must not be null");
42+
3943
this.conversionService = ElasticsearchQueryValueConversionService.getInstance(conversionService);
4044
}
4145

4246
public String replacePlaceholders(String input, ParameterAccessor accessor) {
4347

4448
Matcher matcher = PARAMETER_PLACEHOLDER.matcher(input);
4549
String result = input;
46-
while (matcher.find()) {
4750

51+
while (matcher.find()) {
4852
String placeholder = Pattern.quote(matcher.group()) + "(?!\\d+)";
4953
int index = NumberUtils.parseNumber(matcher.group(1), Integer.class);
5054
String replacement = Matcher.quoteReplacement(getParameterWithIndex(accessor, index, input));
@@ -59,7 +63,8 @@ public String replacePlaceholders(String input, ParameterAccessor accessor) {
5963
private String getParameterWithIndex(ParameterAccessor accessor, int index, String input) {
6064

6165
Object parameter = accessor.getBindableValue(index);
62-
String value = conversionService.convert(parameter, String.class);
66+
String value = conversionService.convert(parameter, String.class);
67+
6368
if (value == null) {
6469
throw new ConversionException(String.format(
6570
"Parameter value can't be null for placeholder at index '%s' in query '%s' when querying elasticsearch",

src/main/java/org/springframework/data/elasticsearch/repository/support/spel/QueryStringSpELEvaluator.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import org.springframework.core.convert.ConversionService;
2222
import org.springframework.data.elasticsearch.core.convert.ConversionException;
2323
import org.springframework.data.elasticsearch.repository.query.ElasticsearchParametersParameterAccessor;
24-
import org.springframework.data.elasticsearch.repository.support.value.ElasticsearchQueryValueConversionService;
2524
import org.springframework.data.elasticsearch.repository.support.value.ElasticsearchCollectionValueToStringConverter;
25+
import org.springframework.data.elasticsearch.repository.support.value.ElasticsearchQueryValueConversionService;
2626
import org.springframework.data.elasticsearch.repository.support.value.ElasticsearchStringValueToStringConverter;
2727
import org.springframework.data.repository.query.QueryMethod;
2828
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
@@ -59,6 +59,13 @@ public class QueryStringSpELEvaluator {
5959
public QueryStringSpELEvaluator(String queryString, ElasticsearchParametersParameterAccessor parameterAccessor,
6060
QueryMethod queryMethod, QueryMethodEvaluationContextProvider evaluationContextProvider,
6161
ConversionService conversionService) {
62+
63+
Assert.notNull(queryString, "queryString must not be null");
64+
Assert.notNull(parameterAccessor, "parameterAccessor must not be null");
65+
Assert.notNull(queryMethod, "queryMethod must not be null");
66+
Assert.notNull(evaluationContextProvider, "evaluationContextProvider must not be null");
67+
Assert.notNull(conversionService, "conversionService must not be null");
68+
6269
this.queryString = queryString;
6370
this.parameterAccessor = parameterAccessor;
6471
this.queryMethod = queryMethod;
@@ -74,9 +81,11 @@ public QueryStringSpELEvaluator(String queryString, ElasticsearchParametersParam
7481
*/
7582
public String evaluate() {
7683
Expression expr = getQueryExpression(queryString);
84+
7785
if (expr != null) {
7886
EvaluationContext context = evaluationContextProvider.getEvaluationContext(parameterAccessor.getParameters(),
7987
parameterAccessor.getValues());
88+
8089
if (context instanceof StandardEvaluationContext standardEvaluationContext) {
8190
standardEvaluationContext.setTypeConverter(elasticsearchSpELTypeConverter);
8291
}
@@ -97,12 +106,14 @@ public String evaluate() {
97106
*/
98107
private String parseExpressions(Expression rootExpr, EvaluationContext context) {
99108
StringBuilder parsed = new StringBuilder();
109+
100110
if (rootExpr instanceof LiteralExpression literalExpression) {
101111
// get the string literal directly
102112
parsed.append(literalExpression.getExpressionString());
103113
} else if (rootExpr instanceof SpelExpression spelExpression) {
104114
// evaluate the value
105115
String value = spelExpression.getValue(context, String.class);
116+
106117
if (value == null) {
107118
throw new ConversionException(String.format(
108119
"Parameter value can't be null for SpEL expression '%s' in method '%s' when querying elasticsearch",

src/main/java/org/springframework/data/elasticsearch/repository/support/value/ElasticsearchCollectionValueToStringConverter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,18 @@ public Set<ConvertiblePair> getConvertibleTypes() {
7070
@Override
7171
@Nullable
7272
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
73+
7374
if (source == null) {
7475
return "[]";
7576
}
7677
Collection<?> sourceCollection = (Collection<?>) source;
78+
7779
if (sourceCollection.isEmpty()) {
7880
return "[]";
7981
}
82+
8083
StringJoiner sb = new StringJoiner(DELIMITER, "[", "]");
84+
8185
for (Object sourceElement : sourceCollection) {
8286
// ignore the null value in collection
8387
if (Objects.isNull(sourceElement)) {
@@ -86,8 +90,9 @@ public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDe
8690

8791
Object targetElement = this.conversionService.convert(
8892
sourceElement, sourceType.elementTypeDescriptor(sourceElement), targetType);
93+
8994
if (sourceElement instanceof String) {
90-
sb.add("\"" + targetElement + "\"");
95+
sb.add("\"" + targetElement + '"');
9196
} else {
9297
sb.add(String.valueOf(targetElement));
9398
}

src/main/java/org/springframework/data/elasticsearch/repository/support/value/ElasticsearchQueryValueConversionService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
/**
2929
* A {@link ConversionService} using custom converters to handle query values in elasticsearch query. If the value to be
30-
* converted beyond the scope of custom converters, it'll delegate to the {@link #delegate delegated conversion service}.
30+
* converted beyond the scope of custom converters, it'll delegate to the {@link #delegate delegated conversion
31+
* service}.
3132
* <p>
3233
* This is a better solution for converting query values in elasticsearch query, because it has all the capability the
3334
* {@link #delegate delegated conversion service} has, especially for user-registered {@link Converter}s.
@@ -44,7 +45,9 @@ public class ElasticsearchQueryValueConversionService implements ConversionServi
4445
private final ConversionService delegate;
4546

4647
private ElasticsearchQueryValueConversionService(ConversionService delegate) {
48+
4749
Assert.notNull(delegate, "delegated ConversionService must not be null");
50+
4851
this.delegate = delegate;
4952

5053
// register elasticsearch custom type converters for conversion service
@@ -85,6 +88,7 @@ public <T> T convert(@Nullable Object source, Class<T> targetType) {
8588
@Override
8689
@Nullable
8790
public Object convert(@Nullable Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
91+
8892
if (valueConversionService.canConvert(sourceType, targetType)) {
8993
return valueConversionService.convert(source, sourceType, targetType);
9094
} else {

src/main/java/org/springframework/data/elasticsearch/repository/support/value/ElasticsearchStringValueToStringConverter.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ public Set<ConvertiblePair> getConvertibleTypes() {
4040
@Override
4141
@Nullable
4242
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
43-
if (source == null) {
44-
return null;
45-
}
46-
return escape(source);
43+
return source != null ? escape(source) : null;
4744
}
4845

4946
private String escape(@Nullable Object source) {

src/test/java/org/springframework/data/elasticsearch/repositories/custommethod/CustomMethodRepositoryIntegrationTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,8 +2109,8 @@ public interface SampleCustomMethodRepository extends ElasticsearchRepository<Sa
21092109
SearchHits<SampleEntity> queryByType(String type);
21102110

21112111
/**
2112-
* The parameter is annotated with {@link Nullable} deliberately to test that our placeholder parameter will
2113-
* not accept a null parameter as query value.
2112+
* The parameter is annotated with {@link Nullable} deliberately to test that our placeholder parameter will not
2113+
* accept a null parameter as query value.
21142114
*/
21152115
@Query("{\"bool\": {\"must\": [{\"term\": {\"type\": \"?0\"}}]}}")
21162116
@Highlight(fields = { @HighlightField(name = "type") })
@@ -2444,8 +2444,7 @@ public void setVersion(@Nullable java.lang.Long version) {
24442444
}
24452445

24462446
static class SampleProperty {
2447-
@Nullable
2448-
private String first;
2447+
@Nullable private String first;
24492448
@Nullable private String last;
24502449

24512450
SampleProperty(@Nullable String first, @Nullable String last) {

0 commit comments

Comments
 (0)