Skip to content

Commit 460b4ac

Browse files
committed
Polishing.
1 parent e1a2412 commit 460b4ac

File tree

8 files changed

+48
-44
lines changed

8 files changed

+48
-44
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public ReactiveElasticsearchStringQuery(String query, ReactiveElasticsearchQuery
5252

5353
@Override
5454
protected BaseQuery createQuery(ElasticsearchParametersParameterAccessor parameterAccessor) {
55-
String queryString = new StringQueryUtil(getElasticsearchOperations().getElasticsearchConverter().getConversionService())
55+
String queryString = new StringQueryUtil(
56+
getElasticsearchOperations().getElasticsearchConverter().getConversionService())
5657
.replacePlaceholders(this.query, parameterAccessor);
5758

5859
QueryStringSpELEvaluator evaluator = new QueryStringSpELEvaluator(queryString, parameterAccessor, queryMethod,

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
*/
1616
package org.springframework.data.elasticsearch.repository.support;
1717

18+
import static org.springframework.data.querydsl.QuerydslUtils.*;
19+
20+
import java.lang.reflect.Method;
21+
import java.util.Optional;
22+
1823
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
1924
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
2025
import org.springframework.data.elasticsearch.repository.query.ElasticsearchPartQuery;
@@ -34,16 +39,9 @@
3439
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
3540
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
3641
import org.springframework.data.repository.query.RepositoryQuery;
37-
import org.springframework.expression.TypeConverter;
38-
import org.springframework.expression.spel.support.StandardTypeConverter;
3942
import org.springframework.lang.Nullable;
4043
import org.springframework.util.Assert;
4144

42-
import java.lang.reflect.Method;
43-
import java.util.Optional;
44-
45-
import static org.springframework.data.querydsl.QuerydslUtils.QUERY_DSL_PRESENT;
46-
4745
/**
4846
* Factory to create {@link ElasticsearchRepository}
4947
*

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
*/
1616
package org.springframework.data.elasticsearch.repository.support.spel;
1717

18+
import java.util.Collection;
19+
import java.util.Collections;
20+
import java.util.Objects;
21+
import java.util.Set;
22+
import java.util.StringJoiner;
23+
1824
import org.springframework.core.convert.ConversionService;
1925
import org.springframework.core.convert.TypeDescriptor;
2026
import org.springframework.core.convert.converter.GenericConverter;
2127
import org.springframework.lang.Nullable;
2228

23-
import java.util.*;
24-
2529
/**
2630
* Convert a collection into string for value part of the elasticsearch query.
2731
* <p>
@@ -30,6 +34,7 @@
3034
* <p>
3135
* eg: The value part of an elasticsearch terms query should looks like {@code ["hello \"Stranger\"","Another string"]}
3236
* for query
37+
*
3338
* <pre>
3439
* {@code
3540
* {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
*/
1616
package org.springframework.data.elasticsearch.repository.support.spel;
1717

18-
import org.springframework.core.convert.TypeDescriptor;
19-
import org.springframework.core.convert.converter.GenericConverter;
20-
import org.springframework.lang.Nullable;
21-
2218
import java.util.Collections;
2319
import java.util.Set;
2420
import java.util.regex.Matcher;
2521

22+
import org.springframework.core.convert.TypeDescriptor;
23+
import org.springframework.core.convert.converter.GenericConverter;
24+
import org.springframework.lang.Nullable;
25+
2626
/**
27-
* Values in elasticsearch query may contain quotations and should be escaped when converting.
28-
* Note that the converter should only be used in this situation, rather than common string to string conversions.
27+
* Values in elasticsearch query may contain quotations and should be escaped when converting. Note that the converter
28+
* should only be used in this situation, rather than common string to string conversions.
2929
*
3030
* @since 5.3
3131
* @author Haibo Liu

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package org.springframework.data.elasticsearch.repository.support.spel;
1717

18+
import java.util.Map;
19+
import java.util.concurrent.ConcurrentHashMap;
20+
1821
import org.springframework.data.elasticsearch.core.convert.ConversionException;
1922
import org.springframework.data.elasticsearch.repository.query.ElasticsearchParametersParameterAccessor;
2023
import org.springframework.data.repository.query.QueryMethod;
@@ -32,9 +35,6 @@
3235
import org.springframework.lang.Nullable;
3336
import org.springframework.util.Assert;
3437

35-
import java.util.Map;
36-
import java.util.concurrent.ConcurrentHashMap;
37-
3838
/**
3939
* To evaluate the SpEL expressions of the query string.
4040
*
@@ -53,12 +53,13 @@ public class QueryStringSpELEvaluator {
5353
private final TypeConverter elasticsearchSpELTypeConverter;
5454

5555
public QueryStringSpELEvaluator(String queryString, ElasticsearchParametersParameterAccessor parameterAccessor,
56-
QueryMethod queryMethod, QueryMethodEvaluationContextProvider evaluationContextProvider) {
56+
QueryMethod queryMethod, QueryMethodEvaluationContextProvider evaluationContextProvider) {
5757
this.queryString = queryString;
5858
this.parameterAccessor = parameterAccessor;
5959
this.queryMethod = queryMethod;
6060
this.evaluationContextProvider = evaluationContextProvider;
61-
this.elasticsearchSpELTypeConverter = new StandardTypeConverter(ElasticsearchValueSpELConversionService.CONVERSION_SERVICE_LAZY);
61+
this.elasticsearchSpELTypeConverter = new StandardTypeConverter(
62+
ElasticsearchValueSpELConversionService.CONVERSION_SERVICE_LAZY);
6263
}
6364

6465
/**
@@ -83,12 +84,11 @@ public String evaluate() {
8384
}
8485

8586
/**
86-
* {@link Expression#getValue(EvaluationContext, Class)} is not used because the value part in SpEL should be converted
87-
* by {@link ElasticsearchStringValueToStringConverter} or
88-
* {@link ElasticsearchCollectionValueToStringConverter} to
89-
* escape the quotations, but other literal parts in SpEL expression should not be processed with these converters.
90-
* So we just get the string value from {@link LiteralExpression} directly rather than
91-
* {@link LiteralExpression#getValue(EvaluationContext, Class)}.
87+
* {@link Expression#getValue(EvaluationContext, Class)} is not used because the value part in SpEL should be
88+
* converted by {@link ElasticsearchStringValueToStringConverter} or
89+
* {@link ElasticsearchCollectionValueToStringConverter} to escape the quotations, but other literal parts in SpEL
90+
* expression should not be processed with these converters. So we just get the string value from
91+
* {@link LiteralExpression} directly rather than {@link LiteralExpression#getValue(EvaluationContext, Class)}.
9292
*/
9393
private String parseExpressions(Expression rootExpr, EvaluationContext context) {
9494
StringBuilder parsed = new StringBuilder();

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
package org.springframework.data.elasticsearch.repositories.custommethod;
1717

1818
import static org.assertj.core.api.Assertions.*;
19-
import static org.junit.jupiter.api.Assertions.assertThrows;
19+
import static org.junit.jupiter.api.Assertions.*;
2020
import static org.springframework.data.elasticsearch.annotations.FieldType.*;
2121
import static org.springframework.data.elasticsearch.utils.IdGenerator.*;
2222

23-
import java.lang.Long;
2423
import java.util.ArrayList;
2524
import java.util.Arrays;
2625
import java.util.Collection;
@@ -2073,8 +2072,8 @@ public interface SampleCustomMethodRepository extends ElasticsearchRepository<Sa
20732072
SearchHits<SampleEntity> queryByString(String type);
20742073

20752074
/**
2076-
* The parameter is annotated with {@link Nullable} deliberately to test that our elasticsearch SpEL converters
2077-
* will not accept a null parameter as query value.
2075+
* The parameter is annotated with {@link Nullable} deliberately to test that our elasticsearch SpEL converters will
2076+
* not accept a null parameter as query value.
20782077
*/
20792078
@Query("""
20802079
{
@@ -2122,8 +2121,8 @@ public interface SampleCustomMethodRepository extends ElasticsearchRepository<Sa
21222121
SearchHits<SampleEntity> queryByBeanPropertySpEL();
21232122

21242123
/**
2125-
* The parameter is annotated with {@link Nullable} deliberately to test that our elasticsearch SpEL converters
2126-
* will not accept a null parameter as query value.
2124+
* The parameter is annotated with {@link Nullable} deliberately to test that our elasticsearch SpEL converters will
2125+
* not accept a null parameter as query value.
21272126
*/
21282127
@Query("""
21292128
{

src/test/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchStringQueryUnitTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,9 @@ void shouldEscapeBackslashesInCollectionQueryParameters() throws NoSuchMethodExc
343343

344344
assertThat(query).isInstanceOf(StringQuery.class);
345345
assertThat(((StringQuery) query).getSource()).isEqualTo(
346-
"{ 'bool' : { 'must' : { 'terms' : { 'name' : [\"param\\\\1\",\"param\\\\2\"] } } } }");
346+
"{ 'bool' : { 'must' : { 'terms' : { 'name' : [\"param\\\\1\",\"param\\\\2\"] } } } }");
347347
}
348+
348349
private org.springframework.data.elasticsearch.core.query.Query createQuery(String methodName, Object... args)
349350
throws NoSuchMethodException {
350351

src/test/java/org/springframework/data/elasticsearch/repository/support/SimpleReactiveElasticsearchRepositoryIntegrationTests.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
import static org.springframework.data.elasticsearch.core.query.Query.*;
2222
import static org.springframework.data.elasticsearch.utils.IdGenerator.*;
2323

24-
import org.springframework.data.elasticsearch.core.convert.ConversionException;
25-
import org.springframework.data.elasticsearch.repositories.custommethod.QueryParameter;
26-
import org.springframework.data.repository.query.Param;
2724
import reactor.core.publisher.Flux;
2825
import reactor.core.publisher.Mono;
2926
import reactor.test.StepVerifier;
@@ -58,9 +55,12 @@
5855
import org.springframework.data.elasticsearch.annotations.SourceFilters;
5956
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
6057
import org.springframework.data.elasticsearch.core.SearchHit;
58+
import org.springframework.data.elasticsearch.core.convert.ConversionException;
6159
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
6260
import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest;
61+
import org.springframework.data.elasticsearch.repositories.custommethod.QueryParameter;
6362
import org.springframework.data.elasticsearch.utils.IndexNameProvider;
63+
import org.springframework.data.repository.query.Param;
6464
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
6565
import org.springframework.lang.Nullable;
6666

@@ -275,7 +275,7 @@ void shouldReturnSearchHitsForParameterPropertyQuerySpEL() {
275275
.block();
276276

277277
QueryParameter param = new QueryParameter("message");
278-
278+
279279
repository.queryByParameterPropertySpEL(param)
280280
.as(StepVerifier::create) //
281281
.expectNextMatches(searchHit -> SearchHit.class.isAssignableFrom(searchHit.getClass()))//
@@ -376,7 +376,7 @@ void shouldReturnSearchHitsForParameterPropertyCollectionQuerySpEL() {
376376
.block();
377377

378378
QueryParameter param = new QueryParameter("message");
379-
379+
380380
repository.queryByParameterPropertyCollectionSpEL(List.of(param))
381381
.as(StepVerifier::create) //
382382
.expectNextMatches(searchHit -> SearchHit.class.isAssignableFrom(searchHit.getClass()))//
@@ -392,7 +392,7 @@ void shouldReturnSearchHitsForParameterPropertyCollectionQuerySpELWithParamAnnot
392392
.block();
393393

394394
QueryParameter param = new QueryParameter("message");
395-
395+
396396
repository.queryByParameterPropertyCollectionSpELWithParamAnnotation(List.of(param))
397397
.as(StepVerifier::create) //
398398
.expectNextMatches(searchHit -> SearchHit.class.isAssignableFrom(searchHit.getClass()))//
@@ -954,8 +954,8 @@ interface ReactiveSampleEntityRepository extends ReactiveCrudRepository<SampleEn
954954
Flux<SampleEntity> findAllViaAnnotatedQueryByMessageLikePaged(String message, Pageable pageable);
955955

956956
/**
957-
* The parameter is annotated with {@link Nullable} deliberately to test that our elasticsearch SpEL converters
958-
* will not accept a null parameter as query value.
957+
* The parameter is annotated with {@link Nullable} deliberately to test that our elasticsearch SpEL converters will
958+
* not accept a null parameter as query value.
959959
*/
960960
@Query("""
961961
{
@@ -1003,8 +1003,8 @@ interface ReactiveSampleEntityRepository extends ReactiveCrudRepository<SampleEn
10031003
Flux<SearchHit<SampleEntity>> queryByBeanPropertySpEL();
10041004

10051005
/**
1006-
* The parameter is annotated with {@link Nullable} deliberately to test that our elasticsearch SpEL converters
1007-
* will not accept a null parameter as query value.
1006+
* The parameter is annotated with {@link Nullable} deliberately to test that our elasticsearch SpEL converters will
1007+
* not accept a null parameter as query value.
10081008
*/
10091009
@Query("""
10101010
{

0 commit comments

Comments
 (0)