|
15 | 15 | */
|
16 | 16 | package org.springframework.data.elasticsearch.core.convert;
|
17 | 17 |
|
| 18 | +import static org.springframework.util.PatternMatchUtils.simpleMatch; |
| 19 | +import static org.springframework.util.StringUtils.hasText; |
| 20 | + |
18 | 21 | import java.time.temporal.TemporalAccessor;
|
19 | 22 | import java.util.*;
|
20 | 23 | import java.util.Map.Entry;
|
|
38 | 41 | import org.springframework.core.env.EnvironmentCapable;
|
39 | 42 | import org.springframework.core.env.StandardEnvironment;
|
40 | 43 | import org.springframework.data.convert.CustomConversions;
|
| 44 | +import org.springframework.data.elasticsearch.annotations.DynamicTemplates; |
41 | 45 | import org.springframework.data.elasticsearch.annotations.FieldType;
|
42 | 46 | import org.springframework.data.elasticsearch.annotations.ScriptedField;
|
| 47 | +import org.springframework.data.elasticsearch.core.ResourceUtil; |
43 | 48 | import org.springframework.data.elasticsearch.core.document.Document;
|
44 | 49 | import org.springframework.data.elasticsearch.core.document.SearchDocument;
|
45 | 50 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
|
53 | 58 | import org.springframework.data.elasticsearch.core.query.Query;
|
54 | 59 | import org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm;
|
55 | 60 | import org.springframework.data.elasticsearch.core.query.SourceFilter;
|
| 61 | +import org.springframework.data.elasticsearch.support.DefaultStringObjectMap; |
56 | 62 | import org.springframework.data.mapping.InstanceCreatorMetadata;
|
57 | 63 | import org.springframework.data.mapping.MappingException;
|
58 | 64 | import org.springframework.data.mapping.Parameter;
|
@@ -388,6 +394,40 @@ private <R> R readEntity(ElasticsearchPersistentEntity<?> entity, Map<String, Ob
|
388 | 394 | targetEntity.getPropertyAccessor(result).setProperty(property, seqNoPrimaryTerm);
|
389 | 395 | }
|
390 | 396 | }
|
| 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 | + } |
391 | 431 | }
|
392 | 432 |
|
393 | 433 | if (source instanceof SearchDocument searchDocument) {
|
|
0 commit comments