Skip to content

Commit e6c6d34

Browse files
authored
Fix subCriteria's not being updated.
Original Pull Request #1670 Closes #1668
1 parent 0ac1b4a commit e6c6d34

File tree

2 files changed

+157
-9
lines changed

2 files changed

+157
-9
lines changed

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
* @author Roman Puchkovskiy
7373
* @author Konrad Kurdej
7474
* @author Subhobrata Dey
75+
* @author Marc Vanbrabant
7576
* @since 3.2
7677
*/
7778
public class MappingElasticsearchConverter
@@ -805,6 +806,11 @@ public void updateCriteriaQuery(CriteriaQuery criteriaQuery, Class<?> domainClas
805806
for (Criteria chainedCriteria : criteriaQuery.getCriteria().getCriteriaChain()) {
806807
updateCriteria(chainedCriteria, persistentEntity);
807808
}
809+
for (Criteria subCriteria : criteriaQuery.getCriteria().getSubCriteria()) {
810+
for (Criteria chainedCriteria : subCriteria.getCriteriaChain()) {
811+
updateCriteria(chainedCriteria, persistentEntity);
812+
}
813+
}
808814
}
809815
}
810816

@@ -843,12 +849,6 @@ private void updateCriteria(Criteria criteria, ElasticsearchPersistentEntity<?>
843849
field.setFieldType(fieldAnnotation.type());
844850
}
845851
}
846-
847-
for (Criteria subCriteria : criteria.getSubCriteria()) {
848-
for (Criteria chainedCriteria : subCriteria.getCriteriaChain()) {
849-
updateCriteria(chainedCriteria, persistentEntity);
850-
}
851-
}
852852
}
853853
// endregion
854854

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

+151-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
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

2523
import org.json.JSONException;
2624
import org.junit.jupiter.api.BeforeEach;
@@ -109,6 +107,155 @@ void shouldMapNamesAndConvertValuesInCriteriaQuery() throws JSONException {
109107
assertEquals(expected, queryString, false);
110108
}
111109

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

0 commit comments

Comments
 (0)