Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 @@ -33,6 +33,7 @@
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;
Expand Down Expand Up @@ -1055,12 +1056,22 @@ private void updateCriteria(Criteria criteria, ElasticsearchPersistentEntity<?>
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) {
Expand All @@ -1077,7 +1088,7 @@ private void updateCriteria(Criteria criteria, ElasticsearchPersistentEntity<?>

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)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,34 @@ void shouldMapNamesAndValueInNestedEntitiesWithSubfields() throws JSONException

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
Expand Down Expand Up @@ -427,6 +455,13 @@ static class House {
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;
}
Expand Down