Skip to content

Commit

Permalink
Make the extractor recursive to pick field per field
Browse files Browse the repository at this point in the history
  • Loading branch information
romulets committed Dec 12, 2024
1 parent a6aabd6 commit 414b951
Showing 1 changed file with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,36 @@ const entityMetadataExtractorProcessor = {
],
lang: 'painless',
source: /* java */ `
// Array, boolean, integer, ip, bytes, anything that is not a map, is a leaf field
void overwriteLeafFields(Object toBeOverwritten, Object toOverwrite) {
if (!(toBeOverwritten instanceof Map)) {
// We can't override anything that isn't a map
return;
}
if (toOverwrite instanceof Map) {
Map mapToBeOverwritten = (Map) toBeOverwritten;
for (entryToOverwrite in ((Map) toOverwrite).entrySet()) {
String keyToOverwrite = entryToOverwrite.getKey();
Object valueToOverwrite = entryToOverwrite.getValue();
if (valueToOverwrite instanceof Map) {
// If no initial value, we just put everything we have to overwrite
if (mapToBeOverwritten.get(keyToOverwrite) == null) {
mapToBeOverwritten.put(keyToOverwrite, valueToOverwrite)
} else {
overwriteLeafFields(mapToBeOverwritten.get(keyToOverwrite), valueToOverwrite);
}
} else {
mapToBeOverwritten.put(keyToOverwrite, valueToOverwrite)
}
}
}
}
Map merged = ctx;
def id = ctx.entity.id;
Expand All @@ -35,11 +65,7 @@ const entityMetadataExtractorProcessor = {
continue;
}
for (entry in ((Map)json)[id].entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
merged.put(key, value);
}
overwriteLeafFields(merged, ((Map)json)[id])
}
merged.entity.id = id;
Expand Down

0 comments on commit 414b951

Please sign in to comment.