Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
* @author Roman Puchkovskiy
* @author Konrad Kurdej
* @author Subhobrata Dey
* @author Marc Vanbrabant
* @since 3.2
*/
public class MappingElasticsearchConverter
Expand Down Expand Up @@ -805,6 +806,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);
}
}
}
}

Expand Down Expand Up @@ -843,12 +849,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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
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;
Expand Down Expand Up @@ -109,6 +107,155 @@ void shouldMapNamesAndConvertValuesInCriteriaQuery() throws JSONException {
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 {
Expand Down Expand Up @@ -197,6 +344,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;
}
Expand Down