19
19
20
20
package org .elasticsearch .ingest .common ;
21
21
22
- import org .elasticsearch .ingest .CompoundProcessor ;
23
- import org .elasticsearch .ingest .IngestDocument ;
24
- import org .elasticsearch .ingest .Processor ;
25
- import org .elasticsearch .ingest .TestProcessor ;
26
- import org .elasticsearch .ingest .TestTemplateService ;
27
- import org .elasticsearch .script .TemplateScript ;
28
- import org .elasticsearch .test .ESTestCase ;
29
-
30
22
import java .util .ArrayList ;
31
23
import java .util .Arrays ;
32
24
import java .util .Collections ;
33
25
import java .util .HashMap ;
34
26
import java .util .List ;
35
27
import java .util .Locale ;
36
28
import java .util .Map ;
29
+ import org .elasticsearch .ingest .CompoundProcessor ;
30
+ import org .elasticsearch .ingest .IngestDocument ;
31
+ import org .elasticsearch .ingest .Processor ;
32
+ import org .elasticsearch .ingest .TestProcessor ;
33
+ import org .elasticsearch .ingest .TestTemplateService ;
34
+ import org .elasticsearch .script .TemplateScript ;
35
+ import org .elasticsearch .test .ESTestCase ;
37
36
37
+ import static org .elasticsearch .ingest .IngestDocumentMatcher .assertIngestDocument ;
38
38
import static org .hamcrest .Matchers .equalTo ;
39
39
40
40
public class ForEachProcessorTests extends ESTestCase {
@@ -49,7 +49,8 @@ public void testExecute() throws Exception {
49
49
);
50
50
51
51
ForEachProcessor processor = new ForEachProcessor (
52
- "_tag" , "values" , new UppercaseProcessor ("_tag" , "_ingest._value" , false , "_ingest._value" )
52
+ "_tag" , "values" , new UppercaseProcessor ("_tag" , "_ingest._value" , false , "_ingest._value" ),
53
+ false
53
54
);
54
55
processor .execute (ingestDocument );
55
56
@@ -69,7 +70,7 @@ public void testExecuteWithFailure() throws Exception {
69
70
throw new RuntimeException ("failure" );
70
71
}
71
72
});
72
- ForEachProcessor processor = new ForEachProcessor ("_tag" , "values" , testProcessor );
73
+ ForEachProcessor processor = new ForEachProcessor ("_tag" , "values" , testProcessor , false );
73
74
try {
74
75
processor .execute (ingestDocument );
75
76
fail ("exception expected" );
@@ -89,7 +90,8 @@ public void testExecuteWithFailure() throws Exception {
89
90
});
90
91
Processor onFailureProcessor = new TestProcessor (ingestDocument1 -> {});
91
92
processor = new ForEachProcessor (
92
- "_tag" , "values" , new CompoundProcessor (false , Arrays .asList (testProcessor ), Arrays .asList (onFailureProcessor ))
93
+ "_tag" , "values" , new CompoundProcessor (false , Arrays .asList (testProcessor ), Arrays .asList (onFailureProcessor )),
94
+ false
93
95
);
94
96
processor .execute (ingestDocument );
95
97
assertThat (testProcessor .getInvokedCounter (), equalTo (3 ));
@@ -109,7 +111,7 @@ public void testMetaDataAvailable() throws Exception {
109
111
id .setFieldValue ("_ingest._value.type" , id .getSourceAndMetadata ().get ("_type" ));
110
112
id .setFieldValue ("_ingest._value.id" , id .getSourceAndMetadata ().get ("_id" ));
111
113
});
112
- ForEachProcessor processor = new ForEachProcessor ("_tag" , "values" , innerProcessor );
114
+ ForEachProcessor processor = new ForEachProcessor ("_tag" , "values" , innerProcessor , false );
113
115
processor .execute (ingestDocument );
114
116
115
117
assertThat (innerProcessor .getInvokedCounter (), equalTo (2 ));
@@ -137,7 +139,7 @@ public void testRestOfTheDocumentIsAvailable() throws Exception {
137
139
ForEachProcessor processor = new ForEachProcessor (
138
140
"_tag" , "values" , new SetProcessor ("_tag" ,
139
141
new TestTemplateService .MockTemplateScript .Factory ("_ingest._value.new_field" ),
140
- (model ) -> model .get ("other" )));
142
+ (model ) -> model .get ("other" )), false );
141
143
processor .execute (ingestDocument );
142
144
143
145
assertThat (ingestDocument .getFieldValue ("values.0.new_field" , String .class ), equalTo ("value" ));
@@ -174,7 +176,7 @@ public String getTag() {
174
176
"_index" , "_type" , "_id" , null , null , null , Collections .singletonMap ("values" , values )
175
177
);
176
178
177
- ForEachProcessor processor = new ForEachProcessor ("_tag" , "values" , innerProcessor );
179
+ ForEachProcessor processor = new ForEachProcessor ("_tag" , "values" , innerProcessor , false );
178
180
processor .execute (ingestDocument );
179
181
@ SuppressWarnings ("unchecked" )
180
182
List <String > result = ingestDocument .getFieldValue ("values" , List .class );
@@ -199,7 +201,7 @@ public void testModifyFieldsOutsideArray() throws Exception {
199
201
"_tag" , "values" , new CompoundProcessor (false ,
200
202
Collections .singletonList (new UppercaseProcessor ("_tag_upper" , "_ingest._value" , false , "_ingest._value" )),
201
203
Collections .singletonList (new AppendProcessor ("_tag" , template , (model ) -> (Collections .singletonList ("added" ))))
202
- ));
204
+ ), false );
203
205
processor .execute (ingestDocument );
204
206
205
207
List result = ingestDocument .getFieldValue ("values" , List .class );
@@ -225,7 +227,7 @@ public void testScalarValueAllowsUnderscoreValueFieldToRemainAccessible() throws
225
227
226
228
TestProcessor processor = new TestProcessor (doc -> doc .setFieldValue ("_ingest._value" ,
227
229
doc .getFieldValue ("_source._value" , String .class )));
228
- ForEachProcessor forEachProcessor = new ForEachProcessor ("_tag" , "values" , processor );
230
+ ForEachProcessor forEachProcessor = new ForEachProcessor ("_tag" , "values" , processor , false );
229
231
forEachProcessor .execute (ingestDocument );
230
232
231
233
List result = ingestDocument .getFieldValue ("values" , List .class );
@@ -258,7 +260,7 @@ public void testNestedForEach() throws Exception {
258
260
doc -> doc .setFieldValue ("_ingest._value" , doc .getFieldValue ("_ingest._value" , String .class ).toUpperCase (Locale .ENGLISH ))
259
261
);
260
262
ForEachProcessor processor = new ForEachProcessor (
261
- "_tag" , "values1" , new ForEachProcessor ("_tag" , "_ingest._value.values2" , testProcessor ) );
263
+ "_tag" , "values1" , new ForEachProcessor ("_tag" , "_ingest._value.values2" , testProcessor , false ), false );
262
264
processor .execute (ingestDocument );
263
265
264
266
List result = ingestDocument .getFieldValue ("values1.0.values2" , List .class );
@@ -270,4 +272,16 @@ public void testNestedForEach() throws Exception {
270
272
assertThat (result .get (1 ), equalTo ("JKL" ));
271
273
}
272
274
275
+ public void testIgnoreMissing () throws Exception {
276
+ IngestDocument originalIngestDocument = new IngestDocument (
277
+ "_index" , "_type" , "_id" , null , null , null , Collections .emptyMap ()
278
+ );
279
+ IngestDocument ingestDocument = new IngestDocument (originalIngestDocument );
280
+ TestProcessor testProcessor = new TestProcessor (doc -> {});
281
+ ForEachProcessor processor = new ForEachProcessor ("_tag" , "_ingest._value" , testProcessor , true );
282
+ processor .execute (ingestDocument );
283
+ assertIngestDocument (originalIngestDocument , ingestDocument );
284
+ assertThat (testProcessor .getInvokedCounter (), equalTo (0 ));
285
+ }
286
+
273
287
}
0 commit comments