Skip to content

Commit f198fb4

Browse files
committed
Extract method to populate fields [dynamic].
Signed-off-by: Youssef Aouichaoui <[email protected]>
1 parent 0f0ac1b commit f198fb4

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

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

+35-31
Original file line numberDiff line numberDiff line change
@@ -396,37 +396,7 @@ private <R> R readEntity(ElasticsearchPersistentEntity<?> entity, Map<String, Ob
396396
}
397397

398398
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-
}
399+
populateFieldsUsingDynamicTemplates(targetEntity, result, document);
430400
}
431401
}
432402

@@ -704,6 +674,40 @@ private <T> void populateScriptFields(ElasticsearchPersistentEntity<?> entity, T
704674
});
705675
}
706676

677+
private <R> void populateFieldsUsingDynamicTemplates(ElasticsearchPersistentEntity<?> targetEntity, R result, Document document) {
678+
String mappingPath = targetEntity.getRequiredAnnotation(DynamicTemplates.class).mappingPath();
679+
if (hasText(mappingPath)) {
680+
String jsonString = ResourceUtil.readFileFromClasspath(mappingPath);
681+
if (hasText(jsonString)) {
682+
Object templates = new DefaultStringObjectMap<>().fromJson(jsonString).get("dynamic_templates");
683+
if (templates instanceof List<?> array) {
684+
for (Object node : array) {
685+
if (node instanceof Map<?, ?> entry) {
686+
Entry<?, ?> templateEntry = entry.entrySet().stream().findFirst().orElse(null);
687+
if (templateEntry != null) {
688+
ElasticsearchPersistentProperty property = targetEntity
689+
.getPersistentPropertyWithFieldName((String) templateEntry.getKey());
690+
if (property != null && property.isDynamicFieldMapping()) {
691+
targetEntity.getPropertyAccessor(result).getProperty(property);
692+
targetEntity.getPropertyAccessor(result).setProperty(property,
693+
document.entrySet().stream().filter(fieldKey -> {
694+
if (templateEntry.getValue() instanceof Map<?, ?> templateValue) {
695+
if (templateValue.containsKey("match")) {
696+
return simpleMatch((String) templateValue.get("match"), fieldKey.getKey());
697+
}
698+
}
699+
700+
return false;
701+
}).collect(Collectors.toMap(Entry::getKey, Entry::getValue)));
702+
}
703+
}
704+
}
705+
}
706+
}
707+
}
708+
}
709+
}
710+
707711
/**
708712
* Compute the type to use by checking the given entity against the store type;
709713
*/

0 commit comments

Comments
 (0)