From e6c6d349488584328d897b3963fcefe4e73e5410 Mon Sep 17 00:00:00 2001 From: slyoldfox Date: Fri, 29 Jan 2021 15:30:42 +0100 Subject: [PATCH] Fix subCriteria's not being updated. Original Pull Request #1670 Closes #1668 --- .../MappingElasticsearchConverter.java | 12 +- .../core/CriteriaQueryMappingUnitTests.java | 154 +++++++++++++++++- 2 files changed, 157 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java b/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java index 07c3e861b..94ca20771 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java @@ -72,6 +72,7 @@ import org.springframework.util.ObjectUtils; * @author Roman Puchkovskiy * @author Konrad Kurdej * @author Subhobrata Dey + * @author Marc Vanbrabant * @since 3.2 */ public class MappingElasticsearchConverter @@ -805,6 +806,11 @@ public class MappingElasticsearchConverter for (Criteria chainedCriteria : criteriaQuery.getCriteria().getCriteriaChain()) { updateCriteria(chainedCriteria, persistentEntity); } + for (Criteria subCriteria : criteriaQuery.getCriteria().getSubCriteria()) { + for (Criteria chainedCriteria : subCriteria.getCriteriaChain()) { + updateCriteria(chainedCriteria, persistentEntity); + } + } } } @@ -843,12 +849,6 @@ public class MappingElasticsearchConverter field.setFieldType(fieldAnnotation.type()); } } - - for (Criteria subCriteria : criteria.getSubCriteria()) { - for (Criteria chainedCriteria : subCriteria.getCriteriaChain()) { - updateCriteria(chainedCriteria, persistentEntity); - } - } } // endregion diff --git a/src/test/java/org/springframework/data/elasticsearch/core/CriteriaQueryMappingUnitTests.java b/src/test/java/org/springframework/data/elasticsearch/core/CriteriaQueryMappingUnitTests.java index 6607d1faa..bce262a9b 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/CriteriaQueryMappingUnitTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/CriteriaQueryMappingUnitTests.java @@ -18,9 +18,7 @@ package org.springframework.data.elasticsearch.core; import static org.skyscreamer.jsonassert.JSONAssert.*; import java.time.LocalDate; -import java.util.Base64; -import java.util.Collections; -import java.util.Objects; +import java.util.*; import org.json.JSONException; import org.junit.jupiter.api.BeforeEach; @@ -109,6 +107,155 @@ public class CriteriaQueryMappingUnitTests { assertEquals(expected, queryString, false); } + @Test + // #1668 + void shouldMapNamesAndConvertValuesInCriteriaQueryForSubCriteria() throws JSONException { + + // use POJO properties and types in the query building + CriteriaQuery criteriaQuery = new CriteriaQuery( // + Criteria.or() + .subCriteria( Criteria.where("birthDate") // + .between(LocalDate.of(1989, 11, 9), LocalDate.of(1990, 11, 9)) ) // + .subCriteria( Criteria.where( "birthDate").is(LocalDate.of(2019, 12, 28)) ) // + ); + + // mapped field name and converted parameter + String expected = "{\n" + + " \"bool\" : {\n" + + " \"should\" : [\n" + + " {\n" + + " \"bool\" : {\n" + + " \"must\" : [\n" + + " {\n" + + " \"range\" : {\n" + + " \"birth-date\" : {\n" + + " \"from\" : \"09.11.1989\",\n" + + " \"to\" : \"09.11.1990\",\n" + + " \"include_lower\" : true,\n" + + " \"include_upper\" : true,\n" + + " \"boost\" : 1.0\n" + + " }\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"adjust_pure_negative\" : true,\n" + + " \"boost\" : 1.0\n" + + " }\n" + + " },\n" + + " {\n" + + " \"bool\" : {\n" + + " \"must\" : [\n" + + " {\n" + + " \"query_string\" : {\n" + + " \"query\" : \"28.12.2019\",\n" + + " \"fields\" : [\n" + + " \"birth-date^1.0\"\n" + + " ],\n" + + " \"type\" : \"best_fields\",\n" + + " \"default_operator\" : \"and\",\n" + + " \"max_determinized_states\" : 10000,\n" + + " \"enable_position_increments\" : true,\n" + + " \"fuzziness\" : \"AUTO\",\n" + + " \"fuzzy_prefix_length\" : 0,\n" + + " \"fuzzy_max_expansions\" : 50,\n" + + " \"phrase_slop\" : 0,\n" + + " \"escape\" : false,\n" + + " \"auto_generate_synonyms_phrase_query\" : true,\n" + + " \"fuzzy_transpositions\" : true,\n" + + " \"boost\" : 1.0\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"adjust_pure_negative\" : true,\n" + + " \"boost\" : 1.0\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"adjust_pure_negative\" : true,\n" + + " \"boost\" : 1.0\n" + + " }\n" + + "}"; // + + mappingElasticsearchConverter.updateQuery(criteriaQuery, Person.class); + String queryString = new CriteriaQueryProcessor().createQuery(criteriaQuery.getCriteria()).toString(); + + assertEquals(expected, queryString, false); + } + + @Test + // #1668 + void shouldMapNamesAndConvertValuesInCriteriaQueryForSubCriteriaWithDate() throws JSONException { + // use POJO properties and types in the query building + CriteriaQuery criteriaQuery = new CriteriaQuery( // + Criteria.or() + .subCriteria( Criteria.where("birthDate") // + .between(LocalDate.of(1989, 11, 9), LocalDate.of(1990, 11, 9)) ) // + .subCriteria( Criteria.where( "createdDate").is(383745721653L) ) // + ); + + // mapped field name and converted parameter + String expected = "{\n" + + " \"bool\" : {\n" + + " \"should\" : [\n" + + " {\n" + + " \"bool\" : {\n" + + " \"must\" : [\n" + + " {\n" + + " \"range\" : {\n" + + " \"birth-date\" : {\n" + + " \"from\" : \"09.11.1989\",\n" + + " \"to\" : \"09.11.1990\",\n" + + " \"include_lower\" : true,\n" + + " \"include_upper\" : true,\n" + + " \"boost\" : 1.0\n" + + " }\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"adjust_pure_negative\" : true,\n" + + " \"boost\" : 1.0\n" + + " }\n" + + " },\n" + + " {\n" + + " \"bool\" : {\n" + + " \"must\" : [\n" + + " {\n" + + " \"query_string\" : {\n" + + " \"query\" : \"383745721653\",\n" + + " \"fields\" : [\n" + + " \"created-date^1.0\"\n" + + " ],\n" + + " \"type\" : \"best_fields\",\n" + + " \"default_operator\" : \"and\",\n" + + " \"max_determinized_states\" : 10000,\n" + + " \"enable_position_increments\" : true,\n" + + " \"fuzziness\" : \"AUTO\",\n" + + " \"fuzzy_prefix_length\" : 0,\n" + + " \"fuzzy_max_expansions\" : 50,\n" + + " \"phrase_slop\" : 0,\n" + + " \"escape\" : false,\n" + + " \"auto_generate_synonyms_phrase_query\" : true,\n" + + " \"fuzzy_transpositions\" : true,\n" + + " \"boost\" : 1.0\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"adjust_pure_negative\" : true,\n" + + " \"boost\" : 1.0\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"adjust_pure_negative\" : true,\n" + + " \"boost\" : 1.0\n" + + " }\n" + + "}"; // + + mappingElasticsearchConverter.updateQuery(criteriaQuery, Person.class); + String queryString = new CriteriaQueryProcessor().createQuery(criteriaQuery.getCriteria()).toString(); + + assertEquals(expected, queryString, false); + } + @Test // DATAES-706 void shouldMapNamesAndValuesInSubCriteriaQuery() throws JSONException { @@ -197,6 +344,7 @@ public class CriteriaQueryMappingUnitTests { @Nullable @Id String id; @Nullable @Field(name = "first-name") String firstName; @Nullable @Field(name = "last-name") String lastName; + @Nullable @Field(name = "created-date", type = FieldType.Date, format = DateFormat.epoch_millis) Date createdDate; @Nullable @Field(name = "birth-date", type = FieldType.Date, format = DateFormat.custom, pattern = "dd.MM.uuuu") LocalDate birthDate; }