mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-29 07:12:26 +00:00
fix: use scripted field name to populate entity.
Original Pull Request: #3023 Closes: #3022
This commit is contained in:
parent
5f297f1dc3
commit
944e7e81dd
@ -85,6 +85,7 @@ import org.springframework.util.ObjectUtils;
|
|||||||
* @author Anton Naydenov
|
* @author Anton Naydenov
|
||||||
* @author vdisk
|
* @author vdisk
|
||||||
* @author Junghoon Ban
|
* @author Junghoon Ban
|
||||||
|
* @author llosimura
|
||||||
* @since 3.2
|
* @since 3.2
|
||||||
*/
|
*/
|
||||||
public class MappingElasticsearchConverter
|
public class MappingElasticsearchConverter
|
||||||
@ -653,14 +654,15 @@ public class MappingElasticsearchConverter
|
|||||||
SearchDocument searchDocument) {
|
SearchDocument searchDocument) {
|
||||||
Map<String, List<Object>> fields = searchDocument.getFields();
|
Map<String, List<Object>> fields = searchDocument.getFields();
|
||||||
entity.doWithProperties((SimplePropertyHandler) property -> {
|
entity.doWithProperties((SimplePropertyHandler) property -> {
|
||||||
if (property.isAnnotationPresent(ScriptedField.class) && fields.containsKey(property.getName())) {
|
if (property.isAnnotationPresent(ScriptedField.class)) {
|
||||||
ScriptedField scriptedField = property.findAnnotation(ScriptedField.class);
|
ScriptedField scriptedField = property.findAnnotation(ScriptedField.class);
|
||||||
// noinspection ConstantConditions
|
// noinspection ConstantConditions
|
||||||
String name = scriptedField.name().isEmpty() ? property.getName() : scriptedField.name();
|
String name = scriptedField.name().isEmpty() ? property.getName() : scriptedField.name();
|
||||||
|
if (fields.containsKey(name)) {
|
||||||
Object value = searchDocument.getFieldValue(name);
|
Object value = searchDocument.getFieldValue(name);
|
||||||
|
|
||||||
entity.getPropertyAccessor(result).setProperty(property, value);
|
entity.getPropertyAccessor(result).setProperty(property, value);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,8 +49,10 @@ import org.springframework.data.elasticsearch.annotations.DateFormat;
|
|||||||
import org.springframework.data.elasticsearch.annotations.Field;
|
import org.springframework.data.elasticsearch.annotations.Field;
|
||||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||||
import org.springframework.data.elasticsearch.annotations.GeoPointField;
|
import org.springframework.data.elasticsearch.annotations.GeoPointField;
|
||||||
|
import org.springframework.data.elasticsearch.annotations.ScriptedField;
|
||||||
import org.springframework.data.elasticsearch.annotations.ValueConverter;
|
import org.springframework.data.elasticsearch.annotations.ValueConverter;
|
||||||
import org.springframework.data.elasticsearch.core.document.Document;
|
import org.springframework.data.elasticsearch.core.document.Document;
|
||||||
|
import org.springframework.data.elasticsearch.core.document.SearchDocumentAdapter;
|
||||||
import org.springframework.data.elasticsearch.core.geo.GeoJsonEntity;
|
import org.springframework.data.elasticsearch.core.geo.GeoJsonEntity;
|
||||||
import org.springframework.data.elasticsearch.core.geo.GeoJsonGeometryCollection;
|
import org.springframework.data.elasticsearch.core.geo.GeoJsonGeometryCollection;
|
||||||
import org.springframework.data.elasticsearch.core.geo.GeoJsonLineString;
|
import org.springframework.data.elasticsearch.core.geo.GeoJsonLineString;
|
||||||
@ -83,6 +85,7 @@ import org.springframework.util.Assert;
|
|||||||
* @author Konrad Kurdej
|
* @author Konrad Kurdej
|
||||||
* @author Roman Puchkovskiy
|
* @author Roman Puchkovskiy
|
||||||
* @author Sascha Woo
|
* @author Sascha Woo
|
||||||
|
* @author llosimura
|
||||||
*/
|
*/
|
||||||
public class MappingElasticsearchConverterUnitTests {
|
public class MappingElasticsearchConverterUnitTests {
|
||||||
|
|
||||||
@ -1800,6 +1803,68 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
assertThat(entity.getStringList()).containsExactly("foo");
|
assertThat(entity.getStringList()).containsExactly("foo");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldPopulateScriptedFields() {
|
||||||
|
SearchDocumentAdapter document = new SearchDocumentAdapter(Document.create(),
|
||||||
|
0.0f,
|
||||||
|
new Object[]{},
|
||||||
|
Map.of(
|
||||||
|
"scriptedField" , List.of("scriptedField"),
|
||||||
|
"custom-name-scripted-field" , List.of("custom-name-scripted-field")
|
||||||
|
),
|
||||||
|
emptyMap(),
|
||||||
|
emptyMap(),
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
// Create a SearchDocument instance
|
||||||
|
var entity = mappingElasticsearchConverter.read(ScriptedEntity.class, document);
|
||||||
|
assertThat(entity.customScriptedField).isEqualTo("custom-name-scripted-field");
|
||||||
|
assertThat(entity.scriptedField).isEqualTo("scriptedField");
|
||||||
|
}
|
||||||
|
|
||||||
|
static class ScriptedEntity {
|
||||||
|
@ScriptedField
|
||||||
|
private String scriptedField;
|
||||||
|
@ScriptedField(name = "custom-name-scripted-field") String customScriptedField;
|
||||||
|
|
||||||
|
ScriptedEntity() {
|
||||||
|
customScriptedField = "";
|
||||||
|
scriptedField = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getScriptedField() {
|
||||||
|
return scriptedField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScriptedField(String scriptedField) {
|
||||||
|
this.scriptedField = scriptedField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCustomScriptedField() {
|
||||||
|
return customScriptedField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCustomScriptedField(String customScriptedField) {
|
||||||
|
this.customScriptedField = customScriptedField;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
ScriptedEntity that = (ScriptedEntity) o;
|
||||||
|
return Objects.equals(scriptedField, that.scriptedField) && Objects.equals(customScriptedField, that.customScriptedField);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(scriptedField, customScriptedField);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test // #2280
|
@Test // #2280
|
||||||
@DisplayName("should read a String array into a List property immutable")
|
@DisplayName("should read a String array into a List property immutable")
|
||||||
void shouldReadAStringArrayIntoAListPropertyImmutable() {
|
void shouldReadAStringArrayIntoAListPropertyImmutable() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user