Skip to content

Commit 3375f1d

Browse files
committed
Fix JsonProcessor nested field access
1 parent 78e4baa commit 3375f1d

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/JsonProcessor.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ public static void apply(
149149
boolean strictJsonParsing
150150
) {
151151
Object value = apply(ctx.get(fieldName), allowDuplicateKeys, strictJsonParsing);
152+
mergeParsedJson(ctx, value, conflictStrategy);
153+
}
154+
155+
private static void mergeParsedJson(Map<String, Object> ctx, Object value, ConflictStrategy conflictStrategy) {
152156
if (value instanceof Map) {
153157
@SuppressWarnings("unchecked")
154158
Map<String, Object> map = (Map<String, Object>) value;
@@ -185,7 +189,8 @@ public static void recursiveMerge(Map<String, Object> target, Map<String, Object
185189
@Override
186190
public IngestDocument execute(IngestDocument document) throws Exception {
187191
if (addToRoot) {
188-
apply(document.getSourceAndMetadata(), field, allowDuplicateKeys, addToRootConflictStrategy, strictJsonParsing);
192+
Object value = apply(document.getFieldValue(field, Object.class), allowDuplicateKeys, strictJsonParsing);
193+
mergeParsedJson(document.getSourceAndMetadata(), value, addToRootConflictStrategy);
189194
} else {
190195
document.setFieldValue(targetField, apply(document.getFieldValue(field, Object.class), allowDuplicateKeys, strictJsonParsing));
191196
}

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.elasticsearch.common.bytes.BytesReference;
1313
import org.elasticsearch.common.xcontent.XContentHelper;
1414
import org.elasticsearch.ingest.IngestDocument;
15+
import org.elasticsearch.ingest.IngestPipelineFieldAccessPattern;
16+
import org.elasticsearch.ingest.IngestPipelineTestUtils;
1517
import org.elasticsearch.ingest.RandomDocumentPicks;
1618
import org.elasticsearch.test.ESTestCase;
1719
import org.elasticsearch.xcontent.XContentBuilder;
@@ -166,6 +168,28 @@ public void testAddToRoot() throws Exception {
166168
assertEquals("see", sourceAndMetadata.get("c"));
167169
}
168170

171+
public void testAddToRootNestedField() throws Exception {
172+
String processorTag = randomAlphaOfLength(3);
173+
String randomTargetField = randomAlphaOfLength(2);
174+
JsonProcessor jsonProcessor = new JsonProcessor(processorTag, null, "a.b", randomTargetField, true, REPLACE, false);
175+
176+
String json = "{\"a\": 1, \"b\": 2}";
177+
Map<String, Object> subfield = new HashMap<>();
178+
subfield.put("b", json);
179+
180+
Map<String, Object> document = new HashMap<>();
181+
document.put("a", subfield);
182+
document.put("c", "see");
183+
184+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
185+
ingestDocument = IngestPipelineTestUtils.runWithRandomAccessPattern(ingestDocument, jsonProcessor);
186+
187+
Map<String, Object> sourceAndMetadata = ingestDocument.getSourceAndMetadata();
188+
assertEquals(1, sourceAndMetadata.get("a"));
189+
assertEquals(2, sourceAndMetadata.get("b"));
190+
assertEquals("see", sourceAndMetadata.get("c"));
191+
}
192+
169193
public void testDuplicateKeys() throws Exception {
170194
String processorTag = randomAlphaOfLength(3);
171195
JsonProcessor lenientJsonProcessor = new JsonProcessor(processorTag, null, "a", null, true, REPLACE, true);

0 commit comments

Comments
 (0)