Skip to content

Commit

Permalink
Support deserialization for DynamicTemplates.
Browse files Browse the repository at this point in the history
Signed-off-by: Youssef Aouichaoui <[email protected]>
  • Loading branch information
youssef3wi committed Aug 20, 2024
1 parent eae9992 commit f052ae2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package org.springframework.data.elasticsearch.core.convert;

import static org.springframework.util.PatternMatchUtils.simpleMatch;
import static org.springframework.util.StringUtils.hasText;

import java.time.temporal.TemporalAccessor;
import java.util.*;
import java.util.Map.Entry;
Expand All @@ -38,8 +41,10 @@
import org.springframework.core.env.EnvironmentCapable;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.elasticsearch.annotations.DynamicTemplates;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.ScriptedField;
import org.springframework.data.elasticsearch.core.ResourceUtil;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.document.SearchDocument;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
Expand All @@ -53,6 +58,7 @@
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm;
import org.springframework.data.elasticsearch.core.query.SourceFilter;
import org.springframework.data.elasticsearch.support.DefaultStringObjectMap;
import org.springframework.data.mapping.InstanceCreatorMetadata;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.Parameter;
Expand Down Expand Up @@ -388,6 +394,40 @@ private <R> R readEntity(ElasticsearchPersistentEntity<?> entity, Map<String, Ob
targetEntity.getPropertyAccessor(result).setProperty(property, seqNoPrimaryTerm);
}
}

if (targetEntity.isAnnotationPresent(DynamicTemplates.class)) {
String mappingPath = targetEntity.getRequiredAnnotation(DynamicTemplates.class).mappingPath();
if (hasText(mappingPath)) {
String jsonString = ResourceUtil.readFileFromClasspath(mappingPath);
if (hasText(jsonString)) {
Object templates = new DefaultStringObjectMap<>().fromJson(jsonString).get("dynamic_templates");
if (templates instanceof List<?> array) {
for (Object node : array) {
if (node instanceof Map<?, ?> entry) {
Entry<?, ?> templateEntry = entry.entrySet().stream().findFirst().orElse(null);
if (templateEntry != null) {
ElasticsearchPersistentProperty property = targetEntity
.getPersistentPropertyWithFieldName((String) templateEntry.getKey());
if (property != null && property.isDynamicFieldMapping()) {
targetEntity.getPropertyAccessor(result).getProperty(property);
targetEntity.getPropertyAccessor(result).setProperty(property,
document.entrySet().stream().filter(fieldKey -> {
if (templateEntry.getValue() instanceof Map<?, ?> templateValue) {
if (templateValue.containsKey("match")) {
return simpleMatch((String) templateValue.get("match"), fieldKey.getKey());
}
}

return false;
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
}
}
}
}
}
}
}
}
}

if (source instanceof SearchDocument searchDocument) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.MappingContextBaseTests;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.StringQuery;
Expand Down Expand Up @@ -299,6 +300,8 @@ void shouldMapDynamicFields() {

// Then
assertThat(results.getTotalHits()).isEqualTo(1);
assertThat(results.getSearchHits()).first().extracting(SearchHit::getContent).extracting(doc -> doc.dynamicFields)
.isEqualTo(document.dynamicFields);
documentOperations.delete();
}

Expand Down Expand Up @@ -962,7 +965,7 @@ private static class DynamicFieldDocument {
@Nullable
@Id String id;

@Field(name = "*_str", dynamicTemplate = true) private Map<String, String> dynamicFields = new HashMap<>();
@Field(name = "_str", dynamicTemplate = true) private Map<String, String> dynamicFields = new HashMap<>();
}
// endregion
}

0 comments on commit f052ae2

Please sign in to comment.