ingest: dot_expander_processor prevent null add/append to source document (#35106)
* don't allow null values to be added or appended to the source document if the field does not exist.
This commit is contained in:
parent
9ac7af6b3e
commit
9150a93b1c
|
@ -52,6 +52,7 @@ public final class DotExpanderProcessor extends AbstractProcessor {
|
|||
map = ingestDocument.getSourceAndMetadata();
|
||||
}
|
||||
|
||||
if (map.containsKey(field)) {
|
||||
if (ingestDocument.hasField(path)) {
|
||||
Object value = map.remove(field);
|
||||
ingestDocument.appendFieldValue(path, value);
|
||||
|
@ -75,6 +76,7 @@ public final class DotExpanderProcessor extends AbstractProcessor {
|
|||
Object value = map.remove(field);
|
||||
ingestDocument.setFieldValue(path, value);
|
||||
}
|
||||
}
|
||||
return ingestDocument;
|
||||
}
|
||||
|
||||
|
|
|
@ -143,4 +143,38 @@ public class DotExpanderProcessorTests extends ESTestCase {
|
|||
assertThat(document.getFieldValue("field.foo.bar.baz", String.class), equalTo("value"));
|
||||
}
|
||||
|
||||
|
||||
public void testEscapeFields_doNothingIfFieldNotInSourceDoc() throws Exception {
|
||||
//asking to expand a (literal) field that is not present in the source document
|
||||
Map<String, Object> source = new HashMap<>();
|
||||
source.put("foo.bar", "baz1");
|
||||
IngestDocument document = new IngestDocument(source, Collections.emptyMap());
|
||||
//abc.def does not exist in source, so don't mutate document
|
||||
DotExpanderProcessor processor = new DotExpanderProcessor("_tag", null, "abc.def");
|
||||
processor.execute(document);
|
||||
//hasField returns false since it requires the expanded form, which is not expanded since we did not ask for it to be
|
||||
assertFalse(document.hasField("foo.bar"));
|
||||
//nothing has changed
|
||||
assertEquals(document.getSourceAndMetadata().get("foo.bar"), "baz1");
|
||||
//abc.def is not found anywhere
|
||||
assertFalse(document.hasField("abc.def"));
|
||||
assertFalse(document.getSourceAndMetadata().containsKey("abc"));
|
||||
assertFalse(document.getSourceAndMetadata().containsKey("abc.def"));
|
||||
|
||||
//asking to expand a (literal) field that does not exist, but the nested field does exist
|
||||
source = new HashMap<>();
|
||||
Map<String, Object> inner = new HashMap<>();
|
||||
inner.put("bar", "baz1");
|
||||
source.put("foo", inner);
|
||||
document = new IngestDocument(source, Collections.emptyMap());
|
||||
//foo.bar, the literal value (as opposed to nested value) does not exist in source, so don't mutate document
|
||||
processor = new DotExpanderProcessor("_tag", null, "foo.bar");
|
||||
processor.execute(document);
|
||||
//hasField returns true because the nested/expanded form exists in the source document
|
||||
assertTrue(document.hasField("foo.bar"));
|
||||
//nothing changed
|
||||
assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1));
|
||||
assertThat(document.getFieldValue("foo.bar", String.class), equalTo("baz1"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue