Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions docs/changelog/135479.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 135479
summary: Correctly apply field path to JSON processor when adding contents to document
root
area: Ingest Node
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public static void apply(
boolean strictJsonParsing
) {
Object value = apply(ctx.get(fieldName), allowDuplicateKeys, strictJsonParsing);
mergeParsedJson(ctx, value, conflictStrategy);
}

private static void mergeParsedJson(Map<String, Object> ctx, Object value, ConflictStrategy conflictStrategy) {
if (value instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) value;
Expand Down Expand Up @@ -183,10 +187,11 @@ public static void recursiveMerge(Map<String, Object> target, Map<String, Object

@Override
public IngestDocument execute(IngestDocument document) throws Exception {
Object value = apply(document.getFieldValue(field, Object.class), allowDuplicateKeys, strictJsonParsing);
if (addToRoot) {
apply(document.getSourceAndMetadata(), field, allowDuplicateKeys, addToRootConflictStrategy, strictJsonParsing);
mergeParsedJson(document.getSourceAndMetadata(), value, addToRootConflictStrategy);
} else {
document.setFieldValue(targetField, apply(document.getFieldValue(field, Object.class), allowDuplicateKeys, strictJsonParsing));
document.setFieldValue(targetField, value);
}
return document;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,28 @@ public void testAddToRoot() throws Exception {
assertEquals("see", sourceAndMetadata.get("c"));
}

public void testAddToRootNestedField() throws Exception {
String processorTag = randomAlphaOfLength(3);
String randomTargetField = randomAlphaOfLength(2);
JsonProcessor jsonProcessor = new JsonProcessor(processorTag, null, "a.b", randomTargetField, true, REPLACE, false);

String json = "{\"a\": 1, \"b\": 2}";
Map<String, Object> subfield = new HashMap<>();
subfield.put("b", json);

Map<String, Object> document = new HashMap<>();
document.put("a", subfield);
document.put("c", "see");

IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
jsonProcessor.execute(ingestDocument);

Map<String, Object> sourceAndMetadata = ingestDocument.getSourceAndMetadata();
assertEquals(1, sourceAndMetadata.get("a"));
assertEquals(2, sourceAndMetadata.get("b"));
assertEquals("see", sourceAndMetadata.get("c"));
}

public void testDuplicateKeys() throws Exception {
String processorTag = randomAlphaOfLength(3);
JsonProcessor lenientJsonProcessor = new JsonProcessor(processorTag, null, "a", null, true, REPLACE, true);
Expand Down