update all processors to only operate on one field at a time when possible
This commit is contained in:
parent
d7c3b51b9c
commit
45f48ac126
|
@ -4,52 +4,46 @@
|
|||
=== Processors
|
||||
|
||||
==== Set processor
|
||||
Sets one or more fields and associates them with the specified values. If a field already exists,
|
||||
Sets one field and associates it with the specified value. If a field already exists,
|
||||
its value will be replaced with the provided one.
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
{
|
||||
"set": {
|
||||
"fields": {
|
||||
"field": 582.1
|
||||
}
|
||||
"field1": 582.1
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
|
||||
==== Remove processor
|
||||
Removes one or more existing fields. If a field doesn't exist, nothing will happen.
|
||||
Removes an existing field. If a field doesn't exist, nothing will happen.
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
{
|
||||
"remove": {
|
||||
"fields": [
|
||||
"field1","field2"
|
||||
]
|
||||
"field": "foo"
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
|
||||
==== Rename processor
|
||||
Renames one or more existing fields. If a field doesn't exist, an exception will be thrown. Also, the new field
|
||||
Renames an existing fields. If a field doesn't exist, an exception will be thrown. Also, the new field
|
||||
name must not exist.
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
{
|
||||
"rename": {
|
||||
"fields": {
|
||||
"field1": "field2"
|
||||
}
|
||||
"field": "foo"
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
|
||||
|
||||
==== Convert processor
|
||||
Converts one or more field value to a different type, like turning a string to an integer.
|
||||
Converts an existing field's value to a different type, like turning a string to an integer.
|
||||
If the field value is an array, all members will be converted.
|
||||
|
||||
The supported types include: `integer`, `float`, `string`, and `boolean`.
|
||||
|
@ -61,10 +55,7 @@ false if its string value is equal to `false` (ignore case) and it will throw ex
|
|||
--------------------------------------------------
|
||||
{
|
||||
"convert": {
|
||||
"fields": {
|
||||
"field1": "integer",
|
||||
"field2": "float"
|
||||
}
|
||||
"foo": "integer"
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
|
@ -73,8 +64,7 @@ false if its string value is equal to `false` (ignore case) and it will throw ex
|
|||
Converts a string field by applying a regular expression and a replacement.
|
||||
If the field is not a string, the processor will throw an exception.
|
||||
|
||||
This configuration takes an `expression` array consisting of objects. Each object
|
||||
holds three elements: `field` for the field name, `pattern` for the
|
||||
This configuration takes a `field` for the field name, `pattern` for the
|
||||
pattern to be replaced, and `replacement` for the string to replace the matching patterns with.
|
||||
|
||||
|
||||
|
@ -82,13 +72,9 @@ pattern to be replaced, and `replacement` for the string to replace the matching
|
|||
--------------------------------------------------
|
||||
{
|
||||
"gsub": {
|
||||
"expressions": [
|
||||
{
|
||||
"field": "field1",
|
||||
"pattern": "\.",
|
||||
"replacement": "-"
|
||||
}
|
||||
]
|
||||
"field": "field1",
|
||||
"pattern": "\.",
|
||||
"replacement": "-"
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
|
@ -101,9 +87,8 @@ Throws error when the field is not an array.
|
|||
--------------------------------------------------
|
||||
{
|
||||
"join": {
|
||||
"fields": {
|
||||
"joined_array_field": "other_array_field"
|
||||
}
|
||||
"field": "joined_array_field",
|
||||
"separator": "-"
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
|
@ -115,9 +100,7 @@ Split a field to an array using a separator character. Only works on string fiel
|
|||
--------------------------------------------------
|
||||
{
|
||||
"split": {
|
||||
"fields": {
|
||||
"message": ","
|
||||
}
|
||||
"field": ","
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
|
@ -129,7 +112,7 @@ Converts a string to its lowercase equivalent.
|
|||
--------------------------------------------------
|
||||
{
|
||||
"lowercase": {
|
||||
"fields": ["foo", "bar"]
|
||||
"field": "foo"
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
|
@ -141,7 +124,7 @@ Converts a string to its uppercase equivalent.
|
|||
--------------------------------------------------
|
||||
{
|
||||
"uppercase": {
|
||||
"fields": ["foo", "bar"]
|
||||
"field": "foo"
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
|
@ -153,7 +136,7 @@ Trims whitespace from field. NOTE: this only works on leading and trailing white
|
|||
--------------------------------------------------
|
||||
{
|
||||
"trim": {
|
||||
"fields": ["foo", "bar"]
|
||||
"field": "foo"
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
|
@ -538,4 +521,4 @@ The delete pipeline api deletes pipelines by id.
|
|||
--------------------------------------------------
|
||||
DELETE _ingest/pipeline/my-pipeline-id
|
||||
--------------------------------------------------
|
||||
// AUTOSENSE
|
||||
// AUTOSENSE
|
||||
|
|
|
@ -32,25 +32,23 @@ import java.util.Map;
|
|||
*/
|
||||
public abstract class AbstractStringProcessor implements Processor {
|
||||
|
||||
private final Collection<String> fields;
|
||||
private final String field;
|
||||
|
||||
protected AbstractStringProcessor(Collection<String> fields) {
|
||||
this.fields = fields;
|
||||
protected AbstractStringProcessor(String field) {
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
public Collection<String> getFields() {
|
||||
return fields;
|
||||
public String getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void execute(IngestDocument document) {
|
||||
for(String field : fields) {
|
||||
String val = document.getFieldValue(field, String.class);
|
||||
if (val == null) {
|
||||
throw new IllegalArgumentException("field [" + field + "] is null, cannot process it.");
|
||||
}
|
||||
document.setFieldValue(field, process(val));
|
||||
String val = document.getFieldValue(field, String.class);
|
||||
if (val == null) {
|
||||
throw new IllegalArgumentException("field [" + field + "] is null, cannot process it.");
|
||||
}
|
||||
document.setFieldValue(field, process(val));
|
||||
}
|
||||
|
||||
protected abstract String process(String value);
|
||||
|
@ -58,10 +56,10 @@ public abstract class AbstractStringProcessor implements Processor {
|
|||
public static abstract class Factory<T extends AbstractStringProcessor> implements Processor.Factory<T> {
|
||||
@Override
|
||||
public T create(Map<String, Object> config) throws Exception {
|
||||
List<String> fields = ConfigurationUtils.readList(config, "fields");
|
||||
return newProcessor(Collections.unmodifiableList(fields));
|
||||
String field = ConfigurationUtils.readStringProperty(config, "field");
|
||||
return newProcessor(field);
|
||||
}
|
||||
|
||||
protected abstract T newProcessor(Collection<String> fields);
|
||||
protected abstract T newProcessor(String field);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -149,4 +149,15 @@ public final class ConfigurationUtils {
|
|||
throw new IllegalArgumentException("property [" + propertyName + "] isn't a map, but of type [" + value.getClass().getName() + "]");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns and removes the specified property as an {@link Object} from the specified configuration map.
|
||||
*/
|
||||
public static Object readObject(Map<String, Object> configuration, String propertyName) {
|
||||
Object value = configuration.remove(propertyName);
|
||||
if (value == null) {
|
||||
throw new IllegalArgumentException("required property [" + propertyName + "] is missing");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -87,38 +87,41 @@ public class ConvertProcessor implements Processor {
|
|||
|
||||
public static final String TYPE = "convert";
|
||||
|
||||
private final Map<String, Type> fields;
|
||||
private final String field;
|
||||
private final Type convertType;
|
||||
|
||||
ConvertProcessor(Map<String, Type> fields) {
|
||||
this.fields = fields;
|
||||
ConvertProcessor(String field, Type convertType) {
|
||||
this.field = field;
|
||||
this.convertType = convertType;
|
||||
}
|
||||
|
||||
Map<String, Type> getFields() {
|
||||
return fields;
|
||||
String getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
Type getConvertType() {
|
||||
return convertType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(IngestDocument document) {
|
||||
for(Map.Entry<String, Type> entry : fields.entrySet()) {
|
||||
Type type = entry.getValue();
|
||||
Object oldValue = document.getFieldValue(entry.getKey(), Object.class);
|
||||
Object newValue;
|
||||
if (oldValue == null) {
|
||||
throw new IllegalArgumentException("Field [" + entry.getKey() + "] is null, cannot be converted to type [" + type + "]");
|
||||
}
|
||||
|
||||
if (oldValue instanceof List) {
|
||||
List<?> list = (List<?>) oldValue;
|
||||
List<Object> newList = new ArrayList<>();
|
||||
for (Object value : list) {
|
||||
newList.add(type.convert(value));
|
||||
}
|
||||
newValue = newList;
|
||||
} else {
|
||||
newValue = type.convert(oldValue);
|
||||
}
|
||||
document.setFieldValue(entry.getKey(), newValue);
|
||||
Object oldValue = document.getFieldValue(field, Object.class);
|
||||
Object newValue;
|
||||
if (oldValue == null) {
|
||||
throw new IllegalArgumentException("Field [" + field + "] is null, cannot be converted to type [" + convertType + "]");
|
||||
}
|
||||
|
||||
if (oldValue instanceof List) {
|
||||
List<?> list = (List<?>) oldValue;
|
||||
List<Object> newList = new ArrayList<>();
|
||||
for (Object value : list) {
|
||||
newList.add(convertType.convert(value));
|
||||
}
|
||||
newValue = newList;
|
||||
} else {
|
||||
newValue = convertType.convert(oldValue);
|
||||
}
|
||||
document.setFieldValue(field, newValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -129,12 +132,9 @@ public class ConvertProcessor implements Processor {
|
|||
public static class Factory implements Processor.Factory<ConvertProcessor> {
|
||||
@Override
|
||||
public ConvertProcessor create(Map<String, Object> config) throws Exception {
|
||||
Map<String, String> fields = ConfigurationUtils.readMap(config, "fields");
|
||||
Map<String, Type> convertFields = new HashMap<>();
|
||||
for (Map.Entry<String, String> entry : fields.entrySet()) {
|
||||
convertFields.put(entry.getKey(), Type.fromString(entry.getValue()));
|
||||
}
|
||||
return new ConvertProcessor(Collections.unmodifiableMap(convertFields));
|
||||
String field = ConfigurationUtils.readStringProperty(config, "field");
|
||||
Type convertType = Type.fromString(ConfigurationUtils.readStringProperty(config, "type"));
|
||||
return new ConvertProcessor(field, convertType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,70 +0,0 @@
|
|||
/*
|
||||
* 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.processor.gsub;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Represents a gsub expression containing the field name, the pattern to look for and its string replacement.
|
||||
*/
|
||||
public class GsubExpression {
|
||||
|
||||
private final String fieldName;
|
||||
private final Pattern pattern;
|
||||
private final String replacement;
|
||||
|
||||
public GsubExpression(String fieldName, Pattern pattern, String replacement) {
|
||||
this.fieldName = Objects.requireNonNull(fieldName);
|
||||
this.pattern = Objects.requireNonNull(pattern);
|
||||
this.replacement = Objects.requireNonNull(replacement);
|
||||
}
|
||||
|
||||
public String getFieldName() {
|
||||
return fieldName;
|
||||
}
|
||||
|
||||
public Pattern getPattern() {
|
||||
return pattern;
|
||||
}
|
||||
|
||||
public String getReplacement() {
|
||||
return replacement;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
GsubExpression that = (GsubExpression) o;
|
||||
return Objects.equals(fieldName, that.fieldName) &&
|
||||
Objects.equals(pattern.pattern(), that.pattern.pattern()) &&
|
||||
Objects.equals(replacement, that.replacement);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(fieldName, pattern, replacement);
|
||||
}
|
||||
}
|
|
@ -23,8 +23,6 @@ import org.elasticsearch.ingest.IngestDocument;
|
|||
import org.elasticsearch.ingest.processor.ConfigurationUtils;
|
||||
import org.elasticsearch.ingest.processor.Processor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
@ -37,27 +35,38 @@ public class GsubProcessor implements Processor {
|
|||
|
||||
public static final String TYPE = "gsub";
|
||||
|
||||
private final List<GsubExpression> gsubExpressions;
|
||||
private final String field;
|
||||
private final Pattern pattern;
|
||||
private final String replacement;
|
||||
|
||||
GsubProcessor(List<GsubExpression> gsubExpressions) {
|
||||
this.gsubExpressions = gsubExpressions;
|
||||
GsubProcessor(String field, Pattern pattern, String replacement) {
|
||||
this.field = field;
|
||||
this.pattern = pattern;
|
||||
this.replacement = replacement;
|
||||
}
|
||||
|
||||
List<GsubExpression> getGsubExpressions() {
|
||||
return gsubExpressions;
|
||||
String getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
Pattern getPattern() {
|
||||
return pattern;
|
||||
}
|
||||
|
||||
String getReplacement() {
|
||||
return replacement;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void execute(IngestDocument document) {
|
||||
for (GsubExpression gsubExpression : gsubExpressions) {
|
||||
String oldVal = document.getFieldValue(gsubExpression.getFieldName(), String.class);
|
||||
if (oldVal == null) {
|
||||
throw new IllegalArgumentException("field [" + gsubExpression.getFieldName() + "] is null, cannot match pattern.");
|
||||
}
|
||||
Matcher matcher = gsubExpression.getPattern().matcher(oldVal);
|
||||
String newVal = matcher.replaceAll(gsubExpression.getReplacement());
|
||||
document.setFieldValue(gsubExpression.getFieldName(), newVal);
|
||||
String oldVal = document.getFieldValue(field, String.class);
|
||||
if (oldVal == null) {
|
||||
throw new IllegalArgumentException("field [" + field + "] is null, cannot match pattern.");
|
||||
}
|
||||
Matcher matcher = pattern.matcher(oldVal);
|
||||
String newVal = matcher.replaceAll(replacement);
|
||||
document.setFieldValue(field, newVal);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -68,25 +77,11 @@ public class GsubProcessor implements Processor {
|
|||
public static class Factory implements Processor.Factory<GsubProcessor> {
|
||||
@Override
|
||||
public GsubProcessor create(Map<String, Object> config) throws Exception {
|
||||
List<Map<String, String>> gsubConfig = ConfigurationUtils.readList(config, "expressions");
|
||||
List<GsubExpression> gsubExpressions = new ArrayList<>();
|
||||
for (Map<String, String> stringObjectMap : gsubConfig) {
|
||||
String field = stringObjectMap.get("field");
|
||||
if (field == null) {
|
||||
throw new IllegalArgumentException("no [field] specified for gsub expression");
|
||||
}
|
||||
String pattern = stringObjectMap.get("pattern");
|
||||
if (pattern == null) {
|
||||
throw new IllegalArgumentException("no [pattern] specified for gsub expression");
|
||||
}
|
||||
String replacement = stringObjectMap.get("replacement");
|
||||
if (replacement == null) {
|
||||
throw new IllegalArgumentException("no [replacement] specified for gsub expression");
|
||||
}
|
||||
Pattern searchPattern = Pattern.compile(pattern);
|
||||
gsubExpressions.add(new GsubExpression(field, searchPattern, replacement));
|
||||
}
|
||||
return new GsubProcessor(gsubExpressions);
|
||||
String field = ConfigurationUtils.readStringProperty(config, "field");
|
||||
String pattern = ConfigurationUtils.readStringProperty(config, "pattern");
|
||||
String replacement = ConfigurationUtils.readStringProperty(config, "replacement");
|
||||
Pattern searchPattern = Pattern.compile(pattern);
|
||||
return new GsubProcessor(field, searchPattern, replacement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ import org.elasticsearch.ingest.IngestDocument;
|
|||
import org.elasticsearch.ingest.processor.ConfigurationUtils;
|
||||
import org.elasticsearch.ingest.processor.Processor;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
@ -36,28 +36,32 @@ public class JoinProcessor implements Processor {
|
|||
|
||||
public static final String TYPE = "join";
|
||||
|
||||
private final Map<String, String> fields;
|
||||
private final String field;
|
||||
private final String separator;
|
||||
|
||||
JoinProcessor(Map<String, String> fields) {
|
||||
this.fields = fields;
|
||||
JoinProcessor(String field, String separator) {
|
||||
this.field = field;
|
||||
this.separator = separator;
|
||||
}
|
||||
|
||||
Map<String, String> getFields() {
|
||||
return fields;
|
||||
String getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
String getSeparator() {
|
||||
return separator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(IngestDocument document) {
|
||||
for(Map.Entry<String, String> entry : fields.entrySet()) {
|
||||
List<?> list = document.getFieldValue(entry.getKey(), List.class);
|
||||
if (list == null) {
|
||||
throw new IllegalArgumentException("field [" + entry.getKey() + "] is null, cannot join.");
|
||||
}
|
||||
String joined = list.stream()
|
||||
.map(Object::toString)
|
||||
.collect(Collectors.joining(entry.getValue()));
|
||||
document.setFieldValue(entry.getKey(), joined);
|
||||
List<?> list = document.getFieldValue(field, List.class);
|
||||
if (list == null) {
|
||||
throw new IllegalArgumentException("field [" + field + "] is null, cannot join.");
|
||||
}
|
||||
String joined = list.stream()
|
||||
.map(Object::toString)
|
||||
.collect(Collectors.joining(separator));
|
||||
document.setFieldValue(field, joined);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -68,8 +72,9 @@ public class JoinProcessor implements Processor {
|
|||
public static class Factory implements Processor.Factory<JoinProcessor> {
|
||||
@Override
|
||||
public JoinProcessor create(Map<String, Object> config) throws Exception {
|
||||
Map<String, String> fields = ConfigurationUtils.readMap(config, "fields");
|
||||
return new JoinProcessor(Collections.unmodifiableMap(fields));
|
||||
String field = ConfigurationUtils.readStringProperty(config, "field");
|
||||
String separator = ConfigurationUtils.readStringProperty(config, "separator");
|
||||
return new JoinProcessor(field, separator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,8 +33,8 @@ public class LowercaseProcessor extends AbstractStringProcessor {
|
|||
|
||||
public static final String TYPE = "lowercase";
|
||||
|
||||
LowercaseProcessor(Collection<String> fields) {
|
||||
super(fields);
|
||||
LowercaseProcessor(String field) {
|
||||
super(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -49,8 +49,8 @@ public class LowercaseProcessor extends AbstractStringProcessor {
|
|||
|
||||
public static class Factory extends AbstractStringProcessor.Factory<LowercaseProcessor> {
|
||||
@Override
|
||||
protected LowercaseProcessor newProcessor(Collection<String> fields) {
|
||||
return new LowercaseProcessor(fields);
|
||||
protected LowercaseProcessor newProcessor(String field) {
|
||||
return new LowercaseProcessor(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,9 +23,6 @@ import org.elasticsearch.ingest.IngestDocument;
|
|||
import org.elasticsearch.ingest.processor.ConfigurationUtils;
|
||||
import org.elasticsearch.ingest.processor.Processor;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
|
@ -35,21 +32,19 @@ public class RemoveProcessor implements Processor {
|
|||
|
||||
public static final String TYPE = "remove";
|
||||
|
||||
private final Collection<String> fields;
|
||||
private final String field;
|
||||
|
||||
RemoveProcessor(Collection<String> fields) {
|
||||
this.fields = fields;
|
||||
RemoveProcessor(String field) {
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
Collection<String> getFields() {
|
||||
return fields;
|
||||
String getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(IngestDocument document) {
|
||||
for(String field : fields) {
|
||||
document.removeField(field);
|
||||
}
|
||||
document.removeField(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -60,8 +55,8 @@ public class RemoveProcessor implements Processor {
|
|||
public static class Factory implements Processor.Factory<RemoveProcessor> {
|
||||
@Override
|
||||
public RemoveProcessor create(Map<String, Object> config) throws Exception {
|
||||
List<String> fields = ConfigurationUtils.readList(config, "fields");
|
||||
return new RemoveProcessor(Collections.unmodifiableList(fields));
|
||||
String field = ConfigurationUtils.readStringProperty(config, "field");
|
||||
return new RemoveProcessor(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ import org.elasticsearch.ingest.IngestDocument;
|
|||
import org.elasticsearch.ingest.processor.ConfigurationUtils;
|
||||
import org.elasticsearch.ingest.processor.Processor;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -33,37 +34,39 @@ public class RenameProcessor implements Processor {
|
|||
|
||||
public static final String TYPE = "rename";
|
||||
|
||||
private final Map<String, String> fields;
|
||||
private final String oldFieldName;
|
||||
private final String newFieldName;
|
||||
|
||||
RenameProcessor(Map<String, String> fields) {
|
||||
this.fields = fields;
|
||||
RenameProcessor(String oldFieldName, String newFieldName) {
|
||||
this.oldFieldName = oldFieldName;
|
||||
this.newFieldName = newFieldName;
|
||||
}
|
||||
|
||||
Map<String, String> getFields() {
|
||||
return fields;
|
||||
String getOldFieldName() {
|
||||
return oldFieldName;
|
||||
}
|
||||
|
||||
String getNewFieldName() {
|
||||
return newFieldName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(IngestDocument document) {
|
||||
for(Map.Entry<String, String> entry : fields.entrySet()) {
|
||||
String oldFieldName = entry.getKey();
|
||||
if (document.hasField(oldFieldName) == false) {
|
||||
throw new IllegalArgumentException("field [" + oldFieldName + "] doesn't exist");
|
||||
}
|
||||
String newFieldName = entry.getValue();
|
||||
if (document.hasField(newFieldName)) {
|
||||
throw new IllegalArgumentException("field [" + newFieldName + "] already exists");
|
||||
}
|
||||
if (document.hasField(oldFieldName) == false) {
|
||||
throw new IllegalArgumentException("field [" + oldFieldName + "] doesn't exist");
|
||||
}
|
||||
if (document.hasField(newFieldName)) {
|
||||
throw new IllegalArgumentException("field [" + newFieldName + "] already exists");
|
||||
}
|
||||
|
||||
Object oldValue = document.getFieldValue(entry.getKey(), Object.class);
|
||||
document.setFieldValue(newFieldName, oldValue);
|
||||
try {
|
||||
document.removeField(oldFieldName);
|
||||
} catch (Exception e) {
|
||||
//remove the new field if the removal of the old one failed
|
||||
document.removeField(newFieldName);
|
||||
throw e;
|
||||
}
|
||||
Object oldValue = document.getFieldValue(oldFieldName, Object.class);
|
||||
document.setFieldValue(newFieldName, oldValue);
|
||||
try {
|
||||
document.removeField(oldFieldName);
|
||||
} catch (Exception e) {
|
||||
//remove the new field if the removal of the old one failed
|
||||
document.removeField(newFieldName);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -75,8 +78,9 @@ public class RenameProcessor implements Processor {
|
|||
public static class Factory implements Processor.Factory<RenameProcessor> {
|
||||
@Override
|
||||
public RenameProcessor create(Map<String, Object> config) throws Exception {
|
||||
Map<String, String> fields = ConfigurationUtils.readMap(config, "fields");
|
||||
return new RenameProcessor(Collections.unmodifiableMap(fields));
|
||||
String field = ConfigurationUtils.readStringProperty(config, "field");
|
||||
String newField = ConfigurationUtils.readStringProperty(config, "to");
|
||||
return new RenameProcessor(field, newField);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ import org.elasticsearch.ingest.IngestDocument;
|
|||
import org.elasticsearch.ingest.processor.ConfigurationUtils;
|
||||
import org.elasticsearch.ingest.processor.Processor;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -34,21 +35,25 @@ public class SetProcessor implements Processor {
|
|||
|
||||
public static final String TYPE = "set";
|
||||
|
||||
private final Map<String, Object> fields;
|
||||
private final String field;
|
||||
private final Object value;
|
||||
|
||||
SetProcessor(Map<String, Object> fields) {
|
||||
this.fields = fields;
|
||||
SetProcessor(String field, Object value) {
|
||||
this.field = field;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
Map<String, Object> getFields() {
|
||||
return fields;
|
||||
String getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(IngestDocument document) {
|
||||
for(Map.Entry<String, Object> entry : fields.entrySet()) {
|
||||
document.setFieldValue(entry.getKey(), entry.getValue());
|
||||
}
|
||||
document.setFieldValue(field, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -59,8 +64,9 @@ public class SetProcessor implements Processor {
|
|||
public static final class Factory implements Processor.Factory<SetProcessor> {
|
||||
@Override
|
||||
public SetProcessor create(Map<String, Object> config) throws Exception {
|
||||
Map<String, Object> fields = ConfigurationUtils.readMap(config, "fields");
|
||||
return new SetProcessor(Collections.unmodifiableMap(fields));
|
||||
String field = ConfigurationUtils.readStringProperty(config, "field");
|
||||
Object value = ConfigurationUtils.readObject(config, "value");
|
||||
return new SetProcessor(field, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,25 +36,29 @@ public class SplitProcessor implements Processor {
|
|||
|
||||
public static final String TYPE = "split";
|
||||
|
||||
private final Map<String, String> fields;
|
||||
private final String field;
|
||||
private final String separator;
|
||||
|
||||
SplitProcessor(Map<String, String> fields) {
|
||||
this.fields = fields;
|
||||
SplitProcessor(String field, String separator) {
|
||||
this.field = field;
|
||||
this.separator = separator;
|
||||
}
|
||||
|
||||
Map<String, String> getFields() {
|
||||
return fields;
|
||||
String getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
String getSeparator() {
|
||||
return separator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(IngestDocument document) {
|
||||
for(Map.Entry<String, String> entry : fields.entrySet()) {
|
||||
String oldVal = document.getFieldValue(entry.getKey(), String.class);
|
||||
if (oldVal == null) {
|
||||
throw new IllegalArgumentException("field [" + entry.getKey() + "] is null, cannot split.");
|
||||
}
|
||||
document.setFieldValue(entry.getKey(), Arrays.asList(oldVal.split(entry.getValue())));
|
||||
String oldVal = document.getFieldValue(field, String.class);
|
||||
if (oldVal == null) {
|
||||
throw new IllegalArgumentException("field [" + field + "] is null, cannot split.");
|
||||
}
|
||||
document.setFieldValue(field, Arrays.asList(oldVal.split(separator)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -65,8 +69,8 @@ public class SplitProcessor implements Processor {
|
|||
public static class Factory implements Processor.Factory<SplitProcessor> {
|
||||
@Override
|
||||
public SplitProcessor create(Map<String, Object> config) throws Exception {
|
||||
Map<String, String> fields = ConfigurationUtils.readMap(config, "fields");
|
||||
return new SplitProcessor(Collections.unmodifiableMap(fields));
|
||||
String field = ConfigurationUtils.readStringProperty(config, "field");
|
||||
return new SplitProcessor(field, ConfigurationUtils.readStringProperty(config, "separator"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,8 +31,8 @@ public class TrimProcessor extends AbstractStringProcessor {
|
|||
|
||||
public static final String TYPE = "trim";
|
||||
|
||||
TrimProcessor(Collection<String> fields) {
|
||||
super(fields);
|
||||
TrimProcessor(String field) {
|
||||
super(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -47,8 +47,8 @@ public class TrimProcessor extends AbstractStringProcessor {
|
|||
|
||||
public static class Factory extends AbstractStringProcessor.Factory<TrimProcessor> {
|
||||
@Override
|
||||
protected TrimProcessor newProcessor(Collection<String> fields) {
|
||||
return new TrimProcessor(fields);
|
||||
protected TrimProcessor newProcessor(String field) {
|
||||
return new TrimProcessor(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,8 +32,8 @@ public class UppercaseProcessor extends AbstractStringProcessor {
|
|||
|
||||
public static final String TYPE = "uppercase";
|
||||
|
||||
UppercaseProcessor(Collection<String> fields) {
|
||||
super(fields);
|
||||
UppercaseProcessor(String field) {
|
||||
super(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -48,8 +48,8 @@ public class UppercaseProcessor extends AbstractStringProcessor {
|
|||
|
||||
public static class Factory extends AbstractStringProcessor.Factory<UppercaseProcessor> {
|
||||
@Override
|
||||
protected UppercaseProcessor newProcessor(Collection<String> fields) {
|
||||
return new UppercaseProcessor(fields);
|
||||
protected UppercaseProcessor newProcessor(String field) {
|
||||
return new UppercaseProcessor(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ import static org.hamcrest.Matchers.equalTo;
|
|||
|
||||
public abstract class AbstractStringProcessorTestCase extends ESTestCase {
|
||||
|
||||
protected abstract AbstractStringProcessor newProcessor(Collection<String> fields);
|
||||
protected abstract AbstractStringProcessor newProcessor(String field);
|
||||
|
||||
protected String modifyInput(String input) {
|
||||
return input;
|
||||
|
@ -43,23 +43,16 @@ public abstract class AbstractStringProcessorTestCase extends ESTestCase {
|
|||
|
||||
public void testProcessor() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||
int numFields = randomIntBetween(1, 5);
|
||||
Map<String, String> expected = new HashMap<>();
|
||||
for (int i = 0; i < numFields; i++) {
|
||||
String fieldValue = RandomDocumentPicks.randomString(random());
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, modifyInput(fieldValue));
|
||||
expected.put(fieldName, expectedResult(fieldValue));
|
||||
}
|
||||
Processor processor = newProcessor(expected.keySet());
|
||||
String fieldValue = RandomDocumentPicks.randomString(random());
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, modifyInput(fieldValue));
|
||||
Processor processor = newProcessor(fieldName);
|
||||
processor.execute(ingestDocument);
|
||||
for (Map.Entry<String, String> entry : expected.entrySet()) {
|
||||
assertThat(ingestDocument.getFieldValue(entry.getKey(), String.class), equalTo(entry.getValue()));
|
||||
}
|
||||
assertThat(ingestDocument.getFieldValue(fieldName, String.class), equalTo(expectedResult(fieldValue)));
|
||||
}
|
||||
|
||||
public void testFieldNotFound() throws Exception {
|
||||
String fieldName = RandomDocumentPicks.randomFieldName(random());
|
||||
Processor processor = newProcessor(Collections.singletonList(fieldName));
|
||||
Processor processor = newProcessor(fieldName);
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
|
@ -70,7 +63,7 @@ public abstract class AbstractStringProcessorTestCase extends ESTestCase {
|
|||
}
|
||||
|
||||
public void testNullValue() throws Exception {
|
||||
Processor processor = newProcessor(Collections.singletonList("field"));
|
||||
Processor processor = newProcessor("field");
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("field", null));
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
|
@ -82,7 +75,7 @@ public abstract class AbstractStringProcessorTestCase extends ESTestCase {
|
|||
|
||||
public void testNonStringValue() throws Exception {
|
||||
String fieldName = RandomDocumentPicks.randomFieldName(random());
|
||||
Processor processor = newProcessor(Collections.singletonList(fieldName));
|
||||
Processor processor = newProcessor(fieldName);
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
|
||||
ingestDocument.setFieldValue(fieldName, randomInt());
|
||||
try {
|
||||
|
|
|
@ -34,30 +34,19 @@ public class ConvertProcessorFactoryTests extends ESTestCase {
|
|||
ConvertProcessor.Factory factory = new ConvertProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
ConvertProcessor.Type type = randomFrom(ConvertProcessor.Type.values());
|
||||
Map<String, String> fields = Collections.singletonMap("field1", type.toString());
|
||||
config.put("fields", fields);
|
||||
config.put("field", "field1");
|
||||
config.put("type", type.toString());
|
||||
ConvertProcessor convertProcessor = factory.create(config);
|
||||
assertThat(convertProcessor.getFields().size(), equalTo(1));
|
||||
assertThat(convertProcessor.getFields().get("field1"), equalTo(type));
|
||||
}
|
||||
|
||||
public void testCreateMissingFields() throws Exception {
|
||||
ConvertProcessor.Factory factory = new ConvertProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("required property [fields] is missing"));
|
||||
}
|
||||
assertThat(convertProcessor.getField(), equalTo("field1"));
|
||||
assertThat(convertProcessor.getConvertType(), equalTo(type));
|
||||
}
|
||||
|
||||
public void testCreateUnsupportedType() throws Exception {
|
||||
ConvertProcessor.Factory factory = new ConvertProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
String type = "type-" + randomAsciiOfLengthBetween(1, 10);
|
||||
Map<String, String> fields = Collections.singletonMap("field1", type);
|
||||
config.put("fields", fields);
|
||||
config.put("field", "field1");
|
||||
config.put("type", type);
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
|
@ -65,4 +54,29 @@ public class ConvertProcessorFactoryTests extends ESTestCase {
|
|||
assertThat(e.getMessage(), Matchers.equalTo("type [" + type + "] not supported, cannot convert field."));
|
||||
}
|
||||
}
|
||||
|
||||
public void testCreateNoFieldPresent() throws Exception {
|
||||
ConvertProcessor.Factory factory = new ConvertProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
String type = "type-" + randomAsciiOfLengthBetween(1, 10);
|
||||
config.put("type", type);
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), Matchers.equalTo("required property [field] is missing"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testCreateNoTypePresent() throws Exception {
|
||||
ConvertProcessor.Factory factory = new ConvertProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
config.put("field", "field1");
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), Matchers.equalTo("required property [type] is missing"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,45 +34,27 @@ public class ConvertProcessorTests extends ESTestCase {
|
|||
|
||||
public void testConvertInt() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||
Map<String, ConvertProcessor.Type> fields = new HashMap<>();
|
||||
Map<String, Integer> expectedResult = new HashMap<>();
|
||||
int numFields = randomIntBetween(1, 5);
|
||||
for (int i = 0; i < numFields; i++) {
|
||||
int randomInt = randomInt();
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, randomInt);
|
||||
fields.put(fieldName, Type.INTEGER);
|
||||
expectedResult.put(fieldName, randomInt);
|
||||
}
|
||||
Processor processor = new ConvertProcessor(fields);
|
||||
int randomInt = randomInt();
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, randomInt);
|
||||
Processor processor = new ConvertProcessor(fieldName, Type.INTEGER);
|
||||
processor.execute(ingestDocument);
|
||||
for (Map.Entry<String, Integer> entry : expectedResult.entrySet()) {
|
||||
assertThat(ingestDocument.getFieldValue(entry.getKey(), Integer.class), equalTo(entry.getValue()));
|
||||
}
|
||||
assertThat(ingestDocument.getFieldValue(fieldName, Integer.class), equalTo(randomInt));
|
||||
}
|
||||
|
||||
public void testConvertIntList() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||
Map<String, ConvertProcessor.Type> fields = new HashMap<>();
|
||||
Map<String, List<Integer>> expectedResult = new HashMap<>();
|
||||
int numFields = randomIntBetween(1, 5);
|
||||
for (int i = 0; i < numFields; i++) {
|
||||
int numItems = randomIntBetween(1, 10);
|
||||
List<String> fieldValue = new ArrayList<>();
|
||||
List<Integer> expectedList = new ArrayList<>();
|
||||
for (int j = 0; j < numItems; j++) {
|
||||
int randomInt = randomInt();
|
||||
fieldValue.add(Integer.toString(randomInt));
|
||||
expectedList.add(randomInt);
|
||||
}
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
|
||||
fields.put(fieldName, Type.INTEGER);
|
||||
expectedResult.put(fieldName, expectedList);
|
||||
int numItems = randomIntBetween(1, 10);
|
||||
List<String> fieldValue = new ArrayList<>();
|
||||
List<Integer> expectedList = new ArrayList<>();
|
||||
for (int j = 0; j < numItems; j++) {
|
||||
int randomInt = randomInt();
|
||||
fieldValue.add(Integer.toString(randomInt));
|
||||
expectedList.add(randomInt);
|
||||
}
|
||||
Processor processor = new ConvertProcessor(fields);
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
|
||||
Processor processor = new ConvertProcessor(fieldName, Type.INTEGER);
|
||||
processor.execute(ingestDocument);
|
||||
for (Map.Entry<String, List<Integer>> entry : expectedResult.entrySet()) {
|
||||
assertThat(ingestDocument.getFieldValue(entry.getKey(), List.class), equalTo(entry.getValue()));
|
||||
}
|
||||
assertThat(ingestDocument.getFieldValue(fieldName, List.class), equalTo(expectedList));
|
||||
}
|
||||
|
||||
public void testConvertIntError() throws Exception {
|
||||
|
@ -81,8 +63,7 @@ public class ConvertProcessorTests extends ESTestCase {
|
|||
String value = "string-" + randomAsciiOfLengthBetween(1, 10);
|
||||
ingestDocument.setFieldValue(fieldName, value);
|
||||
|
||||
Map<String, Type> convert = Collections.singletonMap(fieldName, Type.INTEGER);
|
||||
Processor processor = new ConvertProcessor(convert);
|
||||
Processor processor = new ConvertProcessor(fieldName, Type.INTEGER);
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
fail("processor execute should have failed");
|
||||
|
@ -93,46 +74,30 @@ public class ConvertProcessorTests extends ESTestCase {
|
|||
|
||||
public void testConvertFloat() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||
Map<String, Type> fields = new HashMap<>();
|
||||
Map<String, Float> expectedResult = new HashMap<>();
|
||||
int numFields = randomIntBetween(1, 5);
|
||||
for (int i = 0; i < numFields; i++) {
|
||||
float randomFloat = randomFloat();
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, randomFloat);
|
||||
fields.put(fieldName, Type.FLOAT);
|
||||
expectedResult.put(fieldName, randomFloat);
|
||||
}
|
||||
float randomFloat = randomFloat();
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, randomFloat);
|
||||
expectedResult.put(fieldName, randomFloat);
|
||||
|
||||
Processor processor = new ConvertProcessor(fields);
|
||||
Processor processor = new ConvertProcessor(fieldName, Type.FLOAT);
|
||||
processor.execute(ingestDocument);
|
||||
for (Map.Entry<String, Float> entry : expectedResult.entrySet()) {
|
||||
assertThat(ingestDocument.getFieldValue(entry.getKey(), Float.class), equalTo(entry.getValue()));
|
||||
}
|
||||
assertThat(ingestDocument.getFieldValue(fieldName, Float.class), equalTo(randomFloat));
|
||||
}
|
||||
|
||||
public void testConvertFloatList() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||
Map<String, Type> fields = new HashMap<>();
|
||||
Map<String, List<Float>> expectedResult = new HashMap<>();
|
||||
int numFields = randomIntBetween(1, 5);
|
||||
for (int i = 0; i < numFields; i++) {
|
||||
int numItems = randomIntBetween(1, 10);
|
||||
List<String> fieldValue = new ArrayList<>();
|
||||
List<Float> expectedList = new ArrayList<>();
|
||||
for (int j = 0; j < numItems; j++) {
|
||||
float randomFloat = randomFloat();
|
||||
fieldValue.add(Float.toString(randomFloat));
|
||||
expectedList.add(randomFloat);
|
||||
}
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
|
||||
fields.put(fieldName, Type.FLOAT);
|
||||
expectedResult.put(fieldName, expectedList);
|
||||
int numItems = randomIntBetween(1, 10);
|
||||
List<String> fieldValue = new ArrayList<>();
|
||||
List<Float> expectedList = new ArrayList<>();
|
||||
for (int j = 0; j < numItems; j++) {
|
||||
float randomFloat = randomFloat();
|
||||
fieldValue.add(Float.toString(randomFloat));
|
||||
expectedList.add(randomFloat);
|
||||
}
|
||||
Processor processor = new ConvertProcessor(fields);
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
|
||||
Processor processor = new ConvertProcessor(fieldName, Type.FLOAT);
|
||||
processor.execute(ingestDocument);
|
||||
for (Map.Entry<String, List<Float>> entry : expectedResult.entrySet()) {
|
||||
assertThat(ingestDocument.getFieldValue(entry.getKey(), List.class), equalTo(entry.getValue()));
|
||||
}
|
||||
assertThat(ingestDocument.getFieldValue(fieldName, List.class), equalTo(expectedList));
|
||||
}
|
||||
|
||||
public void testConvertFloatError() throws Exception {
|
||||
|
@ -141,8 +106,7 @@ public class ConvertProcessorTests extends ESTestCase {
|
|||
String value = "string-" + randomAsciiOfLengthBetween(1, 10);
|
||||
ingestDocument.setFieldValue(fieldName, value);
|
||||
|
||||
Map<String, Type> convert = Collections.singletonMap(fieldName, Type.FLOAT);
|
||||
Processor processor = new ConvertProcessor(convert);
|
||||
Processor processor = new ConvertProcessor(fieldName, Type.FLOAT);
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
fail("processor execute should have failed");
|
||||
|
@ -155,52 +119,36 @@ public class ConvertProcessorTests extends ESTestCase {
|
|||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||
Map<String, Type> fields = new HashMap<>();
|
||||
Map<String, Boolean> expectedResult = new HashMap<>();
|
||||
int numFields = randomIntBetween(1, 5);
|
||||
for (int i = 0; i < numFields; i++) {
|
||||
boolean randomBoolean = randomBoolean();
|
||||
String booleanString = Boolean.toString(randomBoolean);
|
||||
if (randomBoolean) {
|
||||
booleanString = booleanString.toUpperCase(Locale.ROOT);
|
||||
}
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, booleanString);
|
||||
|
||||
Processor processor = new ConvertProcessor(fieldName, Type.BOOLEAN);
|
||||
processor.execute(ingestDocument);
|
||||
assertThat(ingestDocument.getFieldValue(fieldName, Boolean.class), equalTo(randomBoolean));
|
||||
}
|
||||
|
||||
public void testConvertBooleanList() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||
int numItems = randomIntBetween(1, 10);
|
||||
List<String> fieldValue = new ArrayList<>();
|
||||
List<Boolean> expectedList = new ArrayList<>();
|
||||
for (int j = 0; j < numItems; j++) {
|
||||
boolean randomBoolean = randomBoolean();
|
||||
String booleanString = Boolean.toString(randomBoolean);
|
||||
if (randomBoolean) {
|
||||
booleanString = booleanString.toUpperCase(Locale.ROOT);
|
||||
}
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, booleanString);
|
||||
fields.put(fieldName, Type.BOOLEAN);
|
||||
expectedResult.put(fieldName, randomBoolean);
|
||||
fieldValue.add(booleanString);
|
||||
expectedList.add(randomBoolean);
|
||||
}
|
||||
|
||||
Processor processor = new ConvertProcessor(fields);
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
|
||||
Processor processor = new ConvertProcessor(fieldName, Type.BOOLEAN);
|
||||
processor.execute(ingestDocument);
|
||||
for (Map.Entry<String, Boolean> entry : expectedResult.entrySet()) {
|
||||
assertThat(ingestDocument.getFieldValue(entry.getKey(), Boolean.class), equalTo(entry.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
public void testConvertBooleanList() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||
Map<String, Type> fields = new HashMap<>();
|
||||
Map<String, List<Boolean>> expectedResult = new HashMap<>();
|
||||
int numFields = randomIntBetween(1, 5);
|
||||
for (int i = 0; i < numFields; i++) {
|
||||
int numItems = randomIntBetween(1, 10);
|
||||
List<String> fieldValue = new ArrayList<>();
|
||||
List<Boolean> expectedList = new ArrayList<>();
|
||||
for (int j = 0; j < numItems; j++) {
|
||||
boolean randomBoolean = randomBoolean();
|
||||
String booleanString = Boolean.toString(randomBoolean);
|
||||
if (randomBoolean) {
|
||||
booleanString = booleanString.toUpperCase(Locale.ROOT);
|
||||
}
|
||||
fieldValue.add(booleanString);
|
||||
expectedList.add(randomBoolean);
|
||||
}
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
|
||||
fields.put(fieldName, Type.BOOLEAN);
|
||||
expectedResult.put(fieldName, expectedList);
|
||||
}
|
||||
Processor processor = new ConvertProcessor(fields);
|
||||
processor.execute(ingestDocument);
|
||||
for (Map.Entry<String, List<Boolean>> entry : expectedResult.entrySet()) {
|
||||
assertThat(ingestDocument.getFieldValue(entry.getKey(), List.class), equalTo(entry.getValue()));
|
||||
}
|
||||
assertThat(ingestDocument.getFieldValue(fieldName, List.class), equalTo(expectedList));
|
||||
}
|
||||
|
||||
public void testConvertBooleanError() throws Exception {
|
||||
|
@ -215,8 +163,7 @@ public class ConvertProcessorTests extends ESTestCase {
|
|||
}
|
||||
ingestDocument.setFieldValue(fieldName, fieldValue);
|
||||
|
||||
Map<String, Type> convert = Collections.singletonMap(fieldName, Type.BOOLEAN);
|
||||
Processor processor = new ConvertProcessor(convert);
|
||||
Processor processor = new ConvertProcessor(fieldName, Type.BOOLEAN);
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
fail("processor execute should have failed");
|
||||
|
@ -227,94 +174,75 @@ public class ConvertProcessorTests extends ESTestCase {
|
|||
|
||||
public void testConvertString() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||
Map<String, Type> fields = new HashMap<>();
|
||||
Map<String, String> expectedResult = new HashMap<>();
|
||||
int numFields = randomIntBetween(1, 5);
|
||||
for (int i = 0; i < numFields; i++) {
|
||||
Object fieldValue;
|
||||
String expectedFieldValue;
|
||||
switch(randomIntBetween(0, 2)) {
|
||||
case 0:
|
||||
float randomFloat = randomFloat();
|
||||
fieldValue = randomFloat;
|
||||
expectedFieldValue = Float.toString(randomFloat);
|
||||
break;
|
||||
case 1:
|
||||
int randomInt = randomInt();
|
||||
fieldValue = randomInt;
|
||||
expectedFieldValue = Integer.toString(randomInt);
|
||||
break;
|
||||
case 2:
|
||||
boolean randomBoolean = randomBoolean();
|
||||
fieldValue = randomBoolean;
|
||||
expectedFieldValue = Boolean.toString(randomBoolean);
|
||||
break;
|
||||
default:
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
|
||||
fields.put(fieldName, Type.STRING);
|
||||
expectedResult.put(fieldName, expectedFieldValue);
|
||||
Object fieldValue;
|
||||
String expectedFieldValue;
|
||||
switch(randomIntBetween(0, 2)) {
|
||||
case 0:
|
||||
float randomFloat = randomFloat();
|
||||
fieldValue = randomFloat;
|
||||
expectedFieldValue = Float.toString(randomFloat);
|
||||
break;
|
||||
case 1:
|
||||
int randomInt = randomInt();
|
||||
fieldValue = randomInt;
|
||||
expectedFieldValue = Integer.toString(randomInt);
|
||||
break;
|
||||
case 2:
|
||||
boolean randomBoolean = randomBoolean();
|
||||
fieldValue = randomBoolean;
|
||||
expectedFieldValue = Boolean.toString(randomBoolean);
|
||||
break;
|
||||
default:
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
|
||||
|
||||
Processor processor = new ConvertProcessor(fields);
|
||||
Processor processor = new ConvertProcessor(fieldName, Type.STRING);
|
||||
processor.execute(ingestDocument);
|
||||
for (Map.Entry<String, String> entry : expectedResult.entrySet()) {
|
||||
assertThat(ingestDocument.getFieldValue(entry.getKey(), String.class), equalTo(entry.getValue()));
|
||||
}
|
||||
assertThat(ingestDocument.getFieldValue(fieldName, String.class), equalTo(expectedFieldValue));
|
||||
}
|
||||
|
||||
public void testConvertStringList() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||
Map<String, Type> fields = new HashMap<>();
|
||||
Map<String, List<String>> expectedResult = new HashMap<>();
|
||||
int numFields = randomIntBetween(1, 5);
|
||||
for (int i = 0; i < numFields; i++) {
|
||||
int numItems = randomIntBetween(1, 10);
|
||||
List<Object> fieldValue = new ArrayList<>();
|
||||
List<String> expectedList = new ArrayList<>();
|
||||
for (int j = 0; j < numItems; j++) {
|
||||
Object randomValue;
|
||||
String randomValueString;
|
||||
switch(randomIntBetween(0, 2)) {
|
||||
case 0:
|
||||
float randomFloat = randomFloat();
|
||||
randomValue = randomFloat;
|
||||
randomValueString = Float.toString(randomFloat);
|
||||
break;
|
||||
case 1:
|
||||
int randomInt = randomInt();
|
||||
randomValue = randomInt;
|
||||
randomValueString = Integer.toString(randomInt);
|
||||
break;
|
||||
case 2:
|
||||
boolean randomBoolean = randomBoolean();
|
||||
randomValue = randomBoolean;
|
||||
randomValueString = Boolean.toString(randomBoolean);
|
||||
break;
|
||||
default:
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
fieldValue.add(randomValue);
|
||||
expectedList.add(randomValueString);
|
||||
int numItems = randomIntBetween(1, 10);
|
||||
List<Object> fieldValue = new ArrayList<>();
|
||||
List<String> expectedList = new ArrayList<>();
|
||||
for (int j = 0; j < numItems; j++) {
|
||||
Object randomValue;
|
||||
String randomValueString;
|
||||
switch(randomIntBetween(0, 2)) {
|
||||
case 0:
|
||||
float randomFloat = randomFloat();
|
||||
randomValue = randomFloat;
|
||||
randomValueString = Float.toString(randomFloat);
|
||||
break;
|
||||
case 1:
|
||||
int randomInt = randomInt();
|
||||
randomValue = randomInt;
|
||||
randomValueString = Integer.toString(randomInt);
|
||||
break;
|
||||
case 2:
|
||||
boolean randomBoolean = randomBoolean();
|
||||
randomValue = randomBoolean;
|
||||
randomValueString = Boolean.toString(randomBoolean);
|
||||
break;
|
||||
default:
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
|
||||
fields.put(fieldName, Type.STRING);
|
||||
expectedResult.put(fieldName, expectedList);
|
||||
fieldValue.add(randomValue);
|
||||
expectedList.add(randomValueString);
|
||||
}
|
||||
Processor processor = new ConvertProcessor(fields);
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
|
||||
Processor processor = new ConvertProcessor(fieldName, Type.STRING);
|
||||
processor.execute(ingestDocument);
|
||||
for (Map.Entry<String, List<String>> entry : expectedResult.entrySet()) {
|
||||
assertThat(ingestDocument.getFieldValue(entry.getKey(), List.class), equalTo(entry.getValue()));
|
||||
}
|
||||
assertThat(ingestDocument.getFieldValue(fieldName, List.class), equalTo(expectedList));
|
||||
}
|
||||
|
||||
public void testConvertNonExistingField() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
|
||||
String fieldName = RandomDocumentPicks.randomFieldName(random());
|
||||
Type type = randomFrom(Type.values());
|
||||
Map<String, Type> convert = Collections.singletonMap(fieldName, type);
|
||||
Processor processor = new ConvertProcessor(convert);
|
||||
Processor processor = new ConvertProcessor(fieldName, type);
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
fail("processor execute should have failed");
|
||||
|
@ -326,8 +254,7 @@ public class ConvertProcessorTests extends ESTestCase {
|
|||
public void testConvertNullField() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("field", null));
|
||||
Type type = randomFrom(Type.values());
|
||||
Map<String, Type> convert = Collections.singletonMap("field", type);
|
||||
Processor processor = new ConvertProcessor(convert);
|
||||
Processor processor = new ConvertProcessor("field", type);
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
fail("processor execute should have failed");
|
||||
|
|
|
@ -33,80 +33,51 @@ public class GsubProcessorFactoryTests extends ESTestCase {
|
|||
public void testCreate() throws Exception {
|
||||
GsubProcessor.Factory factory = new GsubProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
List<Map<String, String>> expressions = new ArrayList<>();
|
||||
Map<String, String> expression = new HashMap<>();
|
||||
expression.put("field", "field1");
|
||||
expression.put("pattern", "\\.");
|
||||
expression.put("replacement", "-");
|
||||
expressions.add(expression);
|
||||
config.put("expressions", expressions);
|
||||
config.put("field", "field1");
|
||||
config.put("pattern", "\\.");
|
||||
config.put("replacement", "-");
|
||||
GsubProcessor gsubProcessor = factory.create(config);
|
||||
assertThat(gsubProcessor.getGsubExpressions().size(), equalTo(1));
|
||||
GsubExpression gsubExpression = gsubProcessor.getGsubExpressions().get(0);
|
||||
assertThat(gsubExpression.getFieldName(), equalTo("field1"));
|
||||
assertThat(gsubExpression.getPattern().toString(), equalTo("\\."));
|
||||
assertThat(gsubExpression.getReplacement(), equalTo("-"));
|
||||
}
|
||||
|
||||
public void testCreateMissingExpressions() throws Exception {
|
||||
GsubProcessor.Factory factory = new GsubProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("required property [expressions] is missing"));
|
||||
}
|
||||
assertThat(gsubProcessor.getField(), equalTo("field1"));
|
||||
assertThat(gsubProcessor.getPattern().toString(), equalTo("\\."));
|
||||
assertThat(gsubProcessor.getReplacement(), equalTo("-"));
|
||||
}
|
||||
|
||||
public void testCreateNoFieldPresent() throws Exception {
|
||||
GsubProcessor.Factory factory = new GsubProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
List<Map<String, String>> expressions = new ArrayList<>();
|
||||
Map<String, String> expression = new HashMap<>();
|
||||
expression.put("pattern", "\\.");
|
||||
expression.put("replacement", "-");
|
||||
expressions.add(expression);
|
||||
config.put("expressions", expressions);
|
||||
config.put("pattern", "\\.");
|
||||
config.put("replacement", "-");
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("no [field] specified for gsub expression"));
|
||||
assertThat(e.getMessage(), equalTo("required property [field] is missing"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testCreateNoPatternPresent() throws Exception {
|
||||
GsubProcessor.Factory factory = new GsubProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
List<Map<String, String>> expressions = new ArrayList<>();
|
||||
Map<String, String> expression = new HashMap<>();
|
||||
expression.put("field", "field1");
|
||||
expression.put("replacement", "-");
|
||||
expressions.add(expression);
|
||||
config.put("expressions", expressions);
|
||||
config.put("field", "field1");
|
||||
config.put("replacement", "-");
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("no [pattern] specified for gsub expression"));
|
||||
assertThat(e.getMessage(), equalTo("required property [pattern] is missing"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testCreateNoReplacementPresent() throws Exception {
|
||||
GsubProcessor.Factory factory = new GsubProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
List<Map<String, String>> expressions = new ArrayList<>();
|
||||
Map<String, String> expression = new HashMap<>();
|
||||
expression.put("field", "field1");
|
||||
expression.put("pattern", "\\.");
|
||||
expressions.add(expression);
|
||||
config.put("expressions", expressions);
|
||||
config.put("field", "field1");
|
||||
config.put("pattern", "\\.");
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("no [replacement] specified for gsub expression"));
|
||||
assertThat(e.getMessage(), equalTo("required property [replacement] is missing"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,25 +37,17 @@ public class GsubProcessorTests extends ESTestCase {
|
|||
|
||||
public void testGsub() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||
int numFields = randomIntBetween(1, 5);
|
||||
List<GsubExpression> expressions = new ArrayList<>();
|
||||
for (int i = 0; i < numFields; i++) {
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "127.0.0.1");
|
||||
expressions.add(new GsubExpression(fieldName, Pattern.compile("\\."), "-"));
|
||||
}
|
||||
Processor processor = new GsubProcessor(expressions);
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "127.0.0.1");
|
||||
Processor processor = new GsubProcessor(fieldName, Pattern.compile("\\."), "-");
|
||||
processor.execute(ingestDocument);
|
||||
for (GsubExpression expression : expressions) {
|
||||
assertThat(ingestDocument.getFieldValue(expression.getFieldName(), String.class), equalTo("127-0-0-1"));
|
||||
}
|
||||
assertThat(ingestDocument.getFieldValue(fieldName, String.class), equalTo("127-0-0-1"));
|
||||
}
|
||||
|
||||
public void testGsubNotAStringValue() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
|
||||
String fieldName = RandomDocumentPicks.randomFieldName(random());
|
||||
ingestDocument.setFieldValue(fieldName, 123);
|
||||
List<GsubExpression> gsubExpressions = Collections.singletonList(new GsubExpression(fieldName, Pattern.compile("\\."), "-"));
|
||||
Processor processor = new GsubProcessor(gsubExpressions);
|
||||
Processor processor = new GsubProcessor(fieldName, Pattern.compile("\\."), "-");
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
fail("processor execution should have failed");
|
||||
|
@ -67,8 +59,7 @@ public class GsubProcessorTests extends ESTestCase {
|
|||
public void testGsubFieldNotFound() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
|
||||
String fieldName = RandomDocumentPicks.randomFieldName(random());
|
||||
List<GsubExpression> gsubExpressions = Collections.singletonList(new GsubExpression(fieldName, Pattern.compile("\\."), "-"));
|
||||
Processor processor = new GsubProcessor(gsubExpressions);
|
||||
Processor processor = new GsubProcessor(fieldName, Pattern.compile("\\."), "-");
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
fail("processor execution should have failed");
|
||||
|
@ -79,8 +70,7 @@ public class GsubProcessorTests extends ESTestCase {
|
|||
|
||||
public void testGsubNullValue() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("field", null));
|
||||
List<GsubExpression> gsubExpressions = Collections.singletonList(new GsubExpression("field", Pattern.compile("\\."), "-"));
|
||||
Processor processor = new GsubProcessor(gsubExpressions);
|
||||
Processor processor = new GsubProcessor("field", Pattern.compile("\\."), "-");
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
fail("processor execution should have failed");
|
||||
|
|
|
@ -21,7 +21,6 @@ package org.elasticsearch.ingest.processor.join;
|
|||
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -32,20 +31,34 @@ public class JoinProcessorFactoryTests extends ESTestCase {
|
|||
public void testCreate() throws Exception {
|
||||
JoinProcessor.Factory factory = new JoinProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
Map<String, String> fields = Collections.singletonMap("field1", "-");
|
||||
config.put("fields", fields);
|
||||
config.put("field", "field1");
|
||||
config.put("separator", "-");
|
||||
JoinProcessor joinProcessor = factory.create(config);
|
||||
assertThat(joinProcessor.getFields(), equalTo(fields));
|
||||
assertThat(joinProcessor.getField(), equalTo("field1"));
|
||||
assertThat(joinProcessor.getSeparator(), equalTo("-"));
|
||||
}
|
||||
|
||||
public void testCreateMissingFields() throws Exception {
|
||||
public void testCreateNoFieldPresent() throws Exception {
|
||||
JoinProcessor.Factory factory = new JoinProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
config.put("separator", "-");
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("required property [fields] is missing"));
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("required property [field] is missing"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testCreateNoSeparatorPresent() throws Exception {
|
||||
JoinProcessor.Factory factory = new JoinProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
config.put("field", "field1");
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("required property [separator] is missing"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,68 +35,49 @@ public class JoinProcessorTests extends ESTestCase {
|
|||
|
||||
public void testJoinStrings() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||
Map<String, String> fields = new HashMap<>();
|
||||
Map<String, String> expectedResultMap = new HashMap<>();
|
||||
int numFields = randomIntBetween(1, 5);
|
||||
for (int i = 0; i < numFields; i++) {
|
||||
int numItems = randomIntBetween(1, 10);
|
||||
String separator = randomFrom(SEPARATORS);
|
||||
List<String> fieldValue = new ArrayList<>(numItems);
|
||||
String expectedResult = "";
|
||||
for (int j = 0; j < numItems; j++) {
|
||||
String value = randomAsciiOfLengthBetween(1, 10);
|
||||
fieldValue.add(value);
|
||||
expectedResult += value;
|
||||
if (j < numItems - 1) {
|
||||
expectedResult += separator;
|
||||
}
|
||||
int numItems = randomIntBetween(1, 10);
|
||||
String separator = randomFrom(SEPARATORS);
|
||||
List<String> fieldValue = new ArrayList<>(numItems);
|
||||
String expectedResult = "";
|
||||
for (int j = 0; j < numItems; j++) {
|
||||
String value = randomAsciiOfLengthBetween(1, 10);
|
||||
fieldValue.add(value);
|
||||
expectedResult += value;
|
||||
if (j < numItems - 1) {
|
||||
expectedResult += separator;
|
||||
}
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
|
||||
expectedResultMap.put(fieldName, expectedResult);
|
||||
fields.put(fieldName, separator);
|
||||
}
|
||||
Processor processor = new JoinProcessor(fields);
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
|
||||
Processor processor = new JoinProcessor(fieldName, separator);
|
||||
processor.execute(ingestDocument);
|
||||
for (Map.Entry<String, String> entry : expectedResultMap.entrySet()) {
|
||||
assertThat(ingestDocument.getFieldValue(entry.getKey(), String.class), equalTo(entry.getValue()));
|
||||
}
|
||||
assertThat(ingestDocument.getFieldValue(fieldName, String.class), equalTo(expectedResult));
|
||||
}
|
||||
|
||||
public void testJoinIntegers() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||
Map<String, String> fields = new HashMap<>();
|
||||
Map<String, String> expectedResultMap = new HashMap<>();
|
||||
int numFields = randomIntBetween(1, 5);
|
||||
for (int i = 0; i < numFields; i++) {
|
||||
int numItems = randomIntBetween(1, 10);
|
||||
String separator = randomFrom(SEPARATORS);
|
||||
List<Integer> fieldValue = new ArrayList<>(numItems);
|
||||
String expectedResult = "";
|
||||
for (int j = 0; j < numItems; j++) {
|
||||
int value = randomInt();
|
||||
fieldValue.add(value);
|
||||
expectedResult += value;
|
||||
if (j < numItems - 1) {
|
||||
expectedResult += separator;
|
||||
}
|
||||
int numItems = randomIntBetween(1, 10);
|
||||
String separator = randomFrom(SEPARATORS);
|
||||
List<Integer> fieldValue = new ArrayList<>(numItems);
|
||||
String expectedResult = "";
|
||||
for (int j = 0; j < numItems; j++) {
|
||||
int value = randomInt();
|
||||
fieldValue.add(value);
|
||||
expectedResult += value;
|
||||
if (j < numItems - 1) {
|
||||
expectedResult += separator;
|
||||
}
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
|
||||
expectedResultMap.put(fieldName, expectedResult);
|
||||
fields.put(fieldName, separator);
|
||||
}
|
||||
Processor processor = new JoinProcessor(fields);
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
|
||||
Processor processor = new JoinProcessor(fieldName, separator);
|
||||
processor.execute(ingestDocument);
|
||||
for (Map.Entry<String, String> entry : expectedResultMap.entrySet()) {
|
||||
assertThat(ingestDocument.getFieldValue(entry.getKey(), String.class), equalTo(entry.getValue()));
|
||||
}
|
||||
assertThat(ingestDocument.getFieldValue(fieldName, String.class), equalTo(expectedResult));
|
||||
}
|
||||
|
||||
public void testJoinNonListField() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
|
||||
String fieldName = RandomDocumentPicks.randomFieldName(random());
|
||||
ingestDocument.setFieldValue(fieldName, randomAsciiOfLengthBetween(1, 10));
|
||||
Map<String, String> join = Collections.singletonMap(fieldName, "-");
|
||||
Processor processor = new JoinProcessor(join);
|
||||
Processor processor = new JoinProcessor(fieldName, "-");
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
} catch(IllegalArgumentException e) {
|
||||
|
@ -107,7 +88,7 @@ public class JoinProcessorTests extends ESTestCase {
|
|||
public void testJoinNonExistingField() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
|
||||
String fieldName = RandomDocumentPicks.randomFieldName(random());
|
||||
Processor processor = new JoinProcessor(Collections.singletonMap(fieldName, "-"));
|
||||
Processor processor = new JoinProcessor(fieldName, "-");
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
} catch(IllegalArgumentException e) {
|
||||
|
@ -117,7 +98,7 @@ public class JoinProcessorTests extends ESTestCase {
|
|||
|
||||
public void testJoinNullValue() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("field", null));
|
||||
Processor processor = new JoinProcessor(Collections.singletonMap("field", "-"));
|
||||
Processor processor = new JoinProcessor("field", "-");
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
} catch(IllegalArgumentException e) {
|
||||
|
|
|
@ -33,20 +33,19 @@ public class LowercaseProcessorFactoryTests extends ESTestCase {
|
|||
public void testCreate() throws Exception {
|
||||
LowercaseProcessor.Factory factory = new LowercaseProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
List<String> fields = Collections.singletonList("field1");
|
||||
config.put("fields", fields);
|
||||
config.put("field", "field1");
|
||||
LowercaseProcessor uppercaseProcessor = factory.create(config);
|
||||
assertThat(uppercaseProcessor.getFields(), equalTo(fields));
|
||||
assertThat(uppercaseProcessor.getField(), equalTo("field1"));
|
||||
}
|
||||
|
||||
public void testCreateMissingFields() throws Exception {
|
||||
public void testCreateMissingField() throws Exception {
|
||||
LowercaseProcessor.Factory factory = new LowercaseProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("required property [fields] is missing"));
|
||||
assertThat(e.getMessage(), equalTo("required property [field] is missing"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,8 +27,8 @@ import java.util.Locale;
|
|||
|
||||
public class LowercaseProcessorTests extends AbstractStringProcessorTestCase {
|
||||
@Override
|
||||
protected AbstractStringProcessor newProcessor(Collection<String> fields) {
|
||||
return new LowercaseProcessor(fields);
|
||||
protected AbstractStringProcessor newProcessor(String field) {
|
||||
return new LowercaseProcessor(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -33,20 +33,19 @@ public class RemoveProcessorFactoryTests extends ESTestCase {
|
|||
public void testCreate() throws Exception {
|
||||
RemoveProcessor.Factory factory = new RemoveProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
List<String> fields = Collections.singletonList("field1");
|
||||
config.put("fields", fields);
|
||||
config.put("field", "field1");
|
||||
RemoveProcessor removeProcessor = factory.create(config);
|
||||
assertThat(removeProcessor.getFields(), equalTo(fields));
|
||||
assertThat(removeProcessor.getField(), equalTo("field1"));
|
||||
}
|
||||
|
||||
public void testCreateMissingFields() throws Exception {
|
||||
public void testCreateMissingField() throws Exception {
|
||||
RemoveProcessor.Factory factory = new RemoveProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("required property [fields] is missing"));
|
||||
assertThat(e.getMessage(), equalTo("required property [field] is missing"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,22 +36,16 @@ public class RemoveProcessorTests extends ESTestCase {
|
|||
|
||||
public void testRemoveFields() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||
int numFields = randomIntBetween(1, 5);
|
||||
Set<String> fields = new HashSet<>();
|
||||
for (int i = 0; i < numFields; i++) {
|
||||
fields.add(RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument));
|
||||
}
|
||||
Processor processor = new RemoveProcessor(fields);
|
||||
String field = RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument);
|
||||
Processor processor = new RemoveProcessor(field);
|
||||
processor.execute(ingestDocument);
|
||||
for (String field : fields) {
|
||||
assertThat(ingestDocument.hasField(field), equalTo(false));
|
||||
}
|
||||
assertThat(ingestDocument.hasField(field), equalTo(false));
|
||||
}
|
||||
|
||||
public void testRemoveNonExistingField() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
|
||||
String fieldName = RandomDocumentPicks.randomFieldName(random());
|
||||
Processor processor = new RemoveProcessor(Collections.singletonList(fieldName));
|
||||
Processor processor = new RemoveProcessor(fieldName);
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
fail("remove field should have failed");
|
||||
|
|
|
@ -32,20 +32,34 @@ public class RenameProcessorFactoryTests extends ESTestCase {
|
|||
public void testCreate() throws Exception {
|
||||
RenameProcessor.Factory factory = new RenameProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
Map<String, String> fields = Collections.singletonMap("field1", "value1");
|
||||
config.put("fields", fields);
|
||||
config.put("field", "old_field");
|
||||
config.put("to", "new_field");
|
||||
RenameProcessor renameProcessor = factory.create(config);
|
||||
assertThat(renameProcessor.getFields(), equalTo(fields));
|
||||
assertThat(renameProcessor.getOldFieldName(), equalTo("old_field"));
|
||||
assertThat(renameProcessor.getNewFieldName(), equalTo("new_field"));
|
||||
}
|
||||
|
||||
public void testCreateMissingFields() throws Exception {
|
||||
public void testCreateNoFieldPresent() throws Exception {
|
||||
RenameProcessor.Factory factory = new RenameProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
config.put("to", "new_field");
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("required property [fields] is missing"));
|
||||
assertThat(e.getMessage(), equalTo("required property [field] is missing"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testCreateNoToPresent() throws Exception {
|
||||
RenameProcessor.Factory factory = new RenameProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
config.put("field", "old_field");
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("required property [to] is missing"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,26 +34,15 @@ public class RenameProcessorTests extends ESTestCase {
|
|||
|
||||
public void testRename() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||
int numFields = randomIntBetween(1, 5);
|
||||
Map<String, String> fields = new HashMap<>();
|
||||
Map<String, Object> newFields = new HashMap<>();
|
||||
for (int i = 0; i < numFields; i++) {
|
||||
String fieldName = RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument);
|
||||
if (fields.containsKey(fieldName)) {
|
||||
continue;
|
||||
}
|
||||
String newFieldName;
|
||||
do {
|
||||
newFieldName = RandomDocumentPicks.randomFieldName(random());
|
||||
} while (RandomDocumentPicks.canAddField(newFieldName, ingestDocument) == false || newFields.containsKey(newFieldName));
|
||||
newFields.put(newFieldName, ingestDocument.getFieldValue(fieldName, Object.class));
|
||||
fields.put(fieldName, newFieldName);
|
||||
}
|
||||
Processor processor = new RenameProcessor(fields);
|
||||
String fieldName = RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument);
|
||||
Object fieldValue = ingestDocument.getFieldValue(fieldName, Object.class);
|
||||
String newFieldName;
|
||||
do {
|
||||
newFieldName = RandomDocumentPicks.randomFieldName(random());
|
||||
} while (RandomDocumentPicks.canAddField(newFieldName, ingestDocument) == false || newFieldName.equals(fieldName));
|
||||
Processor processor = new RenameProcessor(fieldName, newFieldName);
|
||||
processor.execute(ingestDocument);
|
||||
for (Map.Entry<String, Object> entry : newFields.entrySet()) {
|
||||
assertThat(ingestDocument.getFieldValue(entry.getKey(), Object.class), equalTo(entry.getValue()));
|
||||
}
|
||||
assertThat(ingestDocument.getFieldValue(newFieldName, Object.class), equalTo(fieldValue));
|
||||
}
|
||||
|
||||
public void testRenameArrayElement() throws Exception {
|
||||
|
@ -69,7 +58,7 @@ public class RenameProcessorTests extends ESTestCase {
|
|||
document.put("one", one);
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
|
||||
|
||||
Processor processor = new RenameProcessor(Collections.singletonMap("list.0", "item"));
|
||||
Processor processor = new RenameProcessor("list.0", "item");
|
||||
processor.execute(ingestDocument);
|
||||
Object actualObject = ingestDocument.getSource().get("list");
|
||||
assertThat(actualObject, instanceOf(List.class));
|
||||
|
@ -82,7 +71,7 @@ public class RenameProcessorTests extends ESTestCase {
|
|||
assertThat(actualObject, instanceOf(String.class));
|
||||
assertThat(actualObject, equalTo("item1"));
|
||||
|
||||
processor = new RenameProcessor(Collections.singletonMap("list.0", "list.3"));
|
||||
processor = new RenameProcessor("list.0", "list.3");
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
fail("processor execute should have failed");
|
||||
|
@ -97,7 +86,7 @@ public class RenameProcessorTests extends ESTestCase {
|
|||
public void testRenameNonExistingField() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
|
||||
String fieldName = RandomDocumentPicks.randomFieldName(random());
|
||||
Processor processor = new RenameProcessor(Collections.singletonMap(fieldName, RandomDocumentPicks.randomFieldName(random())));
|
||||
Processor processor = new RenameProcessor(fieldName, RandomDocumentPicks.randomFieldName(random()));
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
fail("processor execute should have failed");
|
||||
|
@ -109,7 +98,7 @@ public class RenameProcessorTests extends ESTestCase {
|
|||
public void testRenameNewFieldAlreadyExists() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||
String fieldName = RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument);
|
||||
Processor processor = new RenameProcessor(Collections.singletonMap(RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument), fieldName));
|
||||
Processor processor = new RenameProcessor(RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument), fieldName);
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
fail("processor execute should have failed");
|
||||
|
@ -123,7 +112,7 @@ public class RenameProcessorTests extends ESTestCase {
|
|||
String fieldName = RandomDocumentPicks.randomFieldName(random());
|
||||
ingestDocument.setFieldValue(fieldName, null);
|
||||
String newFieldName = RandomDocumentPicks.randomFieldName(random());
|
||||
Processor processor = new RenameProcessor(Collections.singletonMap(fieldName, newFieldName));
|
||||
Processor processor = new RenameProcessor(fieldName, newFieldName);
|
||||
processor.execute(ingestDocument);
|
||||
assertThat(ingestDocument.hasField(fieldName), equalTo(false));
|
||||
assertThat(ingestDocument.hasField(newFieldName), equalTo(true));
|
||||
|
@ -144,7 +133,7 @@ public class RenameProcessorTests extends ESTestCase {
|
|||
document.put("list", Collections.singletonList("item"));
|
||||
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
|
||||
Processor processor = new RenameProcessor(Collections.singletonMap("list", "new_field"));
|
||||
Processor processor = new RenameProcessor("list", "new_field");
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
fail("processor execute should have failed");
|
||||
|
@ -169,7 +158,7 @@ public class RenameProcessorTests extends ESTestCase {
|
|||
document.put("list", Collections.singletonList("item"));
|
||||
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
|
||||
Processor processor = new RenameProcessor(Collections.singletonMap("list", "new_field"));
|
||||
Processor processor = new RenameProcessor("list", "new_field");
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
fail("processor execute should have failed");
|
||||
|
|
|
@ -32,20 +32,47 @@ public class SetProcessorFactoryTests extends ESTestCase {
|
|||
public void testCreate() throws Exception {
|
||||
SetProcessor.Factory factory = new SetProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
Map<String, String> fields = Collections.singletonMap("field1", "value1");
|
||||
config.put("fields", fields);
|
||||
config.put("field", "field1");
|
||||
config.put("value", "value1");
|
||||
SetProcessor setProcessor = factory.create(config);
|
||||
assertThat(setProcessor.getFields(), equalTo(fields));
|
||||
assertThat(setProcessor.getField(), equalTo("field1"));
|
||||
assertThat(setProcessor.getValue(), equalTo("value1"));
|
||||
}
|
||||
|
||||
public void testCreateMissingFields() throws Exception {
|
||||
public void testCreateNoFieldPresent() throws Exception {
|
||||
SetProcessor.Factory factory = new SetProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
config.put("value", "value1");
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("required property [fields] is missing"));
|
||||
assertThat(e.getMessage(), equalTo("required property [field] is missing"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testCreateNoValuePresent() throws Exception {
|
||||
SetProcessor.Factory factory = new SetProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
config.put("field", "field1");
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("required property [value] is missing"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testCreateNullValue() throws Exception {
|
||||
SetProcessor.Factory factory = new SetProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
config.put("field", "field1");
|
||||
config.put("value", null);
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("required property [value] is missing"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,45 +32,30 @@ public class SetProcessorTests extends ESTestCase {
|
|||
|
||||
public void testSetExistingFields() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||
int numFields = randomIntBetween(1, 5);
|
||||
Map<String, Object> fields = new HashMap<>();
|
||||
for (int i = 0; i < numFields; i++) {
|
||||
String fieldName = RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument);
|
||||
Object fieldValue = RandomDocumentPicks.randomFieldValue(random());
|
||||
fields.put(fieldName, fieldValue);
|
||||
}
|
||||
Processor processor = new SetProcessor(fields);
|
||||
String fieldName = RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument);
|
||||
Object fieldValue = RandomDocumentPicks.randomFieldValue(random());
|
||||
Processor processor = new SetProcessor(fieldName, fieldValue);
|
||||
processor.execute(ingestDocument);
|
||||
|
||||
for (Map.Entry<String, Object> field : fields.entrySet()) {
|
||||
assertThat(ingestDocument.hasField(field.getKey()), equalTo(true));
|
||||
assertThat(ingestDocument.getFieldValue(field.getKey(), Object.class), equalTo(field.getValue()));
|
||||
}
|
||||
assertThat(ingestDocument.hasField(fieldName), equalTo(true));
|
||||
assertThat(ingestDocument.getFieldValue(fieldName, Object.class), equalTo(fieldValue));
|
||||
}
|
||||
|
||||
public void testSetNewFields() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
|
||||
//used to verify that there are no conflicts between subsequent fields going to be added
|
||||
IngestDocument testIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
|
||||
int numFields = randomIntBetween(1, 5);
|
||||
Map<String, Object> fields = new HashMap<>();
|
||||
for (int i = 0; i < numFields; i++) {
|
||||
Object fieldValue = RandomDocumentPicks.randomFieldValue(random());
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), testIngestDocument, fieldValue);
|
||||
fields.put(fieldName, fieldValue);
|
||||
}
|
||||
Processor processor = new SetProcessor(fields);
|
||||
Object fieldValue = RandomDocumentPicks.randomFieldValue(random());
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), testIngestDocument, fieldValue);
|
||||
Processor processor = new SetProcessor(fieldName, fieldValue);
|
||||
processor.execute(ingestDocument);
|
||||
for (Map.Entry<String, Object> field : fields.entrySet()) {
|
||||
assertThat(ingestDocument.hasField(field.getKey()), equalTo(true));
|
||||
assertThat(ingestDocument.getFieldValue(field.getKey(), Object.class), equalTo(field.getValue()));
|
||||
}
|
||||
assertThat(ingestDocument.hasField(fieldName), equalTo(true));
|
||||
assertThat(ingestDocument.getFieldValue(fieldName, Object.class), equalTo(fieldValue));
|
||||
}
|
||||
|
||||
public void testSetFieldsTypeMismatch() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
|
||||
ingestDocument.setFieldValue("field", "value");
|
||||
Processor processor = new SetProcessor(Collections.singletonMap("field.inner", "value"));
|
||||
Processor processor = new SetProcessor("field.inner", "value");
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
fail("processor execute should have failed");
|
||||
|
|
|
@ -32,20 +32,34 @@ public class SplitProcessorFactoryTests extends ESTestCase {
|
|||
public void testCreate() throws Exception {
|
||||
SplitProcessor.Factory factory = new SplitProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
Map<String, String> fields = Collections.singletonMap("field1", "\\.");
|
||||
config.put("fields", fields);
|
||||
config.put("field", "field1");
|
||||
config.put("separator", "\\.");
|
||||
SplitProcessor splitProcessor = factory.create(config);
|
||||
assertThat(splitProcessor.getFields(), equalTo(fields));
|
||||
assertThat(splitProcessor.getField(), equalTo("field1"));
|
||||
assertThat(splitProcessor.getSeparator(), equalTo("\\."));
|
||||
}
|
||||
|
||||
public void testCreateMissingFields() throws Exception {
|
||||
public void testCreateNoFieldPresent() throws Exception {
|
||||
SplitProcessor.Factory factory = new SplitProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
config.put("separator", "\\.");
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("required property [fields] is missing"));
|
||||
assertThat(e.getMessage(), equalTo("required property [field] is missing"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testCreateNoSeparatorPresent() throws Exception {
|
||||
SplitProcessor.Factory factory = new SplitProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
config.put("field", "field1");
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("required property [separator] is missing"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,24 +33,16 @@ public class SplitProcessorTests extends ESTestCase {
|
|||
|
||||
public void testSplit() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||
Map<String, String> fields = new HashMap<>();
|
||||
int numFields = randomIntBetween(1, 5);
|
||||
for (int i = 0; i < numFields; i++) {
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "127.0.0.1");
|
||||
fields.put(fieldName, "\\.");
|
||||
}
|
||||
Processor processor = new SplitProcessor(fields);
|
||||
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "127.0.0.1");
|
||||
Processor processor = new SplitProcessor(fieldName, "\\.");
|
||||
processor.execute(ingestDocument);
|
||||
for (String field : fields.keySet()) {
|
||||
assertThat(ingestDocument.getFieldValue(field, List.class), equalTo(Arrays.asList("127", "0", "0", "1")));
|
||||
}
|
||||
assertThat(ingestDocument.getFieldValue(fieldName, List.class), equalTo(Arrays.asList("127", "0", "0", "1")));
|
||||
}
|
||||
|
||||
public void testSplitFieldNotFound() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
|
||||
String fieldName = RandomDocumentPicks.randomFieldName(random());
|
||||
Map<String, String> split = Collections.singletonMap(fieldName, "\\.");
|
||||
Processor processor = new SplitProcessor(split);
|
||||
Processor processor = new SplitProcessor(fieldName, "\\.");
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
fail("split processor should have failed");
|
||||
|
@ -61,8 +53,7 @@ public class SplitProcessorTests extends ESTestCase {
|
|||
|
||||
public void testSplitNullValue() throws Exception {
|
||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("field", null));
|
||||
Map<String, String> split = Collections.singletonMap("field", "\\.");
|
||||
Processor processor = new SplitProcessor(split);
|
||||
Processor processor = new SplitProcessor("field", "\\.");
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
fail("split processor should have failed");
|
||||
|
@ -75,7 +66,7 @@ public class SplitProcessorTests extends ESTestCase {
|
|||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
|
||||
String fieldName = RandomDocumentPicks.randomFieldName(random());
|
||||
ingestDocument.setFieldValue(fieldName, randomInt());
|
||||
Processor processor = new SplitProcessor(Collections.singletonMap(fieldName, "\\."));
|
||||
Processor processor = new SplitProcessor(fieldName, "\\.");
|
||||
try {
|
||||
processor.execute(ingestDocument);
|
||||
fail("split processor should have failed");
|
||||
|
|
|
@ -33,20 +33,19 @@ public class TrimProcessorFactoryTests extends ESTestCase {
|
|||
public void testCreate() throws Exception {
|
||||
TrimProcessor.Factory factory = new TrimProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
List<String> fields = Collections.singletonList("field1");
|
||||
config.put("fields", fields);
|
||||
config.put("field", "field1");
|
||||
TrimProcessor uppercaseProcessor = factory.create(config);
|
||||
assertThat(uppercaseProcessor.getFields(), equalTo(fields));
|
||||
assertThat(uppercaseProcessor.getField(), equalTo("field1"));
|
||||
}
|
||||
|
||||
public void testCreateMissingFields() throws Exception {
|
||||
public void testCreateMissingField() throws Exception {
|
||||
TrimProcessor.Factory factory = new TrimProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("required property [fields] is missing"));
|
||||
assertThat(e.getMessage(), equalTo("required property [field] is missing"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,13 +22,11 @@ package org.elasticsearch.ingest.processor.trim;
|
|||
import org.elasticsearch.ingest.processor.AbstractStringProcessor;
|
||||
import org.elasticsearch.ingest.processor.AbstractStringProcessorTestCase;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class TrimProcessorTests extends AbstractStringProcessorTestCase {
|
||||
|
||||
@Override
|
||||
protected AbstractStringProcessor newProcessor(Collection<String> fields) {
|
||||
return new TrimProcessor(fields);
|
||||
protected AbstractStringProcessor newProcessor(String field) {
|
||||
return new TrimProcessor(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -33,20 +33,19 @@ public class UppercaseProcessorFactoryTests extends ESTestCase {
|
|||
public void testCreate() throws Exception {
|
||||
UppercaseProcessor.Factory factory = new UppercaseProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
List<String> fields = Collections.singletonList("field1");
|
||||
config.put("fields", fields);
|
||||
config.put("field", "field1");
|
||||
UppercaseProcessor uppercaseProcessor = factory.create(config);
|
||||
assertThat(uppercaseProcessor.getFields(), equalTo(fields));
|
||||
assertThat(uppercaseProcessor.getField(), equalTo("field1"));
|
||||
}
|
||||
|
||||
public void testCreateMissingFields() throws Exception {
|
||||
public void testCreateMissingField() throws Exception {
|
||||
UppercaseProcessor.Factory factory = new UppercaseProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
try {
|
||||
factory.create(config);
|
||||
fail("factory create should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("required property [fields] is missing"));
|
||||
assertThat(e.getMessage(), equalTo("required property [field] is missing"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,8 +28,8 @@ import java.util.Locale;
|
|||
public class UppercaseProcessorTests extends AbstractStringProcessorTestCase {
|
||||
|
||||
@Override
|
||||
protected AbstractStringProcessor newProcessor(Collection<String> fields) {
|
||||
return new UppercaseProcessor(fields);
|
||||
protected AbstractStringProcessor newProcessor(String field) {
|
||||
return new UppercaseProcessor(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -13,9 +13,8 @@
|
|||
"processors": [
|
||||
{
|
||||
"set" : {
|
||||
"fields" : {
|
||||
"field2": "_value"
|
||||
}
|
||||
"field" : "field2",
|
||||
"value": "_value"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
|
|
@ -13,76 +13,59 @@
|
|||
"processors": [
|
||||
{
|
||||
"set" : {
|
||||
"fields" : {
|
||||
"new_field": "new_value"
|
||||
}
|
||||
"field" : "new_field",
|
||||
"value": "new_value"
|
||||
}
|
||||
},
|
||||
{
|
||||
"rename" : {
|
||||
"fields" : {
|
||||
"field_to_rename": "renamed_field"
|
||||
}
|
||||
"field" : "field_to_rename",
|
||||
"to": "renamed_field"
|
||||
}
|
||||
},
|
||||
{
|
||||
"remove" : {
|
||||
"fields" : [
|
||||
"field_to_remove"
|
||||
]
|
||||
"field" : "field_to_remove"
|
||||
}
|
||||
},
|
||||
{
|
||||
"lowercase" : {
|
||||
"fields" : [
|
||||
"field_to_lowercase"
|
||||
]
|
||||
"field" : "field_to_lowercase"
|
||||
}
|
||||
},
|
||||
{
|
||||
"uppercase" : {
|
||||
"fields" : [
|
||||
"field_to_uppercase"
|
||||
]
|
||||
"field" : "field_to_uppercase"
|
||||
}
|
||||
},
|
||||
{
|
||||
"trim" : {
|
||||
"fields" : [
|
||||
"field_to_trim"
|
||||
]
|
||||
"field" : "field_to_trim"
|
||||
}
|
||||
},
|
||||
{
|
||||
"split" : {
|
||||
"fields" : {
|
||||
"field_to_split": "-"
|
||||
}
|
||||
"field" : "field_to_split",
|
||||
"separator": "-"
|
||||
}
|
||||
},
|
||||
{
|
||||
"join" : {
|
||||
"fields" : {
|
||||
"field_to_join": "-"
|
||||
}
|
||||
"field" : "field_to_join",
|
||||
"separator": "-"
|
||||
}
|
||||
},
|
||||
{
|
||||
"convert" : {
|
||||
"fields" : {
|
||||
"field_to_convert": "integer"
|
||||
}
|
||||
"field" : "field_to_convert",
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
{
|
||||
"gsub" : {
|
||||
"expressions" : [
|
||||
{
|
||||
"field": "field_to_gsub",
|
||||
"pattern" : "-",
|
||||
"replacement" : "."
|
||||
}
|
||||
]
|
||||
"field": "field_to_gsub",
|
||||
"pattern" : "-",
|
||||
"replacement" : "."
|
||||
}
|
||||
}
|
||||
]
|
||||
|
|
|
@ -13,9 +13,8 @@
|
|||
"processors": [
|
||||
{
|
||||
"set" : {
|
||||
"fields" : {
|
||||
"field2" : "_value"
|
||||
}
|
||||
"field" : "field2",
|
||||
"value" : "_value"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
@ -67,9 +66,8 @@
|
|||
"processors": [
|
||||
{
|
||||
"set" : {
|
||||
"fields" : {
|
||||
"field2" : "_value"
|
||||
}
|
||||
"field" : "field2",
|
||||
"value" : "_value"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
@ -130,16 +128,14 @@
|
|||
"processors": [
|
||||
{
|
||||
"set" : {
|
||||
"fields" : {
|
||||
"field2" : "_value"
|
||||
}
|
||||
"field" : "field2",
|
||||
"value" : "_value"
|
||||
}
|
||||
},
|
||||
{
|
||||
"set" : {
|
||||
"fields" : {
|
||||
"field3" : "third_val"
|
||||
}
|
||||
"field" : "field3",
|
||||
"value" : "third_val"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
@ -182,7 +178,7 @@
|
|||
"processors": [
|
||||
{
|
||||
"uppercase" : {
|
||||
"fields" : ["foo"]
|
||||
"field" : "foo"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
@ -227,14 +223,13 @@
|
|||
"processors": [
|
||||
{
|
||||
"convert" : {
|
||||
"fields" : {
|
||||
"foo": "integer"
|
||||
}
|
||||
"field" : "foo",
|
||||
"type" : "integer"
|
||||
}
|
||||
},
|
||||
{
|
||||
"uppercase" : {
|
||||
"fields" : ["bar"]
|
||||
"field" : "bar"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue