From c158fd9be80334eb1d9d1aa43953d02cfdb954c5 Mon Sep 17 00:00:00 2001 From: James Baiera Date: Thu, 25 Sep 2025 19:14:56 -0400 Subject: [PATCH 1/2] Correctly apply field path to JSON processor when adding contents to document root (#135479) Processor now correctly uses ingest document methods to obtain field data. --------- Co-authored-by: Joe Gallo --- docs/changelog/135479.yaml | 6 +++++ .../ingest/common/JsonProcessor.java | 9 ++++++-- .../ingest/common/JsonProcessorTests.java | 23 +++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 docs/changelog/135479.yaml diff --git a/docs/changelog/135479.yaml b/docs/changelog/135479.yaml new file mode 100644 index 0000000000000..fa6f5ea620550 --- /dev/null +++ b/docs/changelog/135479.yaml @@ -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: [] diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/JsonProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/JsonProcessor.java index 47e7d5846d592..a3bd97e153edc 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/JsonProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/JsonProcessor.java @@ -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 ctx, Object value, ConflictStrategy conflictStrategy) { if (value instanceof Map) { @SuppressWarnings("unchecked") Map map = (Map) value; @@ -184,10 +188,11 @@ public static void recursiveMerge(Map target, Map subfield = new HashMap<>(); + subfield.put("b", json); + + Map document = new HashMap<>(); + document.put("a", subfield); + document.put("c", "see"); + + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); + ingestDocument = IngestPipelineTestUtils.runWithRandomAccessPattern(ingestDocument, jsonProcessor); + + Map 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); From 2e49da4f3906745757aaca2ab28b9939a74381d1 Mon Sep 17 00:00:00 2001 From: James Baiera Date: Fri, 26 Sep 2025 13:50:40 -0400 Subject: [PATCH 2/2] Remove missing test util --- .../org/elasticsearch/ingest/common/JsonProcessorTests.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorTests.java index f61d7d09c8b9c..3873174ec9c10 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorTests.java @@ -12,7 +12,6 @@ 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; @@ -181,7 +180,7 @@ public void testAddToRootNestedField() throws Exception { document.put("c", "see"); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); - ingestDocument = IngestPipelineTestUtils.runWithRandomAccessPattern(ingestDocument, jsonProcessor); + jsonProcessor.execute(ingestDocument); Map sourceAndMetadata = ingestDocument.getSourceAndMetadata(); assertEquals(1, sourceAndMetadata.get("a"));