Skip to content

Commit c9573f6

Browse files
Ingest: Add ignore_missing option to RemoveProc (#31693) (#31892)
Added `ignore_missing` setting to the RemoveProcessor to fix #23086
1 parent d061a9a commit c9573f6

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

docs/reference/ingest/ingest-node.asciidoc

+3-2
Original file line numberDiff line numberDiff line change
@@ -1767,8 +1767,9 @@ Removes existing fields. If one field doesn't exist, an exception will be thrown
17671767
.Remove Options
17681768
[options="header"]
17691769
|======
1770-
| Name | Required | Default | Description
1771-
| `field` | yes | - | Fields to be removed
1770+
| Name | Required | Default | Description
1771+
| `field` | yes | - | Fields to be removed
1772+
| `ignore_missing` | no | `false` | If `true` and `field` does not exist or is `null`, the processor quietly exits without modifying the document
17721773
|======
17731774

17741775
Here is an example to remove a single field:

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ public final class RemoveProcessor extends AbstractProcessor {
3939
public static final String TYPE = "remove";
4040

4141
private final List<TemplateScript.Factory> fields;
42+
private final boolean ignoreMissing;
4243

43-
RemoveProcessor(String tag, List<TemplateScript.Factory> fields) {
44+
RemoveProcessor(String tag, List<TemplateScript.Factory> fields, boolean ignoreMissing) {
4445
super(tag);
4546
this.fields = new ArrayList<>(fields);
47+
this.ignoreMissing = ignoreMissing;
4648
}
4749

4850
public List<TemplateScript.Factory> getFields() {
@@ -51,7 +53,16 @@ public List<TemplateScript.Factory> getFields() {
5153

5254
@Override
5355
public void execute(IngestDocument document) {
54-
fields.forEach(document::removeField);
56+
if (ignoreMissing) {
57+
fields.forEach(field -> {
58+
String path = document.renderTemplate(field);
59+
if (document.hasField(path)) {
60+
document.removeField(path);
61+
}
62+
});
63+
} else {
64+
fields.forEach(document::removeField);
65+
}
5566
}
5667

5768
@Override
@@ -81,7 +92,8 @@ public RemoveProcessor create(Map<String, Processor.Factory> registry, String pr
8192
final List<TemplateScript.Factory> compiledTemplates = fields.stream()
8293
.map(f -> ConfigurationUtils.compileTemplate(TYPE, processorTag, "field", f, scriptService))
8394
.collect(Collectors.toList());
84-
return new RemoveProcessor(processorTag, compiledTemplates);
95+
boolean ignoreMissing = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "ignore_missing", false);
96+
return new RemoveProcessor(processorTag, compiledTemplates, ignoreMissing);
8597
}
8698
}
8799
}

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

+17-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.util.Collections;
2929
import java.util.HashMap;
30+
import java.util.Map;
3031

3132
import static org.hamcrest.Matchers.containsString;
3233
import static org.hamcrest.Matchers.equalTo;
@@ -37,21 +38,34 @@ public void testRemoveFields() throws Exception {
3738
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
3839
String field = RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument);
3940
Processor processor = new RemoveProcessor(randomAlphaOfLength(10),
40-
Collections.singletonList(new TestTemplateService.MockTemplateScript.Factory(field)));
41+
Collections.singletonList(new TestTemplateService.MockTemplateScript.Factory(field)), false);
4142
processor.execute(ingestDocument);
4243
assertThat(ingestDocument.hasField(field), equalTo(false));
4344
}
4445

4546
public void testRemoveNonExistingField() throws Exception {
4647
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
4748
String fieldName = RandomDocumentPicks.randomFieldName(random());
48-
Processor processor = new RemoveProcessor(randomAlphaOfLength(10),
49-
Collections.singletonList(new TestTemplateService.MockTemplateScript.Factory(fieldName)));
49+
Map<String, Object> config = new HashMap<>();
50+
config.put("field", fieldName);
51+
String processorTag = randomAlphaOfLength(10);
52+
Processor processor = new RemoveProcessor.Factory(TestTemplateService.instance()).create(null, processorTag, config);
5053
try {
5154
processor.execute(ingestDocument);
5255
fail("remove field should have failed");
5356
} catch(IllegalArgumentException e) {
5457
assertThat(e.getMessage(), containsString("not present as part of path [" + fieldName + "]"));
5558
}
5659
}
60+
61+
public void testIgnoreMissing() throws Exception {
62+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
63+
String fieldName = RandomDocumentPicks.randomFieldName(random());
64+
Map<String, Object> config = new HashMap<>();
65+
config.put("field", fieldName);
66+
config.put("ignore_missing", true);
67+
String processorTag = randomAlphaOfLength(10);
68+
Processor processor = new RemoveProcessor.Factory(TestTemplateService.instance()).create(null, processorTag, config);
69+
processor.execute(ingestDocument);
70+
}
5771
}

0 commit comments

Comments
 (0)