Skip to content

Commit ab203f2

Browse files
jbaierajoegallo
andauthored
[8.18] Correctly apply field path to JSON processor when adding contents to document root (#135479) (#135497)
* 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 <[email protected]> * Remove missing test util --------- Co-authored-by: Joe Gallo <[email protected]>
1 parent 3b96040 commit ab203f2

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,28 @@ public void testAddToRoot() throws Exception {
166166
assertEquals("see", sourceAndMetadata.get("c"));
167167
}
168168

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

0 commit comments

Comments
 (0)