Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -149,6 +149,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 @@ -185,7 +189,8 @@ public static void recursiveMerge(Map<String, Object> target, Map<String, Object
@Override
public IngestDocument execute(IngestDocument document) throws Exception {
if (addToRoot) {
apply(document.getSourceAndMetadata(), field, allowDuplicateKeys, addToRootConflictStrategy, strictJsonParsing);
Object value = apply(document.getFieldValue(field, Object.class), allowDuplicateKeys, strictJsonParsing);
mergeParsedJson(document.getSourceAndMetadata(), value, addToRootConflictStrategy);
} else {
document.setFieldValue(targetField, apply(document.getFieldValue(field, Object.class), allowDuplicateKeys, strictJsonParsing));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.IngestPipelineTestUtils;
import org.elasticsearch.ingest.RandomDocumentPicks;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xcontent.XContentBuilder;
Expand Down Expand Up @@ -166,6 +167,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);
ingestDocument = IngestPipelineTestUtils.runWithRandomAccessPattern(ingestDocument, jsonProcessor);

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