forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce KV Processor in Ingest Node (#22272)
Now you can parse field values of the `key=value` variety and have `key` be inserted as a field name in an ingest document. Closes #22222.
- Loading branch information
Showing
7 changed files
with
411 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/KeyValueProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.ingest.common; | ||
|
||
import org.elasticsearch.ingest.AbstractProcessor; | ||
import org.elasticsearch.ingest.ConfigurationUtils; | ||
import org.elasticsearch.ingest.IngestDocument; | ||
import org.elasticsearch.ingest.Processor; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* The KeyValueProcessor parses and extracts messages of the `key=value` variety into fields with values of the keys. | ||
*/ | ||
public final class KeyValueProcessor extends AbstractProcessor { | ||
|
||
public static final String TYPE = "kv"; | ||
|
||
private final String field; | ||
private final String fieldSplit; | ||
private final String valueSplit; | ||
private final List<String> includeKeys; | ||
private final String targetField; | ||
private final boolean ignoreMissing; | ||
|
||
KeyValueProcessor(String tag, String field, String fieldSplit, String valueSplit, List<String> includeKeys, | ||
String targetField, boolean ignoreMissing) { | ||
super(tag); | ||
this.field = field; | ||
this.targetField = targetField; | ||
this.fieldSplit = fieldSplit; | ||
this.valueSplit = valueSplit; | ||
this.includeKeys = includeKeys; | ||
this.ignoreMissing = ignoreMissing; | ||
} | ||
|
||
String getField() { | ||
return field; | ||
} | ||
|
||
String getFieldSplit() { | ||
return fieldSplit; | ||
} | ||
|
||
String getValueSplit() { | ||
return valueSplit; | ||
} | ||
|
||
List<String> getIncludeKeys() { | ||
return includeKeys; | ||
} | ||
|
||
String getTargetField() { | ||
return targetField; | ||
} | ||
|
||
boolean isIgnoreMissing() { | ||
return ignoreMissing; | ||
} | ||
|
||
public void append(IngestDocument document, String targetField, String value) { | ||
if (document.hasField(targetField)) { | ||
document.appendFieldValue(targetField, value); | ||
} else { | ||
document.setFieldValue(targetField, value); | ||
} | ||
} | ||
|
||
@Override | ||
public void execute(IngestDocument document) { | ||
String oldVal = document.getFieldValue(field, String.class, ignoreMissing); | ||
|
||
if (oldVal == null && ignoreMissing) { | ||
return; | ||
} else if (oldVal == null) { | ||
throw new IllegalArgumentException("field [" + field + "] is null, cannot extract key-value pairs."); | ||
} | ||
|
||
String fieldPathPrefix = (targetField == null) ? "" : targetField + "."; | ||
Arrays.stream(oldVal.split(fieldSplit)) | ||
.map((f) -> f.split(valueSplit, 2)) | ||
.filter((p) -> includeKeys == null || includeKeys.contains(p[0])) | ||
.forEach((p) -> append(document, fieldPathPrefix + p[0], p[1])); | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return TYPE; | ||
} | ||
|
||
public static class Factory implements Processor.Factory { | ||
@Override | ||
public KeyValueProcessor create(Map<String, Processor.Factory> registry, String processorTag, | ||
Map<String, Object> config) throws Exception { | ||
String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field"); | ||
String targetField = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, "target_field"); | ||
String fieldSplit = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field_split"); | ||
String valueSplit = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "value_split"); | ||
List<String> includeKeys = ConfigurationUtils.readOptionalList(TYPE, processorTag, config, "include_keys"); | ||
if (includeKeys != null) { | ||
includeKeys = Collections.unmodifiableList(includeKeys); | ||
} | ||
boolean ignoreMissing = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "ignore_missing", false); | ||
return new KeyValueProcessor(processorTag, field, fieldSplit, valueSplit, includeKeys, targetField, ignoreMissing); | ||
} | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
...t-common/src/test/java/org/elasticsearch/ingest/common/KeyValueProcessorFactoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.ingest.common; | ||
|
||
import org.elasticsearch.ElasticsearchException; | ||
import org.elasticsearch.ElasticsearchParseException; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
public class KeyValueProcessorFactoryTests extends ESTestCase { | ||
|
||
public void testCreateWithDefaults() throws Exception { | ||
KeyValueProcessor.Factory factory = new KeyValueProcessor.Factory(); | ||
Map<String, Object> config = new HashMap<>(); | ||
config.put("field", "field1"); | ||
config.put("field_split", "&"); | ||
config.put("value_split", "="); | ||
String processorTag = randomAsciiOfLength(10); | ||
KeyValueProcessor processor = factory.create(null, processorTag, config); | ||
assertThat(processor.getTag(), equalTo(processorTag)); | ||
assertThat(processor.getField(), equalTo("field1")); | ||
assertThat(processor.getFieldSplit(), equalTo("&")); | ||
assertThat(processor.getValueSplit(), equalTo("=")); | ||
assertThat(processor.getIncludeKeys(), is(nullValue())); | ||
assertThat(processor.getTargetField(), is(nullValue())); | ||
assertFalse(processor.isIgnoreMissing()); | ||
} | ||
|
||
public void testCreateWithAllFieldsSet() throws Exception { | ||
KeyValueProcessor.Factory factory = new KeyValueProcessor.Factory(); | ||
Map<String, Object> config = new HashMap<>(); | ||
config.put("field", "field1"); | ||
config.put("field_split", "&"); | ||
config.put("value_split", "="); | ||
config.put("target_field", "target"); | ||
config.put("include_keys", Arrays.asList("a", "b")); | ||
config.put("ignore_missing", true); | ||
String processorTag = randomAsciiOfLength(10); | ||
KeyValueProcessor processor = factory.create(null, processorTag, config); | ||
assertThat(processor.getTag(), equalTo(processorTag)); | ||
assertThat(processor.getField(), equalTo("field1")); | ||
assertThat(processor.getFieldSplit(), equalTo("&")); | ||
assertThat(processor.getValueSplit(), equalTo("=")); | ||
assertThat(processor.getIncludeKeys(), equalTo(Arrays.asList("a", "b"))); | ||
assertThat(processor.getTargetField(), equalTo("target")); | ||
assertTrue(processor.isIgnoreMissing()); | ||
} | ||
|
||
public void testCreateWithMissingField() { | ||
KeyValueProcessor.Factory factory = new KeyValueProcessor.Factory(); | ||
Map<String, Object> config = new HashMap<>(); | ||
String processorTag = randomAsciiOfLength(10); | ||
ElasticsearchException exception = expectThrows(ElasticsearchParseException.class, | ||
() -> factory.create(null, processorTag, config)); | ||
assertThat(exception.getMessage(), equalTo("[field] required property is missing")); | ||
} | ||
|
||
public void testCreateWithMissingFieldSplit() { | ||
KeyValueProcessor.Factory factory = new KeyValueProcessor.Factory(); | ||
Map<String, Object> config = new HashMap<>(); | ||
config.put("field", "field1"); | ||
String processorTag = randomAsciiOfLength(10); | ||
ElasticsearchException exception = expectThrows(ElasticsearchParseException.class, | ||
() -> factory.create(null, processorTag, config)); | ||
assertThat(exception.getMessage(), equalTo("[field_split] required property is missing")); | ||
} | ||
|
||
public void testCreateWithMissingValueSplit() { | ||
KeyValueProcessor.Factory factory = new KeyValueProcessor.Factory(); | ||
Map<String, Object> config = new HashMap<>(); | ||
config.put("field", "field1"); | ||
config.put("field_split", "&"); | ||
String processorTag = randomAsciiOfLength(10); | ||
ElasticsearchException exception = expectThrows(ElasticsearchParseException.class, | ||
() -> factory.create(null, processorTag, config)); | ||
assertThat(exception.getMessage(), equalTo("[value_split] required property is missing")); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...s/ingest-common/src/test/java/org/elasticsearch/ingest/common/KeyValueProcessorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.ingest.common; | ||
|
||
import org.elasticsearch.ingest.IngestDocument; | ||
import org.elasticsearch.ingest.Processor; | ||
import org.elasticsearch.ingest.RandomDocumentPicks; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class KeyValueProcessorTests extends ESTestCase { | ||
|
||
public void test() throws Exception { | ||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); | ||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "first=hello&second=world&second=universe"); | ||
Processor processor = new KeyValueProcessor(randomAsciiOfLength(10), fieldName, "&", "=", null, "target", false); | ||
processor.execute(ingestDocument); | ||
assertThat(ingestDocument.getFieldValue("target.first", String.class), equalTo("hello")); | ||
assertThat(ingestDocument.getFieldValue("target.second", List.class), equalTo(Arrays.asList("world", "universe"))); | ||
} | ||
|
||
public void testRootTarget() throws Exception { | ||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap()); | ||
ingestDocument.setFieldValue("myField", "first=hello&second=world&second=universe"); | ||
Processor processor = new KeyValueProcessor(randomAsciiOfLength(10), "myField", "&", "=", null, null, false); | ||
processor.execute(ingestDocument); | ||
assertThat(ingestDocument.getFieldValue("first", String.class), equalTo("hello")); | ||
assertThat(ingestDocument.getFieldValue("second", List.class), equalTo(Arrays.asList("world", "universe"))); | ||
} | ||
|
||
public void testKeySameAsSourceField() throws Exception { | ||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap()); | ||
ingestDocument.setFieldValue("first", "first=hello"); | ||
Processor processor = new KeyValueProcessor(randomAsciiOfLength(10), "first", "&", "=", null, null, false); | ||
processor.execute(ingestDocument); | ||
assertThat(ingestDocument.getFieldValue("first", List.class), equalTo(Arrays.asList("first=hello", "hello"))); | ||
} | ||
|
||
public void testIncludeKeys() throws Exception { | ||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); | ||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "first=hello&second=world&second=universe"); | ||
Processor processor = new KeyValueProcessor(randomAsciiOfLength(10), fieldName, "&", "=", | ||
Collections.singletonList("first"), "target", false); | ||
processor.execute(ingestDocument); | ||
assertThat(ingestDocument.getFieldValue("target.first", String.class), equalTo("hello")); | ||
assertFalse(ingestDocument.hasField("target.second")); | ||
} | ||
|
||
public void testMissingField() { | ||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap()); | ||
Processor processor = new KeyValueProcessor(randomAsciiOfLength(10), "unknown", "&", "=", null, "target", false); | ||
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> processor.execute(ingestDocument)); | ||
assertThat(exception.getMessage(), equalTo("field [unknown] not present as part of path [unknown]")); | ||
} | ||
|
||
public void testNullValueWithIgnoreMissing() throws Exception { | ||
String fieldName = RandomDocumentPicks.randomFieldName(random()); | ||
IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), | ||
Collections.singletonMap(fieldName, null)); | ||
IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); | ||
Processor processor = new KeyValueProcessor(randomAsciiOfLength(10), fieldName, "", "", null, "target", true); | ||
processor.execute(ingestDocument); | ||
assertIngestDocument(originalIngestDocument, ingestDocument); | ||
} | ||
|
||
public void testNonExistentWithIgnoreMissing() throws Exception { | ||
IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap()); | ||
IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); | ||
Processor processor = new KeyValueProcessor(randomAsciiOfLength(10), "unknown", "", "", null, "target", true); | ||
processor.execute(ingestDocument); | ||
assertIngestDocument(originalIngestDocument, ingestDocument); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.