2424import lombok .Setter ;
2525
2626import java .lang .reflect .Method ;
27+ import java .util .ArrayList ;
2728import java .util .Arrays ;
2829import java .util .Collection ;
2930import java .util .HashMap ;
5758/**
5859 * @author Christoph Strobl
5960 * @author Peter-Josef Meisch
61+ * @author Niklas Herder
6062 */
6163@ ExtendWith (MockitoExtension .class )
6264public class ElasticsearchStringQueryUnitTests {
@@ -101,14 +103,50 @@ void shouldEscapeStringsInQueryParameters() throws Exception {
101103 .isEqualTo ("{\" bool\" :{\" must\" : [{\" match\" : {\" prefix\" : {\" name\" : \" hello \\ \" Stranger\\ \" \" }}]}}" );
102104 }
103105
104- private org .springframework .data .elasticsearch .core .query .Query createQuery (String methodName , String ... args )
106+ @ Test // #1858
107+ @ DisplayName ("should only quote String query parameters" )
108+ void shouldOnlyEscapeStringQueryParameters () throws Exception {
109+ org .springframework .data .elasticsearch .core .query .Query query = createQuery ("findByAge" , Integer .valueOf (30 ));
110+
111+ assertThat (query ).isInstanceOf (StringQuery .class );
112+ assertThat (((StringQuery ) query ).getSource ()).isEqualTo ("{ 'bool' : { 'must' : { 'term' : { 'age' : 30 } } } }" );
113+
114+ }
115+
116+ @ Test // #1858
117+ @ DisplayName ("should only quote String collection query parameters" )
118+ void shouldOnlyEscapeStringCollectionQueryParameters () throws Exception {
119+ org .springframework .data .elasticsearch .core .query .Query query = createQuery ("findByAgeIn" ,
120+ new ArrayList <>(Arrays .asList (30 , 35 , 40 )));
121+
122+ assertThat (query ).isInstanceOf (StringQuery .class );
123+ assertThat (((StringQuery ) query ).getSource ())
124+ .isEqualTo ("{ 'bool' : { 'must' : { 'term' : { 'age' : [30,35,40] } } } }" );
125+
126+ }
127+
128+ @ Test // #1858
129+ @ DisplayName ("should escape Strings in collection query parameters" )
130+ void shouldEscapeStringsInCollectionsQueryParameters () throws Exception {
131+
132+ final List <String > another_string = Arrays .asList ("hello \" Stranger\" " , "Another string" );
133+ List <String > params = new ArrayList <>(another_string );
134+ org .springframework .data .elasticsearch .core .query .Query query = createQuery ("findByNameIn" , params );
135+
136+ assertThat (query ).isInstanceOf (StringQuery .class );
137+ assertThat (((StringQuery ) query ).getSource ()).isEqualTo (
138+ "{ 'bool' : { 'must' : { 'terms' : { 'name' : [\" hello \\ \" Stranger\\ \" \" ,\" Another string\" ] } } } }" );
139+ }
140+
141+ private org .springframework .data .elasticsearch .core .query .Query createQuery (String methodName , Object ... args )
105142 throws NoSuchMethodException {
106143
107144 Class <?>[] argTypes = Arrays .stream (args ).map (Object ::getClass ).toArray (Class []::new );
108145 ElasticsearchQueryMethod queryMethod = getQueryMethod (methodName , argTypes );
109146 ElasticsearchStringQuery elasticsearchStringQuery = queryForMethod (queryMethod );
110147 return elasticsearchStringQuery .createQuery (new ElasticsearchParametersParameterAccessor (queryMethod , args ));
111148 }
149+
112150 private ElasticsearchStringQuery queryForMethod (ElasticsearchQueryMethod queryMethod ) {
113151 return new ElasticsearchStringQuery (queryMethod , operations , queryMethod .getAnnotatedQuery ());
114152 }
@@ -122,9 +160,18 @@ private ElasticsearchQueryMethod getQueryMethod(String name, Class<?>... paramet
122160
123161 private interface SampleRepository extends Repository <Person , String > {
124162
163+ @ Query ("{ 'bool' : { 'must' : { 'term' : { 'age' : ?0 } } } }" )
164+ List <Person > findByAge (Integer age );
165+
166+ @ Query ("{ 'bool' : { 'must' : { 'term' : { 'age' : ?0 } } } }" )
167+ List <Person > findByAgeIn (ArrayList <Integer > age );
168+
125169 @ Query ("{ 'bool' : { 'must' : { 'term' : { 'name' : '?0' } } } }" )
126170 Person findByName (String name );
127171
172+ @ Query ("{ 'bool' : { 'must' : { 'terms' : { 'name' : ?0 } } } }" )
173+ Person findByNameIn (ArrayList <String > names );
174+
128175 @ Query (value = "name:(?0, ?11, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?0, ?1)" )
129176 Person findWithRepeatedPlaceholder (String arg0 , String arg1 , String arg2 , String arg3 , String arg4 , String arg5 ,
130177 String arg6 , String arg7 , String arg8 , String arg9 , String arg10 , String arg11 );
@@ -137,16 +184,27 @@ Person findWithRepeatedPlaceholder(String arg0, String arg1, String arg2, String
137184 * @author Rizwan Idrees
138185 * @author Mohsin Husen
139186 * @author Artur Konczak
187+ * @author Niklas Herder
140188 */
141189
142190 @ Document (indexName = "test-index-person-query-unittest" , replicas = 0 , refreshInterval = "-1" )
143191 static class Person {
144192
193+ @ Nullable public int age ;
145194 @ Nullable @ Id private String id ;
146195 @ Nullable private String name ;
147196 @ Nullable @ Field (type = FieldType .Nested ) private List <Car > car ;
148197 @ Nullable @ Field (type = FieldType .Nested , includeInParent = true ) private List <Book > books ;
149198
199+ @ Nullable
200+ public int getAge () {
201+ return age ;
202+ }
203+
204+ public void setAge (int age ) {
205+ this .age = age ;
206+ }
207+
150208 @ Nullable
151209 public String getId () {
152210 return id ;
0 commit comments