Polishing.

This commit is contained in:
Peter-Josef Meisch 2024-01-19 19:38:56 +01:00
parent e1a2412651
commit 460b4ac0f5
No known key found for this signature in database
GPG Key ID: DE108246970C7708
8 changed files with 48 additions and 44 deletions

View File

@ -52,7 +52,8 @@ public class ReactiveElasticsearchStringQuery extends AbstractReactiveElasticsea
@Override @Override
protected BaseQuery createQuery(ElasticsearchParametersParameterAccessor parameterAccessor) { protected BaseQuery createQuery(ElasticsearchParametersParameterAccessor parameterAccessor) {
String queryString = new StringQueryUtil(getElasticsearchOperations().getElasticsearchConverter().getConversionService()) String queryString = new StringQueryUtil(
getElasticsearchOperations().getElasticsearchConverter().getConversionService())
.replacePlaceholders(this.query, parameterAccessor); .replacePlaceholders(this.query, parameterAccessor);
QueryStringSpELEvaluator evaluator = new QueryStringSpELEvaluator(queryString, parameterAccessor, queryMethod, QueryStringSpELEvaluator evaluator = new QueryStringSpELEvaluator(queryString, parameterAccessor, queryMethod,

View File

@ -15,6 +15,11 @@
*/ */
package org.springframework.data.elasticsearch.repository.support; package org.springframework.data.elasticsearch.repository.support;
import static org.springframework.data.querydsl.QuerydslUtils.*;
import java.lang.reflect.Method;
import java.util.Optional;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.data.elasticsearch.repository.query.ElasticsearchPartQuery; import org.springframework.data.elasticsearch.repository.query.ElasticsearchPartQuery;
@ -34,16 +39,9 @@ import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.data.repository.query.QueryLookupStrategy.Key; import org.springframework.data.repository.query.QueryLookupStrategy.Key;
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider; import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
import org.springframework.data.repository.query.RepositoryQuery; import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.expression.TypeConverter;
import org.springframework.expression.spel.support.StandardTypeConverter;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import java.lang.reflect.Method;
import java.util.Optional;
import static org.springframework.data.querydsl.QuerydslUtils.QUERY_DSL_PRESENT;
/** /**
* Factory to create {@link ElasticsearchRepository} * Factory to create {@link ElasticsearchRepository}
* *

View File

@ -15,13 +15,17 @@
*/ */
package org.springframework.data.elasticsearch.repository.support.spel; package org.springframework.data.elasticsearch.repository.support.spel;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import java.util.Set;
import java.util.StringJoiner;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter; import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import java.util.*;
/** /**
* Convert a collection into string for value part of the elasticsearch query. * Convert a collection into string for value part of the elasticsearch query.
* <p> * <p>
@ -30,6 +34,7 @@ import java.util.*;
* <p> * <p>
* eg: The value part of an elasticsearch terms query should looks like {@code ["hello \"Stranger\"","Another string"]} * eg: The value part of an elasticsearch terms query should looks like {@code ["hello \"Stranger\"","Another string"]}
* for query * for query
*
* <pre> * <pre>
* {@code * {@code
* { * {

View File

@ -15,17 +15,17 @@
*/ */
package org.springframework.data.elasticsearch.repository.support.spel; package org.springframework.data.elasticsearch.repository.support.spel;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.lang.Nullable;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.lang.Nullable;
/** /**
* Values in elasticsearch query may contain quotations and should be escaped when converting. * Values in elasticsearch query may contain quotations and should be escaped when converting. Note that the converter
* Note that the converter should only be used in this situation, rather than common string to string conversions. * should only be used in this situation, rather than common string to string conversions.
* *
* @since 5.3 * @since 5.3
* @author Haibo Liu * @author Haibo Liu

View File

@ -15,6 +15,9 @@
*/ */
package org.springframework.data.elasticsearch.repository.support.spel; package org.springframework.data.elasticsearch.repository.support.spel;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.data.elasticsearch.core.convert.ConversionException; import org.springframework.data.elasticsearch.core.convert.ConversionException;
import org.springframework.data.elasticsearch.repository.query.ElasticsearchParametersParameterAccessor; import org.springframework.data.elasticsearch.repository.query.ElasticsearchParametersParameterAccessor;
import org.springframework.data.repository.query.QueryMethod; import org.springframework.data.repository.query.QueryMethod;
@ -32,9 +35,6 @@ import org.springframework.expression.spel.support.StandardTypeConverter;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/** /**
* To evaluate the SpEL expressions of the query string. * To evaluate the SpEL expressions of the query string.
* *
@ -53,12 +53,13 @@ public class QueryStringSpELEvaluator {
private final TypeConverter elasticsearchSpELTypeConverter; private final TypeConverter elasticsearchSpELTypeConverter;
public QueryStringSpELEvaluator(String queryString, ElasticsearchParametersParameterAccessor parameterAccessor, public QueryStringSpELEvaluator(String queryString, ElasticsearchParametersParameterAccessor parameterAccessor,
QueryMethod queryMethod, QueryMethodEvaluationContextProvider evaluationContextProvider) { QueryMethod queryMethod, QueryMethodEvaluationContextProvider evaluationContextProvider) {
this.queryString = queryString; this.queryString = queryString;
this.parameterAccessor = parameterAccessor; this.parameterAccessor = parameterAccessor;
this.queryMethod = queryMethod; this.queryMethod = queryMethod;
this.evaluationContextProvider = evaluationContextProvider; this.evaluationContextProvider = evaluationContextProvider;
this.elasticsearchSpELTypeConverter = new StandardTypeConverter(ElasticsearchValueSpELConversionService.CONVERSION_SERVICE_LAZY); this.elasticsearchSpELTypeConverter = new StandardTypeConverter(
ElasticsearchValueSpELConversionService.CONVERSION_SERVICE_LAZY);
} }
/** /**
@ -83,12 +84,11 @@ public class QueryStringSpELEvaluator {
} }
/** /**
* {@link Expression#getValue(EvaluationContext, Class)} is not used because the value part in SpEL should be converted * {@link Expression#getValue(EvaluationContext, Class)} is not used because the value part in SpEL should be
* by {@link ElasticsearchStringValueToStringConverter} or * converted by {@link ElasticsearchStringValueToStringConverter} or
* {@link ElasticsearchCollectionValueToStringConverter} to * {@link ElasticsearchCollectionValueToStringConverter} to escape the quotations, but other literal parts in SpEL
* escape the quotations, but other literal parts in SpEL expression should not be processed with these converters. * expression should not be processed with these converters. So we just get the string value from
* So we just get the string value from {@link LiteralExpression} directly rather than * {@link LiteralExpression} directly rather than {@link LiteralExpression#getValue(EvaluationContext, Class)}.
* {@link LiteralExpression#getValue(EvaluationContext, Class)}.
*/ */
private String parseExpressions(Expression rootExpr, EvaluationContext context) { private String parseExpressions(Expression rootExpr, EvaluationContext context) {
StringBuilder parsed = new StringBuilder(); StringBuilder parsed = new StringBuilder();

View File

@ -16,11 +16,10 @@
package org.springframework.data.elasticsearch.repositories.custommethod; package org.springframework.data.elasticsearch.repositories.custommethod;
import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.data.elasticsearch.annotations.FieldType.*; import static org.springframework.data.elasticsearch.annotations.FieldType.*;
import static org.springframework.data.elasticsearch.utils.IdGenerator.*; import static org.springframework.data.elasticsearch.utils.IdGenerator.*;
import java.lang.Long;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -2073,8 +2072,8 @@ public abstract class CustomMethodRepositoryIntegrationTests {
SearchHits<SampleEntity> queryByString(String type); SearchHits<SampleEntity> queryByString(String type);
/** /**
* The parameter is annotated with {@link Nullable} deliberately to test that our elasticsearch SpEL converters * The parameter is annotated with {@link Nullable} deliberately to test that our elasticsearch SpEL converters will
* will not accept a null parameter as query value. * not accept a null parameter as query value.
*/ */
@Query(""" @Query("""
{ {
@ -2122,8 +2121,8 @@ public abstract class CustomMethodRepositoryIntegrationTests {
SearchHits<SampleEntity> queryByBeanPropertySpEL(); SearchHits<SampleEntity> queryByBeanPropertySpEL();
/** /**
* The parameter is annotated with {@link Nullable} deliberately to test that our elasticsearch SpEL converters * The parameter is annotated with {@link Nullable} deliberately to test that our elasticsearch SpEL converters will
* will not accept a null parameter as query value. * not accept a null parameter as query value.
*/ */
@Query(""" @Query("""
{ {

View File

@ -343,8 +343,9 @@ public class ElasticsearchStringQueryUnitTests extends ElasticsearchStringQueryU
assertThat(query).isInstanceOf(StringQuery.class); assertThat(query).isInstanceOf(StringQuery.class);
assertThat(((StringQuery) query).getSource()).isEqualTo( assertThat(((StringQuery) query).getSource()).isEqualTo(
"{ 'bool' : { 'must' : { 'terms' : { 'name' : [\"param\\\\1\",\"param\\\\2\"] } } } }"); "{ 'bool' : { 'must' : { 'terms' : { 'name' : [\"param\\\\1\",\"param\\\\2\"] } } } }");
} }
private org.springframework.data.elasticsearch.core.query.Query createQuery(String methodName, Object... args) private org.springframework.data.elasticsearch.core.query.Query createQuery(String methodName, Object... args)
throws NoSuchMethodException { throws NoSuchMethodException {

View File

@ -21,9 +21,6 @@ import static org.springframework.data.elasticsearch.core.IndexOperationsAdapter
import static org.springframework.data.elasticsearch.core.query.Query.*; import static org.springframework.data.elasticsearch.core.query.Query.*;
import static org.springframework.data.elasticsearch.utils.IdGenerator.*; import static org.springframework.data.elasticsearch.utils.IdGenerator.*;
import org.springframework.data.elasticsearch.core.convert.ConversionException;
import org.springframework.data.elasticsearch.repositories.custommethod.QueryParameter;
import org.springframework.data.repository.query.Param;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.test.StepVerifier; import reactor.test.StepVerifier;
@ -58,9 +55,12 @@ import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.annotations.SourceFilters; import org.springframework.data.elasticsearch.annotations.SourceFilters;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations; import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHit; import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.convert.ConversionException;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest; import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest;
import org.springframework.data.elasticsearch.repositories.custommethod.QueryParameter;
import org.springframework.data.elasticsearch.utils.IndexNameProvider; import org.springframework.data.elasticsearch.utils.IndexNameProvider;
import org.springframework.data.repository.query.Param;
import org.springframework.data.repository.reactive.ReactiveCrudRepository; import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
@ -954,8 +954,8 @@ abstract class SimpleReactiveElasticsearchRepositoryIntegrationTests {
Flux<SampleEntity> findAllViaAnnotatedQueryByMessageLikePaged(String message, Pageable pageable); Flux<SampleEntity> findAllViaAnnotatedQueryByMessageLikePaged(String message, Pageable pageable);
/** /**
* The parameter is annotated with {@link Nullable} deliberately to test that our elasticsearch SpEL converters * The parameter is annotated with {@link Nullable} deliberately to test that our elasticsearch SpEL converters will
* will not accept a null parameter as query value. * not accept a null parameter as query value.
*/ */
@Query(""" @Query("""
{ {
@ -1003,8 +1003,8 @@ abstract class SimpleReactiveElasticsearchRepositoryIntegrationTests {
Flux<SearchHit<SampleEntity>> queryByBeanPropertySpEL(); Flux<SearchHit<SampleEntity>> queryByBeanPropertySpEL();
/** /**
* The parameter is annotated with {@link Nullable} deliberately to test that our elasticsearch SpEL converters * The parameter is annotated with {@link Nullable} deliberately to test that our elasticsearch SpEL converters will
* will not accept a null parameter as query value. * not accept a null parameter as query value.
*/ */
@Query(""" @Query("""
{ {