CriteriaQuery must use nested query only with properties of type nested.

Original Pull Request #1763
Closes #1761
This commit is contained in:
Peter-Josef Meisch 2021-04-06 20:50:24 +02:00 committed by GitHub
parent ab73c68ca9
commit 4782414596
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 1 deletions

View File

@ -33,6 +33,7 @@ import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.ScriptedField;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.document.SearchDocument;
@ -1055,12 +1056,22 @@ public class MappingElasticsearchConverter
ElasticsearchPersistentEntity<?> currentEntity = persistentEntity;
ElasticsearchPersistentProperty persistentProperty = null;
int propertyCount = 0;
boolean isNested = false;
for (int i = 0; i < fieldNames.length; i++) {
persistentProperty = currentEntity.getPersistentProperty(fieldNames[i]);
if (persistentProperty != null) {
propertyCount++;
fieldNames[i] = persistentProperty.getFieldName();
org.springframework.data.elasticsearch.annotations.Field fieldAnnotation = persistentProperty
.findAnnotation(org.springframework.data.elasticsearch.annotations.Field.class);
if (fieldAnnotation != null && fieldAnnotation.type() == FieldType.Nested) {
isNested = true;
}
try {
currentEntity = mappingContext.getPersistentEntity(persistentProperty.getActualType());
} catch (Exception e) {
@ -1077,7 +1088,7 @@ public class MappingElasticsearchConverter
field.setName(String.join(".", fieldNames));
if (propertyCount > 1) {
if (propertyCount > 1 && isNested) {
List<String> propertyNames = Arrays.asList(fieldNames);
field.setPath(String.join(".", propertyNames.subList(0, propertyCount - 1)));
}

View File

@ -397,6 +397,34 @@ public class CriteriaQueryMappingUnitTests {
assertEquals(expected, queryString, false);
}
@Test // #1761
@DisplayName("should map names and value in object entities")
void shouldMapNamesAndValueInObjectEntities() throws JSONException {
String expected = "{\n" + //
" \"bool\": {\n" + //
" \"must\": [\n" + //
" {\n" + //
" \"query_string\": {\n" + //
" \"query\": \"03.10.1999\",\n" + //
" \"fields\": [\n" + //
" \"per-sons.birth-date^1.0\"\n" + //
" ]\n" + //
" }\n" + //
" }\n" + //
" ]\n" + //
" }\n" + //
"}\n"; //
CriteriaQuery criteriaQuery = new CriteriaQuery(
new Criteria("persons.birthDate").is(LocalDate.of(1999, 10, 3))
);
mappingElasticsearchConverter.updateQuery(criteriaQuery, ObjectWithPerson.class);
String queryString = new CriteriaQueryProcessor().createQuery(criteriaQuery.getCriteria()).toString();
assertEquals(expected, queryString, false);
}
// endregion
// region helper functions
@ -427,6 +455,13 @@ public class CriteriaQueryMappingUnitTests {
List<Person> persons;
}
static class ObjectWithPerson {
@Nullable @Id String id;
@Nullable
@Field(name = "per-sons", type = FieldType.Object)
List<Person> persons;
}
static class GeoShapeEntity {
@Nullable @Field(name = "geo-shape-field") GeoJson<?> geoShapeField;
}