mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-05-30 16:52:11 +00:00
CriteriaQuery must use nested query only with properties of type nested.
Original Pull Request #1763 Closes #1761
This commit is contained in:
parent
ab73c68ca9
commit
4782414596
@ -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)));
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user