mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-05-31 09:12:11 +00:00
Fix cutting of unknown properties in property paths for search.
Original Pull Request #3082 Closes #3081 Signed-off-by: Peter-Josef Meisch <pj.meisch@sothawo.com> (cherry picked from commit 1ae6301c2f7a4c659c59f0fe3b164114d3c45dc0) (cherry picked from commit 394fb7a831fae8425f319fedfc4d4b2dd108a9a8)
This commit is contained in:
parent
7b782c7c62
commit
f32cbb81f8
@ -1358,15 +1358,85 @@ public class MappingElasticsearchConverter
|
||||
return;
|
||||
}
|
||||
|
||||
String[] fieldNames = field.getName().split("\\.");
|
||||
var propertyNamesUpdate = updatePropertyNames(persistentEntity, field.getName());
|
||||
|
||||
var fieldNames = propertyNamesUpdate.names();
|
||||
field.setName(String.join(".", fieldNames));
|
||||
|
||||
if (propertyNamesUpdate.propertyCount() > 1 && propertyNamesUpdate.nestedProperty()) {
|
||||
List<String> propertyNames = Arrays.asList(fieldNames);
|
||||
field.setPath(String.join(".", propertyNames.subList(0, propertyNamesUpdate.propertyCount - 1)));
|
||||
}
|
||||
|
||||
if (propertyNamesUpdate.persistentProperty != null) {
|
||||
|
||||
if (propertyNamesUpdate.persistentProperty.hasPropertyValueConverter()) {
|
||||
PropertyValueConverter propertyValueConverter = Objects
|
||||
.requireNonNull(propertyNamesUpdate.persistentProperty.getPropertyValueConverter());
|
||||
criteria.getQueryCriteriaEntries().forEach(criteriaEntry -> {
|
||||
|
||||
if (criteriaEntry.getKey().hasValue()) {
|
||||
Object value = criteriaEntry.getValue();
|
||||
|
||||
if (value.getClass().isArray()) {
|
||||
Object[] objects = (Object[]) value;
|
||||
|
||||
for (int i = 0; i < objects.length; i++) {
|
||||
objects[i] = propertyValueConverter.write(objects[i]);
|
||||
}
|
||||
} else {
|
||||
criteriaEntry.setValue(propertyValueConverter.write(value));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
org.springframework.data.elasticsearch.annotations.Field fieldAnnotation = propertyNamesUpdate.persistentProperty
|
||||
.findAnnotation(org.springframework.data.elasticsearch.annotations.Field.class);
|
||||
|
||||
if (fieldAnnotation != null) {
|
||||
field.setFieldType(fieldAnnotation.type());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static record PropertyNamesUpdate(
|
||||
String[] names,
|
||||
Boolean nestedProperty,
|
||||
Integer propertyCount,
|
||||
ElasticsearchPersistentProperty persistentProperty) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String updateFieldNames(String propertyPath, ElasticsearchPersistentEntity<?> persistentEntity) {
|
||||
|
||||
Assert.notNull(propertyPath, "propertyPath must not be null");
|
||||
Assert.notNull(persistentEntity, "persistentEntity must not be null");
|
||||
|
||||
var propertyNamesUpdate = updatePropertyNames(persistentEntity, propertyPath);
|
||||
return String.join(".", propertyNamesUpdate.names());
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a propertyPath and replace the path values with the field names from a persistentEntity. path entries not
|
||||
* found in the entity are kept as they are.
|
||||
*
|
||||
* @return the eventually modified names, a flag if a nested entity was encountered the number of processed
|
||||
* propertiesand the last processed PersistentProperty.
|
||||
*/
|
||||
PropertyNamesUpdate updatePropertyNames(ElasticsearchPersistentEntity<?> persistentEntity, String propertyPath) {
|
||||
|
||||
String[] propertyNames = propertyPath.split("\\.");
|
||||
String[] fieldNames = Arrays.copyOf(propertyNames, propertyNames.length);
|
||||
|
||||
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]);
|
||||
for (int i = 0; i < propertyNames.length; i++) {
|
||||
persistentProperty = currentEntity.getPersistentProperty(propertyNames[i]);
|
||||
|
||||
if (persistentProperty != null) {
|
||||
propertyCount++;
|
||||
@ -1393,75 +1463,7 @@ public class MappingElasticsearchConverter
|
||||
}
|
||||
}
|
||||
|
||||
field.setName(String.join(".", fieldNames));
|
||||
|
||||
if (propertyCount > 1 && isNested) {
|
||||
List<String> propertyNames = Arrays.asList(fieldNames);
|
||||
field.setPath(String.join(".", propertyNames.subList(0, propertyCount - 1)));
|
||||
}
|
||||
|
||||
if (persistentProperty != null) {
|
||||
|
||||
if (persistentProperty.hasPropertyValueConverter()) {
|
||||
PropertyValueConverter propertyValueConverter = Objects
|
||||
.requireNonNull(persistentProperty.getPropertyValueConverter());
|
||||
criteria.getQueryCriteriaEntries().forEach(criteriaEntry -> {
|
||||
|
||||
if (criteriaEntry.getKey().hasValue()) {
|
||||
Object value = criteriaEntry.getValue();
|
||||
|
||||
if (value.getClass().isArray()) {
|
||||
Object[] objects = (Object[]) value;
|
||||
|
||||
for (int i = 0; i < objects.length; i++) {
|
||||
objects[i] = propertyValueConverter.write(objects[i]);
|
||||
}
|
||||
} else {
|
||||
criteriaEntry.setValue(propertyValueConverter.write(value));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
org.springframework.data.elasticsearch.annotations.Field fieldAnnotation = persistentProperty
|
||||
.findAnnotation(org.springframework.data.elasticsearch.annotations.Field.class);
|
||||
|
||||
if (fieldAnnotation != null) {
|
||||
field.setFieldType(fieldAnnotation.type());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String updateFieldNames(String propertyPath, ElasticsearchPersistentEntity<?> persistentEntity) {
|
||||
|
||||
Assert.notNull(propertyPath, "propertyPath must not be null");
|
||||
Assert.notNull(persistentEntity, "persistentEntity must not be null");
|
||||
|
||||
var properties = propertyPath.split("\\.", 2);
|
||||
|
||||
if (properties.length > 0) {
|
||||
var propertyName = properties[0];
|
||||
var fieldName = propertyToFieldName(persistentEntity, propertyName);
|
||||
|
||||
if (properties.length > 1) {
|
||||
var persistentProperty = persistentEntity.getPersistentProperty(propertyName);
|
||||
|
||||
if (persistentProperty != null) {
|
||||
ElasticsearchPersistentEntity<?> nestedPersistentEntity = mappingContext
|
||||
.getPersistentEntity(persistentProperty);
|
||||
if (nestedPersistentEntity != null) {
|
||||
return fieldName + '.' + updateFieldNames(properties[1], nestedPersistentEntity);
|
||||
} else {
|
||||
return fieldName;
|
||||
}
|
||||
}
|
||||
}
|
||||
return fieldName;
|
||||
} else {
|
||||
return propertyPath;
|
||||
}
|
||||
|
||||
return new PropertyNamesUpdate(fieldNames, isNested, propertyCount, persistentProperty);
|
||||
}
|
||||
// endregion
|
||||
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core.query;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.skyscreamer.jsonassert.JSONAssert.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
@ -27,6 +28,7 @@ import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
||||
@ -679,6 +681,45 @@ public abstract class ElasticsearchPartQueryIntegrationTests {
|
||||
assertEquals(expected, query, false);
|
||||
}
|
||||
|
||||
@Test // #3081
|
||||
@DisplayName("should build sort object with unknown field names")
|
||||
void shouldBuildSortObjectWithUnknownFieldNames() throws NoSuchMethodException, JSONException {
|
||||
|
||||
String methodName = "findByName";
|
||||
Class<?>[] parameterClasses = new Class[] { String.class, Sort.class };
|
||||
Object[] parameters = new Object[] { BOOK_TITLE, Sort.by("sortAuthor.sortName.raw") };
|
||||
|
||||
String query = getQueryString(methodName, parameterClasses, parameters);
|
||||
|
||||
String expected = """
|
||||
|
||||
{
|
||||
"query": {
|
||||
"bool": {
|
||||
"must": [
|
||||
{
|
||||
"query_string": {
|
||||
"query": "Title",
|
||||
"fields": [
|
||||
"name"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"sort": [
|
||||
{
|
||||
"sort_author.sort_name.raw": {
|
||||
"order": "asc"
|
||||
}
|
||||
}
|
||||
]
|
||||
}""";
|
||||
|
||||
assertEquals(expected, query, false);
|
||||
}
|
||||
|
||||
private String getQueryString(String methodName, Class<?>[] parameterClasses, Object[] parameters)
|
||||
throws NoSuchMethodException {
|
||||
|
||||
@ -768,6 +809,7 @@ public abstract class ElasticsearchPartQueryIntegrationTests {
|
||||
|
||||
List<Book> findByNameOrderBySortAuthor_SortName(String name);
|
||||
|
||||
List<Book> findByName(String name, Sort sort);
|
||||
}
|
||||
|
||||
public static class Book {
|
||||
|
Loading…
x
Reference in New Issue
Block a user