Skip to content

Commit 42fda1d

Browse files
author
Marc Vanbrabant
committed
#1668 Fix subCriteria's not being updated
1 parent 0ac1b4a commit 42fda1d

File tree

2 files changed

+160
-9
lines changed

2 files changed

+160
-9
lines changed

Diff for: src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,11 @@ public void updateCriteriaQuery(CriteriaQuery criteriaQuery, Class<?> domainClas
805805
for (Criteria chainedCriteria : criteriaQuery.getCriteria().getCriteriaChain()) {
806806
updateCriteria(chainedCriteria, persistentEntity);
807807
}
808+
for (Criteria subCriteria : criteriaQuery.getCriteria().getSubCriteria()) {
809+
for (Criteria chainedCriteria : subCriteria.getCriteriaChain()) {
810+
updateCriteria(chainedCriteria, persistentEntity);
811+
}
812+
}
808813
}
809814
}
810815

@@ -843,12 +848,6 @@ private void updateCriteria(Criteria criteria, ElasticsearchPersistentEntity<?>
843848
field.setFieldType(fieldAnnotation.type());
844849
}
845850
}
846-
847-
for (Criteria subCriteria : criteria.getSubCriteria()) {
848-
for (Criteria chainedCriteria : subCriteria.getCriteriaChain()) {
849-
updateCriteria(chainedCriteria, persistentEntity);
850-
}
851-
}
852851
}
853852
// endregion
854853

Diff for: src/test/java/org/springframework/data/elasticsearch/core/CriteriaQueryMappingUnitTests.java

+155-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
import static org.skyscreamer.jsonassert.JSONAssert.*;
1919

2020
import java.time.LocalDate;
21-
import java.util.Base64;
22-
import java.util.Collections;
23-
import java.util.Objects;
21+
import java.util.*;
2422

23+
import org.apache.commons.lang3.time.DateUtils;
2524
import org.json.JSONException;
2625
import org.junit.jupiter.api.BeforeEach;
2726
import org.junit.jupiter.api.DisplayName;
@@ -109,6 +108,158 @@ void shouldMapNamesAndConvertValuesInCriteriaQuery() throws JSONException {
109108
assertEquals(expected, queryString, false);
110109
}
111110

111+
@Test
112+
// DATAES-1668
113+
void shouldMapNamesAndConvertValuesInCriteriaQueryForSubCriteria() throws JSONException {
114+
115+
// use POJO properties and types in the query building
116+
CriteriaQuery criteriaQuery = new CriteriaQuery( //
117+
Criteria.or()
118+
.subCriteria( Criteria.where("birthDate") //
119+
.between(LocalDate.of(1989, 11, 9), LocalDate.of(1990, 11, 9)) ) //
120+
.subCriteria( Criteria.where( "birthDate").is(LocalDate.of(2019, 12, 28)) ) //
121+
);
122+
123+
// mapped field name and converted parameter
124+
String expected = "{\n" +
125+
" \"bool\" : {\n" +
126+
" \"should\" : [\n" +
127+
" {\n" +
128+
" \"bool\" : {\n" +
129+
" \"must\" : [\n" +
130+
" {\n" +
131+
" \"range\" : {\n" +
132+
" \"birth-date\" : {\n" +
133+
" \"from\" : \"09.11.1989\",\n" +
134+
" \"to\" : \"09.11.1990\",\n" +
135+
" \"include_lower\" : true,\n" +
136+
" \"include_upper\" : true,\n" +
137+
" \"boost\" : 1.0\n" +
138+
" }\n" +
139+
" }\n" +
140+
" }\n" +
141+
" ],\n" +
142+
" \"adjust_pure_negative\" : true,\n" +
143+
" \"boost\" : 1.0\n" +
144+
" }\n" +
145+
" },\n" +
146+
" {\n" +
147+
" \"bool\" : {\n" +
148+
" \"must\" : [\n" +
149+
" {\n" +
150+
" \"query_string\" : {\n" +
151+
" \"query\" : \"28.12.2019\",\n" +
152+
" \"fields\" : [\n" +
153+
" \"birth-date^1.0\"\n" +
154+
" ],\n" +
155+
" \"type\" : \"best_fields\",\n" +
156+
" \"default_operator\" : \"and\",\n" +
157+
" \"max_determinized_states\" : 10000,\n" +
158+
" \"enable_position_increments\" : true,\n" +
159+
" \"fuzziness\" : \"AUTO\",\n" +
160+
" \"fuzzy_prefix_length\" : 0,\n" +
161+
" \"fuzzy_max_expansions\" : 50,\n" +
162+
" \"phrase_slop\" : 0,\n" +
163+
" \"escape\" : false,\n" +
164+
" \"auto_generate_synonyms_phrase_query\" : true,\n" +
165+
" \"fuzzy_transpositions\" : true,\n" +
166+
" \"boost\" : 1.0\n" +
167+
" }\n" +
168+
" }\n" +
169+
" ],\n" +
170+
" \"adjust_pure_negative\" : true,\n" +
171+
" \"boost\" : 1.0\n" +
172+
" }\n" +
173+
" }\n" +
174+
" ],\n" +
175+
" \"adjust_pure_negative\" : true,\n" +
176+
" \"boost\" : 1.0\n" +
177+
" }\n" +
178+
"}"; //
179+
180+
mappingElasticsearchConverter.updateQuery(criteriaQuery, Person.class);
181+
String queryString = new CriteriaQueryProcessor().createQuery(criteriaQuery.getCriteria()).toString();
182+
183+
assertEquals(expected, queryString, false);
184+
}
185+
186+
@Test
187+
// DATAES-1668
188+
void shouldMapNamesAndConvertValuesInCriteriaQueryForSubCriteriaWithDate() throws JSONException {
189+
Calendar calendar = Calendar.getInstance();
190+
//calendar.set(1982, Calendar.FEBRUARY, 28, 13, 2, 1 );
191+
calendar.setTimeInMillis(383745721653L);
192+
// use POJO properties and types in the query building
193+
CriteriaQuery criteriaQuery = new CriteriaQuery( //
194+
Criteria.or()
195+
.subCriteria( Criteria.where("birthDate") //
196+
.between(LocalDate.of(1989, 11, 9), LocalDate.of(1990, 11, 9)) ) //
197+
.subCriteria( Criteria.where( "createdDate").is(calendar.getTime()) ) //
198+
);
199+
200+
// mapped field name and converted parameter
201+
String expected = "{\n" +
202+
" \"bool\" : {\n" +
203+
" \"should\" : [\n" +
204+
" {\n" +
205+
" \"bool\" : {\n" +
206+
" \"must\" : [\n" +
207+
" {\n" +
208+
" \"range\" : {\n" +
209+
" \"birth-date\" : {\n" +
210+
" \"from\" : \"09.11.1989\",\n" +
211+
" \"to\" : \"09.11.1990\",\n" +
212+
" \"include_lower\" : true,\n" +
213+
" \"include_upper\" : true,\n" +
214+
" \"boost\" : 1.0\n" +
215+
" }\n" +
216+
" }\n" +
217+
" }\n" +
218+
" ],\n" +
219+
" \"adjust_pure_negative\" : true,\n" +
220+
" \"boost\" : 1.0\n" +
221+
" }\n" +
222+
" },\n" +
223+
" {\n" +
224+
" \"bool\" : {\n" +
225+
" \"must\" : [\n" +
226+
" {\n" +
227+
" \"query_string\" : {\n" +
228+
" \"query\" : \"383745721653\",\n" +
229+
" \"fields\" : [\n" +
230+
" \"created-date^1.0\"\n" +
231+
" ],\n" +
232+
" \"type\" : \"best_fields\",\n" +
233+
" \"default_operator\" : \"and\",\n" +
234+
" \"max_determinized_states\" : 10000,\n" +
235+
" \"enable_position_increments\" : true,\n" +
236+
" \"fuzziness\" : \"AUTO\",\n" +
237+
" \"fuzzy_prefix_length\" : 0,\n" +
238+
" \"fuzzy_max_expansions\" : 50,\n" +
239+
" \"phrase_slop\" : 0,\n" +
240+
" \"escape\" : false,\n" +
241+
" \"auto_generate_synonyms_phrase_query\" : true,\n" +
242+
" \"fuzzy_transpositions\" : true,\n" +
243+
" \"boost\" : 1.0\n" +
244+
" }\n" +
245+
" }\n" +
246+
" ],\n" +
247+
" \"adjust_pure_negative\" : true,\n" +
248+
" \"boost\" : 1.0\n" +
249+
" }\n" +
250+
" }\n" +
251+
" ],\n" +
252+
" \"adjust_pure_negative\" : true,\n" +
253+
" \"boost\" : 1.0\n" +
254+
" }\n" +
255+
"}"; //
256+
257+
mappingElasticsearchConverter.updateQuery(criteriaQuery, Person.class);
258+
String queryString = new CriteriaQueryProcessor().createQuery(criteriaQuery.getCriteria()).toString();
259+
260+
assertEquals(expected, queryString, false);
261+
}
262+
112263
@Test
113264
// DATAES-706
114265
void shouldMapNamesAndValuesInSubCriteriaQuery() throws JSONException {
@@ -197,6 +348,7 @@ static class Person {
197348
@Nullable @Id String id;
198349
@Nullable @Field(name = "first-name") String firstName;
199350
@Nullable @Field(name = "last-name") String lastName;
351+
@Nullable @Field(name = "created-date", type = FieldType.Date, format = DateFormat.epoch_millis) Date createdDate;
200352
@Nullable @Field(name = "birth-date", type = FieldType.Date, format = DateFormat.custom,
201353
pattern = "dd.MM.uuuu") LocalDate birthDate;
202354
}

0 commit comments

Comments
 (0)