mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-01 09:42: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.DefaultConversionService;
|
||||||
import org.springframework.core.convert.support.GenericConversionService;
|
import org.springframework.core.convert.support.GenericConversionService;
|
||||||
import org.springframework.data.convert.CustomConversions;
|
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.annotations.ScriptedField;
|
||||||
import org.springframework.data.elasticsearch.core.document.Document;
|
import org.springframework.data.elasticsearch.core.document.Document;
|
||||||
import org.springframework.data.elasticsearch.core.document.SearchDocument;
|
import org.springframework.data.elasticsearch.core.document.SearchDocument;
|
||||||
@ -1055,12 +1056,22 @@ public class MappingElasticsearchConverter
|
|||||||
ElasticsearchPersistentEntity<?> currentEntity = persistentEntity;
|
ElasticsearchPersistentEntity<?> currentEntity = persistentEntity;
|
||||||
ElasticsearchPersistentProperty persistentProperty = null;
|
ElasticsearchPersistentProperty persistentProperty = null;
|
||||||
int propertyCount = 0;
|
int propertyCount = 0;
|
||||||
|
boolean isNested = false;
|
||||||
|
|
||||||
for (int i = 0; i < fieldNames.length; i++) {
|
for (int i = 0; i < fieldNames.length; i++) {
|
||||||
persistentProperty = currentEntity.getPersistentProperty(fieldNames[i]);
|
persistentProperty = currentEntity.getPersistentProperty(fieldNames[i]);
|
||||||
|
|
||||||
if (persistentProperty != null) {
|
if (persistentProperty != null) {
|
||||||
propertyCount++;
|
propertyCount++;
|
||||||
fieldNames[i] = persistentProperty.getFieldName();
|
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 {
|
try {
|
||||||
currentEntity = mappingContext.getPersistentEntity(persistentProperty.getActualType());
|
currentEntity = mappingContext.getPersistentEntity(persistentProperty.getActualType());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -1077,7 +1088,7 @@ public class MappingElasticsearchConverter
|
|||||||
|
|
||||||
field.setName(String.join(".", fieldNames));
|
field.setName(String.join(".", fieldNames));
|
||||||
|
|
||||||
if (propertyCount > 1) {
|
if (propertyCount > 1 && isNested) {
|
||||||
List<String> propertyNames = Arrays.asList(fieldNames);
|
List<String> propertyNames = Arrays.asList(fieldNames);
|
||||||
field.setPath(String.join(".", propertyNames.subList(0, propertyCount - 1)));
|
field.setPath(String.join(".", propertyNames.subList(0, propertyCount - 1)));
|
||||||
}
|
}
|
||||||
|
@ -397,6 +397,34 @@ public class CriteriaQueryMappingUnitTests {
|
|||||||
|
|
||||||
assertEquals(expected, queryString, false);
|
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
|
// endregion
|
||||||
|
|
||||||
// region helper functions
|
// region helper functions
|
||||||
@ -427,6 +455,13 @@ public class CriteriaQueryMappingUnitTests {
|
|||||||
List<Person> persons;
|
List<Person> persons;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class ObjectWithPerson {
|
||||||
|
@Nullable @Id String id;
|
||||||
|
@Nullable
|
||||||
|
@Field(name = "per-sons", type = FieldType.Object)
|
||||||
|
List<Person> persons;
|
||||||
|
}
|
||||||
|
|
||||||
static class GeoShapeEntity {
|
static class GeoShapeEntity {
|
||||||
@Nullable @Field(name = "geo-shape-field") GeoJson<?> geoShapeField;
|
@Nullable @Field(name = "geo-shape-field") GeoJson<?> geoShapeField;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user