Skip to content

Commit 6b5a728

Browse files
committed
Support deserialization for DynamicTemplates.
Signed-off-by: Youssef Aouichaoui <[email protected]>
1 parent 1456611 commit 6b5a728

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Diff for: src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java

+40
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core.convert;
1717

18+
import static org.springframework.util.PatternMatchUtils.simpleMatch;
19+
import static org.springframework.util.StringUtils.hasText;
20+
1821
import java.time.temporal.TemporalAccessor;
1922
import java.util.*;
2023
import java.util.Map.Entry;
@@ -38,8 +41,10 @@
3841
import org.springframework.core.env.EnvironmentCapable;
3942
import org.springframework.core.env.StandardEnvironment;
4043
import org.springframework.data.convert.CustomConversions;
44+
import org.springframework.data.elasticsearch.annotations.DynamicTemplates;
4145
import org.springframework.data.elasticsearch.annotations.FieldType;
4246
import org.springframework.data.elasticsearch.annotations.ScriptedField;
47+
import org.springframework.data.elasticsearch.core.ResourceUtil;
4348
import org.springframework.data.elasticsearch.core.document.Document;
4449
import org.springframework.data.elasticsearch.core.document.SearchDocument;
4550
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
@@ -53,6 +58,7 @@
5358
import org.springframework.data.elasticsearch.core.query.Query;
5459
import org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm;
5560
import org.springframework.data.elasticsearch.core.query.SourceFilter;
61+
import org.springframework.data.elasticsearch.support.DefaultStringObjectMap;
5662
import org.springframework.data.mapping.InstanceCreatorMetadata;
5763
import org.springframework.data.mapping.MappingException;
5864
import org.springframework.data.mapping.Parameter;
@@ -388,6 +394,40 @@ private <R> R readEntity(ElasticsearchPersistentEntity<?> entity, Map<String, Ob
388394
targetEntity.getPropertyAccessor(result).setProperty(property, seqNoPrimaryTerm);
389395
}
390396
}
397+
398+
if (targetEntity.isAnnotationPresent(DynamicTemplates.class)) {
399+
String mappingPath = targetEntity.getRequiredAnnotation(DynamicTemplates.class).mappingPath();
400+
if (hasText(mappingPath)) {
401+
String jsonString = ResourceUtil.readFileFromClasspath(mappingPath);
402+
if (hasText(jsonString)) {
403+
Object templates = new DefaultStringObjectMap<>().fromJson(jsonString).get("dynamic_templates");
404+
if (templates instanceof List<?> array) {
405+
for (Object node : array) {
406+
if (node instanceof Map<?, ?> entry) {
407+
Entry<?, ?> templateEntry = entry.entrySet().stream().findFirst().orElse(null);
408+
if (templateEntry != null) {
409+
ElasticsearchPersistentProperty property = targetEntity
410+
.getPersistentPropertyWithFieldName((String) templateEntry.getKey());
411+
if (property != null && property.isDynamicFieldMapping()) {
412+
targetEntity.getPropertyAccessor(result).getProperty(property);
413+
targetEntity.getPropertyAccessor(result).setProperty(property,
414+
document.entrySet().stream().filter(fieldKey -> {
415+
if (templateEntry.getValue() instanceof Map<?, ?> templateValue) {
416+
if (templateValue.containsKey("match")) {
417+
return simpleMatch((String) templateValue.get("match"), fieldKey.getKey());
418+
}
419+
}
420+
421+
return false;
422+
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
423+
}
424+
}
425+
}
426+
}
427+
}
428+
}
429+
}
430+
}
391431
}
392432

393433
if (source instanceof SearchDocument searchDocument) {

Diff for: src/test/java/org/springframework/data/elasticsearch/core/index/MappingBuilderIntegrationTests.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
4343
import org.springframework.data.elasticsearch.core.IndexOperations;
4444
import org.springframework.data.elasticsearch.core.MappingContextBaseTests;
45+
import org.springframework.data.elasticsearch.core.SearchHit;
4546
import org.springframework.data.elasticsearch.core.SearchHits;
4647
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
4748
import org.springframework.data.elasticsearch.core.query.StringQuery;
@@ -299,6 +300,8 @@ void shouldMapDynamicFields() {
299300

300301
// Then
301302
assertThat(results.getTotalHits()).isEqualTo(1);
303+
assertThat(results.getSearchHits()).first().extracting(SearchHit::getContent).extracting(doc -> doc.dynamicFields)
304+
.isEqualTo(document.dynamicFields);
302305
documentOperations.delete();
303306
}
304307

@@ -962,7 +965,7 @@ private static class DynamicFieldDocument {
962965
@Nullable
963966
@Id String id;
964967

965-
@Field(name = "*_str", dynamicTemplate = true) private Map<String, String> dynamicFields = new HashMap<>();
968+
@Field(name = "_str", dynamicTemplate = true) private Map<String, String> dynamicFields = new HashMap<>();
966969
}
967970
// endregion
968971
}

0 commit comments

Comments
 (0)