Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions docs/reference/ingest/ingest-node.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -1489,10 +1489,12 @@ If the field is not a string, the processor will throw an exception.
.Gsub Options
[options="header"]
|======
| Name | Required | Default | Description
| `field` | yes | - | The field to apply the replacement to
| `pattern` | yes | - | The pattern to be replaced
| `replacement` | yes | - | The string to replace the matching patterns with
| Name | Required | Default | Description
| `field` | yes | - | The field to apply the replacement to
| `pattern` | yes | - | The pattern to be replaced
| `replacement` | yes | - | The string to replace the matching patterns with
| `target_field` | no | `field` | The field to assign the converted value to, by default `field` is updated in-place
| `ignore_missing` | no | `false` | If `true` and `field` does not exist or is `null`, the processor quietly exits without modifying the document
|======

[source,js]
Expand All @@ -1516,9 +1518,10 @@ Throws an error when the field is not an array.
.Join Options
[options="header"]
|======
| Name | Required | Default | Description
| `field` | yes | - | The field to be separated
| `separator` | yes | - | The separator character
| Name | Required | Default | Description
| `field` | yes | - | The field to be separated
| `separator` | yes | - | The separator character
| `target_field` | no | `field` | The field to assign the joined value to, by default `field` is updated in-place
|======

[source,js]
Expand Down Expand Up @@ -1662,6 +1665,7 @@ Converts a string to its lowercase equivalent.
|======
| Name | Required | Default | Description
| `field` | yes | - | The field to make lowercase
| `target_field` | no | `field` | The field to assign the converted value to, by default `field` is updated in-place
| `ignore_missing` | no | `false` | If `true` and `field` does not exist or is `null`, the processor quietly exits without modifying the document
|======

Expand Down Expand Up @@ -1874,6 +1878,7 @@ Splits a field into an array using a separator character. Only works on string f
| Name | Required | Default | Description
| `field` | yes | - | The field to split
| `separator` | yes | - | A regex which matches the separator, eg `,` or `\s+`
| `target_field` | no | `field` | The field to assign the split value to, by default `field` is updated in-place
| `ignore_missing` | no | `false` | If `true` and `field` does not exist, the processor quietly exits without modifying the document
|======

Expand All @@ -1899,9 +1904,10 @@ Throws an error when the field is not an array.
.Sort Options
[options="header"]
|======
| Name | Required | Default | Description
| `field` | yes | - | The field to be sorted
| `order` | no | `"asc"` | The sort order to use. Accepts `"asc"` or `"desc"`.
| Name | Required | Default | Description
| `field` | yes | - | The field to be sorted
| `order` | no | `"asc"` | The sort order to use. Accepts `"asc"` or `"desc"`.
| `target_field` | no | `field` | The field to assign the sorted value to, by default `field` is updated in-place
|======

[source,js]
Expand All @@ -1927,6 +1933,7 @@ NOTE: This only works on leading and trailing whitespace.
|======
| Name | Required | Default | Description
| `field` | yes | - | The string-valued field to trim whitespace from
| `target_field` | no | `field` | The field to assign the trimmed value to, by default `field` is updated in-place
| `ignore_missing` | no | `false` | If `true` and `field` does not exist, the processor quietly exits without modifying the document
|======

Expand All @@ -1950,6 +1957,7 @@ Converts a string to its uppercase equivalent.
|======
| Name | Required | Default | Description
| `field` | yes | - | The field to make uppercase
| `target_field` | no | `field` | The field to assign the converted value to, by default `field` is updated in-place
| `ignore_missing` | no | `false` | If `true` and `field` does not exist or is `null`, the processor quietly exits without modifying the document
|======

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
abstract class AbstractStringProcessor extends AbstractProcessor {
private final String field;
private final boolean ignoreMissing;
private final String targetField;

AbstractStringProcessor(String tag, String field, boolean ignoreMissing) {
AbstractStringProcessor(String tag, String field, boolean ignoreMissing, String targetField) {
super(tag);
this.field = field;
this.ignoreMissing = ignoreMissing;
this.targetField = targetField;
}

public String getField() {
Expand All @@ -48,6 +50,10 @@ boolean isIgnoreMissing() {
return ignoreMissing;
}

String getTargetField() {
return targetField;
}

@Override
public final void execute(IngestDocument document) {
String val = document.getFieldValue(field, String.class, ignoreMissing);
Expand All @@ -58,7 +64,7 @@ public final void execute(IngestDocument document) {
throw new IllegalArgumentException("field [" + field + "] is null, cannot process it.");
}

document.setFieldValue(field, process(val));
document.setFieldValue(targetField, process(val));
}

protected abstract String process(String value);
Expand All @@ -75,9 +81,12 @@ public AbstractStringProcessor create(Map<String, Processor.Factory> registry, S
Map<String, Object> config) throws Exception {
String field = ConfigurationUtils.readStringProperty(processorType, tag, config, "field");
boolean ignoreMissing = ConfigurationUtils.readBooleanProperty(processorType, tag, config, "ignore_missing", false);
return newProcessor(tag, field, ignoreMissing);
String targetField = ConfigurationUtils.readStringProperty(processorType, tag, config, "target_field", field);

return newProcessor(tag, config, field, ignoreMissing, targetField);
}

protected abstract AbstractStringProcessor newProcessor(String processorTag, String field, boolean ignoreMissing);
protected abstract AbstractStringProcessor newProcessor(String processorTag, Map<String, Object> config, String field,
boolean ignoreMissing, String targetField);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@

package org.elasticsearch.ingest.common;

import org.elasticsearch.ingest.AbstractProcessor;
import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.Processor;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.elasticsearch.ingest.ConfigurationUtils.newConfigurationException;
Expand All @@ -34,25 +29,19 @@
* Processor that allows to search for patterns in field content and replace them with corresponding string replacement.
* Support fields of string type only, throws exception if a field is of a different type.
*/
public final class GsubProcessor extends AbstractProcessor {
public final class GsubProcessor extends AbstractStringProcessor {

public static final String TYPE = "gsub";

private final String field;
private final Pattern pattern;
private final String replacement;

GsubProcessor(String tag, String field, Pattern pattern, String replacement) {
super(tag);
this.field = field;
GsubProcessor(String tag, String field, Pattern pattern, String replacement, boolean ignoreMissing, String targetField) {
super(tag, field, ignoreMissing, targetField);
this.pattern = pattern;
this.replacement = replacement;
}

String getField() {
return field;
}

Pattern getPattern() {
return pattern;
}
Expand All @@ -61,28 +50,25 @@ String getReplacement() {
return replacement;
}


@Override
public void execute(IngestDocument document) {
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);
protected String process(String value) {
return pattern.matcher(value).replaceAll(replacement);
}

@Override
public String getType() {
return TYPE;
}

public static final class Factory implements Processor.Factory {
public static final class Factory extends AbstractStringProcessor.Factory {

public Factory() {
super(TYPE);
}

@Override
public GsubProcessor create(Map<String, Processor.Factory> registry, String processorTag,
Map<String, Object> config) throws Exception {
String field = readStringProperty(TYPE, processorTag, config, "field");
protected AbstractStringProcessor newProcessor(String processorTag, Map<String, Object> config, String field,
boolean ignoreMissing, String targetField) {
String pattern = readStringProperty(TYPE, processorTag, config, "pattern");
String replacement = readStringProperty(TYPE, processorTag, config, "replacement");
Pattern searchPattern;
Expand All @@ -91,7 +77,7 @@ public GsubProcessor create(Map<String, Processor.Factory> registry, String proc
} catch (Exception e) {
throw newConfigurationException(TYPE, processorTag, "pattern", "Invalid regex pattern. " + e.getMessage());
}
return new GsubProcessor(processorTag, field, searchPattern, replacement);
return new GsubProcessor(processorTag, field, searchPattern, replacement, ignoreMissing, targetField);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ public final class JoinProcessor extends AbstractProcessor {

private final String field;
private final String separator;
private final String targetField;

JoinProcessor(String tag, String field, String separator) {
JoinProcessor(String tag, String field, String separator, String targetField) {
super(tag);
this.field = field;
this.separator = separator;
this.targetField = targetField;
}

String getField() {
Expand All @@ -53,6 +55,10 @@ String getSeparator() {
return separator;
}

String getTargetField() {
return targetField;
}

@Override
public void execute(IngestDocument document) {
List<?> list = document.getFieldValue(field, List.class);
Expand All @@ -62,7 +68,7 @@ public void execute(IngestDocument document) {
String joined = list.stream()
.map(Object::toString)
.collect(Collectors.joining(separator));
document.setFieldValue(field, joined);
document.setFieldValue(targetField, joined);
}

@Override
Expand All @@ -76,7 +82,8 @@ public JoinProcessor create(Map<String, Processor.Factory> registry, String proc
Map<String, Object> config) throws Exception {
String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field");
String separator = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "separator");
return new JoinProcessor(processorTag, field, separator);
String targetField = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "target_field", field);
return new JoinProcessor(processorTag, field, separator, targetField);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.ingest.common;

import java.util.Locale;
import java.util.Map;

/**
* Processor that converts the content of string fields to lowercase.
Expand All @@ -30,8 +31,8 @@ public final class LowercaseProcessor extends AbstractStringProcessor {

public static final String TYPE = "lowercase";

LowercaseProcessor(String processorTag, String field, boolean ignoreMissing) {
super(processorTag, field, ignoreMissing);
LowercaseProcessor(String processorTag, String field, boolean ignoreMissing, String targetField) {
super(processorTag, field, ignoreMissing, targetField);
}

@Override
Expand All @@ -51,8 +52,9 @@ public Factory() {
}

@Override
protected LowercaseProcessor newProcessor(String tag, String field, boolean ignoreMissing) {
return new LowercaseProcessor(tag, field, ignoreMissing);
protected LowercaseProcessor newProcessor(String tag, Map<String, Object> config, String field,
boolean ignoreMissing, String targetField) {
return new LowercaseProcessor(tag, field, ignoreMissing, targetField);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ public static SortOrder fromString(String value) {

private final String field;
private final SortOrder order;
private final String targetField;

SortProcessor(String tag, String field, SortOrder order) {
SortProcessor(String tag, String field, SortOrder order, String targetField) {
super(tag);
this.field = field;
this.order = order;
this.targetField = targetField;
}

String getField() {
Expand All @@ -84,6 +86,10 @@ SortOrder getOrder() {
return order;
}

String getTargetField() {
return targetField;
}

@Override
@SuppressWarnings("unchecked")
public void execute(IngestDocument document) {
Expand All @@ -103,7 +109,7 @@ public void execute(IngestDocument document) {
Collections.sort(list, Collections.reverseOrder());
}

document.setFieldValue(field, list);
document.setFieldValue(targetField, list);
}

@Override
Expand All @@ -117,6 +123,7 @@ public static final class Factory implements Processor.Factory {
public SortProcessor create(Map<String, Processor.Factory> registry, String processorTag,
Map<String, Object> config) throws Exception {
String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, FIELD);
String targetField = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "target_field", field);
try {
SortOrder direction = SortOrder.fromString(
ConfigurationUtils.readStringProperty(
Expand All @@ -125,7 +132,7 @@ public SortProcessor create(Map<String, Processor.Factory> registry, String proc
config,
ORDER,
DEFAULT_ORDER));
return new SortProcessor(processorTag, field, direction);
return new SortProcessor(processorTag, field, direction, targetField);
} catch (IllegalArgumentException e) {
throw ConfigurationUtils.newConfigurationException(TYPE, processorTag, ORDER, e.getMessage());
}
Expand Down
Loading