Skip to content

Support for DynamicTemplates. #2969

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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Extract method to populate fields [dynamic].
Signed-off-by: Youssef Aouichaoui <[email protected]>
  • Loading branch information
youssef3wi committed Sep 6, 2024
commit cdf3fd23c9efc562f793387eba87642f2d271a9f
Original file line number Diff line number Diff line change
Expand Up @@ -396,37 +396,7 @@ private <R> R readEntity(ElasticsearchPersistentEntity<?> entity, Map<String, Ob
}

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)));
}
}
}
}
}
}
}
populateFieldsUsingDynamicTemplates(targetEntity, result, document);
}
}

Expand Down Expand Up @@ -704,6 +674,40 @@ private <T> void populateScriptFields(ElasticsearchPersistentEntity<?> entity, T
});
}

private <R> void populateFieldsUsingDynamicTemplates(ElasticsearchPersistentEntity<?> targetEntity, R result, Document document) {
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(Entry::getKey, Entry::getValue)));
}
}
}
}
}
}
}
}

/**
* Compute the type to use by checking the given entity against the store type;
*/
Expand Down