Skip to content

Commit 18c295c

Browse files
jbaierajoegallo
andcommitted
Correctly apply field path to JSON processor when adding contents to document root (elastic#135479)
Processor now correctly uses ingest document methods to obtain field data. --------- Co-authored-by: Joe Gallo <[email protected]>
1 parent 7aa3fec commit 18c295c

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

docs/changelog/135479.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 135479
2+
summary: Correctly apply field path to JSON processor when adding contents to document
3+
root
4+
area: Ingest Node
5+
type: bug
6+
issues: []

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ public static void apply(
148148
boolean strictJsonParsing
149149
) {
150150
Object value = apply(ctx.get(fieldName), allowDuplicateKeys, strictJsonParsing);
151+
mergeParsedJson(ctx, value, conflictStrategy);
152+
}
153+
154+
private static void mergeParsedJson(Map<String, Object> ctx, Object value, ConflictStrategy conflictStrategy) {
151155
if (value instanceof Map) {
152156
@SuppressWarnings("unchecked")
153157
Map<String, Object> map = (Map<String, Object>) value;
@@ -183,10 +187,11 @@ public static void recursiveMerge(Map<String, Object> target, Map<String, Object
183187

184188
@Override
185189
public IngestDocument execute(IngestDocument document) throws Exception {
190+
Object value = apply(document.getFieldValue(field, Object.class), allowDuplicateKeys, strictJsonParsing);
186191
if (addToRoot) {
187-
apply(document.getSourceAndMetadata(), field, allowDuplicateKeys, addToRootConflictStrategy, strictJsonParsing);
192+
mergeParsedJson(document.getSourceAndMetadata(), value, addToRootConflictStrategy);
188193
} else {
189-
document.setFieldValue(targetField, apply(document.getFieldValue(field, Object.class), allowDuplicateKeys, strictJsonParsing));
194+
document.setFieldValue(targetField, value);
190195
}
191196
return document;
192197
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
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.IngestPipelineTestUtils;
1516
import org.elasticsearch.ingest.RandomDocumentPicks;
1617
import org.elasticsearch.test.ESTestCase;
1718
import org.elasticsearch.xcontent.XContentBuilder;
@@ -166,6 +167,28 @@ public void testAddToRoot() throws Exception {
166167
assertEquals("see", sourceAndMetadata.get("c"));
167168
}
168169

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

0 commit comments

Comments
 (0)