Skip to content

Use the field name specified on the ScriptedField to populate it. #3023

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
merged 1 commit into from
Dec 14, 2024
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 @@ -85,6 +85,7 @@
* @author Anton Naydenov
* @author vdisk
* @author Junghoon Ban
* @author llosimura
* @since 3.2
*/
public class MappingElasticsearchConverter
Expand Down Expand Up @@ -653,13 +654,14 @@ private <T> void populateScriptFields(ElasticsearchPersistentEntity<?> entity, T
SearchDocument searchDocument) {
Map<String, List<Object>> fields = searchDocument.getFields();
entity.doWithProperties((SimplePropertyHandler) property -> {
if (property.isAnnotationPresent(ScriptedField.class) && fields.containsKey(property.getName())) {
if (property.isAnnotationPresent(ScriptedField.class)) {
ScriptedField scriptedField = property.findAnnotation(ScriptedField.class);
// noinspection ConstantConditions
String name = scriptedField.name().isEmpty() ? property.getName() : scriptedField.name();
Object value = searchDocument.getFieldValue(name);

entity.getPropertyAccessor(result).setProperty(property, value);
if (fields.containsKey(name)) {
Object value = searchDocument.getFieldValue(name);
entity.getPropertyAccessor(result).setProperty(property, value);
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
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.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.GeoJsonGeometryCollection;
import org.springframework.data.elasticsearch.core.geo.GeoJsonLineString;
Expand Down Expand Up @@ -83,6 +85,7 @@
* @author Konrad Kurdej
* @author Roman Puchkovskiy
* @author Sascha Woo
* @author llosimura
*/
public class MappingElasticsearchConverterUnitTests {

Expand Down Expand Up @@ -1800,6 +1803,68 @@ void shouldReadASingleStringIntoAListPropertyImmutable() {
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
@DisplayName("should read a String array into a List property immutable")
void shouldReadAStringArrayIntoAListPropertyImmutable() {
Expand Down