Skip to content

Commit 8ffe5d1

Browse files
authored
Support array for all string ingest processors
1 parent fa6d197 commit 8ffe5d1

File tree

9 files changed

+66
-10
lines changed

9 files changed

+66
-10
lines changed

docs/reference/ingest/processors/bytes.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[[bytes-processor]]
22
=== Bytes Processor
3-
Converts a human readable byte value (e.g. 1kb) to its value in bytes (e.g. 1024).
3+
Converts a human readable byte value (e.g. 1kb) to its value in bytes (e.g. 1024). If the field is an array of strings, all members of the array will be converted.
44

55
Supported human readable units are "b", "kb", "mb", "gb", "tb", "pb" case insensitive. An error will occur if
66
the field is not a supported format or resultant value exceeds 2^63.

docs/reference/ingest/processors/gsub.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[[gsub-processor]]
22
=== Gsub Processor
33
Converts a string field by applying a regular expression and a replacement.
4-
If the field is not a string, the processor will throw an exception.
4+
If the field is an array of string, all members of the array will be converted. If any non-string values are encountered, the processor will throw an exception.
55

66
[[gsub-options]]
77
.Gsub Options

docs/reference/ingest/processors/html_strip.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[[htmlstrip-processor]]
22
=== HTML Strip Processor
3-
Removes HTML from field.
3+
Removes HTML tags from the field. If the field is an array of strings, HTML tags will be removed from all members of the array.
44

55
NOTE: Each HTML tag is replaced with a `\n` character.
66

docs/reference/ingest/processors/lowercase.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[[lowercase-processor]]
22
=== Lowercase Processor
3-
Converts a string to its lowercase equivalent.
3+
Converts a string to its lowercase equivalent. If the field is an array of strings, all members of the array will be converted.
44

55
[[lowercase-options]]
66
.Lowercase Options

docs/reference/ingest/processors/trim.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[[trim-processor]]
22
=== Trim Processor
3-
Trims whitespace from field.
3+
Trims whitespace from field. If the field is an array of strings, all members of the array will be trimmed.
44

55
NOTE: This only works on leading and trailing whitespace.
66

docs/reference/ingest/processors/uppercase.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[[uppercase-processor]]
22
=== Uppercase Processor
3-
Converts a string to its uppercase equivalent.
3+
Converts a string to its uppercase equivalent. If the field is an array of strings, all members of the array will be converted.
44

55
[[uppercase-options]]
66
.Uppercase Options

docs/reference/ingest/processors/url-decode.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[[urldecode-processor]]
22
=== URL Decode Processor
3-
URL-decodes a string
3+
URL-decodes a string. If the field is an array of strings, all members of the array will be decoded.
44

55
[[urldecode-options]]
66
.URL Decode Options

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.elasticsearch.ingest.IngestDocument;
2525
import org.elasticsearch.ingest.Processor;
2626

27+
import java.util.ArrayList;
28+
import java.util.List;
2729
import java.util.Map;
2830

2931
/**
@@ -58,15 +60,38 @@ String getTargetField() {
5860

5961
@Override
6062
public final IngestDocument execute(IngestDocument document) {
61-
String val = document.getFieldValue(field, String.class, ignoreMissing);
63+
Object val = document.getFieldValue(field, Object.class, ignoreMissing);
64+
Object newValue;
6265

6366
if (val == null && ignoreMissing) {
6467
return document;
6568
} else if (val == null) {
6669
throw new IllegalArgumentException("field [" + field + "] is null, cannot process it.");
6770
}
6871

69-
document.setFieldValue(targetField, process(val));
72+
if (val instanceof List) {
73+
List<?> list = (List<?>) val;
74+
List<Object> newList = new ArrayList<>(list.size());
75+
for (Object value : list) {
76+
if (value instanceof String) {
77+
newList.add(process((String) value));
78+
} else {
79+
throw new IllegalArgumentException("value [" + value + "] of type [" + value.getClass().getName() +
80+
"] in list field [" + field + "] cannot be cast to [" + String.class.getName() + "]");
81+
}
82+
}
83+
newValue = newList;
84+
} else {
85+
if (val instanceof String) {
86+
newValue = process((String) val);
87+
} else {
88+
throw new IllegalArgumentException("field [" + field + "] of type [" + val.getClass().getName() + "] cannot be cast to [" +
89+
String.class.getName() + "]");
90+
}
91+
92+
}
93+
94+
document.setFieldValue(targetField, newValue);
7095
return document;
7196
}
7297

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
import org.elasticsearch.ingest.RandomDocumentPicks;
2525
import org.elasticsearch.test.ESTestCase;
2626

27+
import java.util.ArrayList;
2728
import java.util.Collections;
2829
import java.util.HashMap;
30+
import java.util.List;
2931

3032
import static org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument;
3133
import static org.hamcrest.Matchers.containsString;
@@ -41,7 +43,7 @@ protected String modifyInput(String input) {
4143

4244
protected abstract T expectedResult(String input);
4345

44-
protected Class<?> expectedResultType(){
46+
protected Class<?> expectedResultType() {
4547
return String.class; // most results types are Strings
4648
}
4749

@@ -52,6 +54,19 @@ public void testProcessor() throws Exception {
5254
Processor processor = newProcessor(fieldName, randomBoolean(), fieldName);
5355
processor.execute(ingestDocument);
5456
assertThat(ingestDocument.getFieldValue(fieldName, expectedResultType()), equalTo(expectedResult(fieldValue)));
57+
58+
int numItems = randomIntBetween(1, 10);
59+
List<String> fieldValueList = new ArrayList<>();
60+
List<T> expectedList = new ArrayList<>();
61+
for (int i = 0; i < numItems; i++) {
62+
String randomString = RandomDocumentPicks.randomString(random());
63+
fieldValueList.add(modifyInput(randomString));
64+
expectedList.add(expectedResult(randomString));
65+
}
66+
String multiValueFieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValueList);
67+
Processor multiValueProcessor = newProcessor(multiValueFieldName, randomBoolean(), multiValueFieldName);
68+
multiValueProcessor.execute(ingestDocument);
69+
assertThat(ingestDocument.getFieldValue(multiValueFieldName, List.class), equalTo(expectedList));
5570
}
5671

5772
public void testFieldNotFound() throws Exception {
@@ -94,6 +109,14 @@ public void testNonStringValue() throws Exception {
94109
Exception e = expectThrows(Exception.class, () -> processor.execute(ingestDocument));
95110
assertThat(e.getMessage(), equalTo("field [" + fieldName +
96111
"] of type [java.lang.Integer] cannot be cast to [java.lang.String]"));
112+
113+
List<Integer> fieldValueList = new ArrayList<>();
114+
int randomValue = randomInt();
115+
fieldValueList.add(randomValue);
116+
ingestDocument.setFieldValue(fieldName, fieldValueList);
117+
Exception exception = expectThrows(Exception.class, () -> processor.execute(ingestDocument));
118+
assertThat(exception.getMessage(), equalTo("value [" + randomValue + "] of type [java.lang.Integer] in list field [" + fieldName +
119+
"] cannot be cast to [java.lang.String]"));
97120
}
98121

99122
public void testNonStringValueWithIgnoreMissing() throws Exception {
@@ -104,6 +127,14 @@ public void testNonStringValueWithIgnoreMissing() throws Exception {
104127
Exception e = expectThrows(Exception.class, () -> processor.execute(ingestDocument));
105128
assertThat(e.getMessage(), equalTo("field [" + fieldName +
106129
"] of type [java.lang.Integer] cannot be cast to [java.lang.String]"));
130+
131+
List<Integer> fieldValueList = new ArrayList<>();
132+
int randomValue = randomInt();
133+
fieldValueList.add(randomValue);
134+
ingestDocument.setFieldValue(fieldName, fieldValueList);
135+
Exception exception = expectThrows(Exception.class, () -> processor.execute(ingestDocument));
136+
assertThat(exception.getMessage(), equalTo("value [" + randomValue + "] of type [java.lang.Integer] in list field [" + fieldName +
137+
"] cannot be cast to [java.lang.String]"));
107138
}
108139

109140
public void testTargetField() throws Exception {

0 commit comments

Comments
 (0)