Skip to content

Commit 01d8f2b

Browse files
jbaierajoegallo
andauthored
[9.1] Correctly apply field path to JSON processor when adding contents to document root (elastic#135479) (elastic#135496)
* 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]> * Remove missing test util --------- Co-authored-by: Joe Gallo <[email protected]>
1 parent 9cdfe5d commit 01d8f2b

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
@@ -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;
@@ -184,10 +188,11 @@ public static void recursiveMerge(Map<String, Object> target, Map<String, Object
184188

185189
@Override
186190
public IngestDocument execute(IngestDocument document) throws Exception {
191+
Object value = apply(document.getFieldValue(field, Object.class), allowDuplicateKeys, strictJsonParsing);
187192
if (addToRoot) {
188-
apply(document.getSourceAndMetadata(), field, allowDuplicateKeys, addToRootConflictStrategy, strictJsonParsing);
193+
mergeParsedJson(document.getSourceAndMetadata(), value, addToRootConflictStrategy);
189194
} else {
190-
document.setFieldValue(targetField, apply(document.getFieldValue(field, Object.class), allowDuplicateKeys, strictJsonParsing));
195+
document.setFieldValue(targetField, value);
191196
}
192197
return document;
193198
}

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)