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