Skip to content

Commit 4782414

Browse files
authored
CriteriaQuery must use nested query only with properties of type nested.
Original Pull Request #1763 Closes #1761
1 parent ab73c68 commit 4782414

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.core.convert.support.DefaultConversionService;
3434
import org.springframework.core.convert.support.GenericConversionService;
3535
import org.springframework.data.convert.CustomConversions;
36+
import org.springframework.data.elasticsearch.annotations.FieldType;
3637
import org.springframework.data.elasticsearch.annotations.ScriptedField;
3738
import org.springframework.data.elasticsearch.core.document.Document;
3839
import org.springframework.data.elasticsearch.core.document.SearchDocument;
@@ -1055,12 +1056,22 @@ private void updateCriteria(Criteria criteria, ElasticsearchPersistentEntity<?>
10551056
ElasticsearchPersistentEntity<?> currentEntity = persistentEntity;
10561057
ElasticsearchPersistentProperty persistentProperty = null;
10571058
int propertyCount = 0;
1059+
boolean isNested = false;
1060+
10581061
for (int i = 0; i < fieldNames.length; i++) {
10591062
persistentProperty = currentEntity.getPersistentProperty(fieldNames[i]);
10601063

10611064
if (persistentProperty != null) {
10621065
propertyCount++;
10631066
fieldNames[i] = persistentProperty.getFieldName();
1067+
1068+
org.springframework.data.elasticsearch.annotations.Field fieldAnnotation = persistentProperty
1069+
.findAnnotation(org.springframework.data.elasticsearch.annotations.Field.class);
1070+
1071+
if (fieldAnnotation != null && fieldAnnotation.type() == FieldType.Nested) {
1072+
isNested = true;
1073+
}
1074+
10641075
try {
10651076
currentEntity = mappingContext.getPersistentEntity(persistentProperty.getActualType());
10661077
} catch (Exception e) {
@@ -1077,7 +1088,7 @@ private void updateCriteria(Criteria criteria, ElasticsearchPersistentEntity<?>
10771088

10781089
field.setName(String.join(".", fieldNames));
10791090

1080-
if (propertyCount > 1) {
1091+
if (propertyCount > 1 && isNested) {
10811092
List<String> propertyNames = Arrays.asList(fieldNames);
10821093
field.setPath(String.join(".", propertyNames.subList(0, propertyCount - 1)));
10831094
}

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

+35
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,34 @@ void shouldMapNamesAndValueInNestedEntitiesWithSubfields() throws JSONException
397397

398398
assertEquals(expected, queryString, false);
399399
}
400+
401+
@Test // #1761
402+
@DisplayName("should map names and value in object entities")
403+
void shouldMapNamesAndValueInObjectEntities() throws JSONException {
404+
405+
String expected = "{\n" + //
406+
" \"bool\": {\n" + //
407+
" \"must\": [\n" + //
408+
" {\n" + //
409+
" \"query_string\": {\n" + //
410+
" \"query\": \"03.10.1999\",\n" + //
411+
" \"fields\": [\n" + //
412+
" \"per-sons.birth-date^1.0\"\n" + //
413+
" ]\n" + //
414+
" }\n" + //
415+
" }\n" + //
416+
" ]\n" + //
417+
" }\n" + //
418+
"}\n"; //
419+
420+
CriteriaQuery criteriaQuery = new CriteriaQuery(
421+
new Criteria("persons.birthDate").is(LocalDate.of(1999, 10, 3))
422+
);
423+
mappingElasticsearchConverter.updateQuery(criteriaQuery, ObjectWithPerson.class);
424+
String queryString = new CriteriaQueryProcessor().createQuery(criteriaQuery.getCriteria()).toString();
425+
426+
assertEquals(expected, queryString, false);
427+
}
400428
// endregion
401429

402430
// region helper functions
@@ -427,6 +455,13 @@ static class House {
427455
List<Person> persons;
428456
}
429457

458+
static class ObjectWithPerson {
459+
@Nullable @Id String id;
460+
@Nullable
461+
@Field(name = "per-sons", type = FieldType.Object)
462+
List<Person> persons;
463+
}
464+
430465
static class GeoShapeEntity {
431466
@Nullable @Field(name = "geo-shape-field") GeoJson<?> geoShapeField;
432467
}

0 commit comments

Comments
 (0)