From 42fda1d7154688292cac1047bd100b44b18cd7a8 Mon Sep 17 00:00:00 2001 From: Marc Vanbrabant Date: Wed, 27 Jan 2021 16:13:39 +0100 Subject: [PATCH 1/3] #1668 Fix subCriteria's not being updated --- .../MappingElasticsearchConverter.java | 11 +- .../core/CriteriaQueryMappingUnitTests.java | 158 +++++++++++++++++- 2 files changed, 160 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..efc99c7c9 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 @@ -805,6 +805,11 @@ public void updateCriteriaQuery(CriteriaQuery criteriaQuery, Class domainClas 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 +848,6 @@ private void updateCriteria(Criteria criteria, ElasticsearchPersistentEntity 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..8d46277aa 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/CriteriaQueryMappingUnitTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/CriteriaQueryMappingUnitTests.java @@ -18,10 +18,9 @@ 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.apache.commons.lang3.time.DateUtils; import org.json.JSONException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -109,6 +108,158 @@ void shouldMapNamesAndConvertValuesInCriteriaQuery() throws JSONException { assertEquals(expected, queryString, false); } + @Test + // DATAES-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 + // DATAES-1668 + void shouldMapNamesAndConvertValuesInCriteriaQueryForSubCriteriaWithDate() throws JSONException { + Calendar calendar = Calendar.getInstance(); + //calendar.set(1982, Calendar.FEBRUARY, 28, 13, 2, 1 ); + calendar.setTimeInMillis(383745721653L); + // 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(calendar.getTime()) ) // + ); + + // 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 +348,7 @@ static class Person { @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; } From e771016ccf5fd99a2ce047bd209d5c65b68b9368 Mon Sep 17 00:00:00 2001 From: Marc Vanbrabant Date: Thu, 28 Jan 2021 23:15:01 +0100 Subject: [PATCH 2/3] PR comments for #1668 --- .../core/CriteriaQueryMappingUnitTests.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) 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 8d46277aa..bce262a9b 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/CriteriaQueryMappingUnitTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/CriteriaQueryMappingUnitTests.java @@ -20,7 +20,6 @@ import java.time.LocalDate; import java.util.*; -import org.apache.commons.lang3.time.DateUtils; import org.json.JSONException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -109,7 +108,7 @@ void shouldMapNamesAndConvertValuesInCriteriaQuery() throws JSONException { } @Test - // DATAES-1668 + // #1668 void shouldMapNamesAndConvertValuesInCriteriaQueryForSubCriteria() throws JSONException { // use POJO properties and types in the query building @@ -184,17 +183,14 @@ void shouldMapNamesAndConvertValuesInCriteriaQueryForSubCriteria() throws JSONEx } @Test - // DATAES-1668 + // #1668 void shouldMapNamesAndConvertValuesInCriteriaQueryForSubCriteriaWithDate() throws JSONException { - Calendar calendar = Calendar.getInstance(); - //calendar.set(1982, Calendar.FEBRUARY, 28, 13, 2, 1 ); - calendar.setTimeInMillis(383745721653L); // 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(calendar.getTime()) ) // + .subCriteria( Criteria.where( "createdDate").is(383745721653L) ) // ); // mapped field name and converted parameter From f69de74f0c850d4664b239b97c29f9b8b7158278 Mon Sep 17 00:00:00 2001 From: Marc Vanbrabant Date: Thu, 28 Jan 2021 23:22:33 +0100 Subject: [PATCH 3/3] PR comments for #1668 --- .../core/convert/MappingElasticsearchConverter.java | 1 + 1 file changed, 1 insertion(+) 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 efc99c7c9..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 @@ * @author Roman Puchkovskiy * @author Konrad Kurdej * @author Subhobrata Dey + * @author Marc Vanbrabant * @since 3.2 */ public class MappingElasticsearchConverter