1919
2020package org .elasticsearch .ingest .common ;
2121
22+ import org .elasticsearch .common .util .set .Sets ;
2223import org .elasticsearch .ingest .IngestDocument ;
2324import org .elasticsearch .ingest .Processor ;
2425import org .elasticsearch .ingest .RandomDocumentPicks ;
@@ -36,7 +37,7 @@ public class KeyValueProcessorTests extends ESTestCase {
3637 public void test () throws Exception {
3738 IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
3839 String fieldName = RandomDocumentPicks .addRandomField (random (), ingestDocument , "first=hello&second=world&second=universe" );
39- Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), fieldName , "&" , "=" , null , "target" , false );
40+ Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), fieldName , "&" , "=" , null , null , "target" , false );
4041 processor .execute (ingestDocument );
4142 assertThat (ingestDocument .getFieldValue ("target.first" , String .class ), equalTo ("hello" ));
4243 assertThat (ingestDocument .getFieldValue ("target.second" , List .class ), equalTo (Arrays .asList ("world" , "universe" )));
@@ -45,7 +46,7 @@ public void test() throws Exception {
4546 public void testRootTarget () throws Exception {
4647 IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), Collections .emptyMap ());
4748 ingestDocument .setFieldValue ("myField" , "first=hello&second=world&second=universe" );
48- Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), "myField" , "&" , "=" , null , null , false );
49+ Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), "myField" , "&" , "=" , null , null ,null , false );
4950 processor .execute (ingestDocument );
5051 assertThat (ingestDocument .getFieldValue ("first" , String .class ), equalTo ("hello" ));
5152 assertThat (ingestDocument .getFieldValue ("second" , List .class ), equalTo (Arrays .asList ("world" , "universe" )));
@@ -54,7 +55,7 @@ public void testRootTarget() throws Exception {
5455 public void testKeySameAsSourceField () throws Exception {
5556 IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), Collections .emptyMap ());
5657 ingestDocument .setFieldValue ("first" , "first=hello" );
57- Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), "first" , "&" , "=" , null , null , false );
58+ Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), "first" , "&" , "=" , null , null ,null , false );
5859 processor .execute (ingestDocument );
5960 assertThat (ingestDocument .getFieldValue ("first" , List .class ), equalTo (Arrays .asList ("first=hello" , "hello" )));
6061 }
@@ -63,15 +64,38 @@ public void testIncludeKeys() throws Exception {
6364 IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
6465 String fieldName = RandomDocumentPicks .addRandomField (random (), ingestDocument , "first=hello&second=world&second=universe" );
6566 Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), fieldName , "&" , "=" ,
66- Collections . singletonList ("first" ), "target" , false );
67+ Sets . newHashSet ("first" ), null , "target" , false );
6768 processor .execute (ingestDocument );
6869 assertThat (ingestDocument .getFieldValue ("target.first" , String .class ), equalTo ("hello" ));
6970 assertFalse (ingestDocument .hasField ("target.second" ));
7071 }
7172
73+ public void testExcludeKeys () throws Exception {
74+ IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
75+ String fieldName = RandomDocumentPicks .addRandomField (random (), ingestDocument , "first=hello&second=world&second=universe" );
76+ Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), fieldName , "&" , "=" ,
77+ null , Sets .newHashSet ("second" ), "target" , false );
78+ processor .execute (ingestDocument );
79+ assertThat (ingestDocument .getFieldValue ("target.first" , String .class ), equalTo ("hello" ));
80+ assertFalse (ingestDocument .hasField ("target.second" ));
81+ }
82+
83+ public void testIncludeAndExcludeKeys () throws Exception {
84+ IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
85+ String fieldName = RandomDocumentPicks .addRandomField (random (), ingestDocument ,
86+ "first=hello&second=world&second=universe&third=bar" );
87+ Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), fieldName , "&" , "=" ,
88+ Sets .newHashSet ("first" , "second" ), Sets .newHashSet ("first" , "second" ), "target" , false );
89+ processor .execute (ingestDocument );
90+ assertFalse (ingestDocument .hasField ("target.first" ));
91+ assertFalse (ingestDocument .hasField ("target.second" ));
92+ assertFalse (ingestDocument .hasField ("target.third" ));
93+ }
94+
7295 public void testMissingField () {
7396 IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), Collections .emptyMap ());
74- Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), "unknown" , "&" , "=" , null , "target" , false );
97+ Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), "unknown" , "&" ,
98+ "=" , null , null , "target" , false );
7599 IllegalArgumentException exception = expectThrows (IllegalArgumentException .class , () -> processor .execute (ingestDocument ));
76100 assertThat (exception .getMessage (), equalTo ("field [unknown] not present as part of path [unknown]" ));
77101 }
@@ -81,31 +105,31 @@ public void testNullValueWithIgnoreMissing() throws Exception {
81105 IngestDocument originalIngestDocument = RandomDocumentPicks .randomIngestDocument (random (),
82106 Collections .singletonMap (fieldName , null ));
83107 IngestDocument ingestDocument = new IngestDocument (originalIngestDocument );
84- Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), fieldName , "" , "" , null , "target" , true );
108+ Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), fieldName , "" , "" , null , null , "target" , true );
85109 processor .execute (ingestDocument );
86110 assertIngestDocument (originalIngestDocument , ingestDocument );
87111 }
88112
89113 public void testNonExistentWithIgnoreMissing () throws Exception {
90114 IngestDocument originalIngestDocument = RandomDocumentPicks .randomIngestDocument (random (), Collections .emptyMap ());
91115 IngestDocument ingestDocument = new IngestDocument (originalIngestDocument );
92- Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), "unknown" , "" , "" , null , "target" , true );
116+ Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), "unknown" , "" , "" , null , null , "target" , true );
93117 processor .execute (ingestDocument );
94118 assertIngestDocument (originalIngestDocument , ingestDocument );
95119 }
96120
97121 public void testFailFieldSplitMatch () throws Exception {
98122 IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
99123 String fieldName = RandomDocumentPicks .addRandomField (random (), ingestDocument , "first=hello|second=world|second=universe" );
100- Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), fieldName , "&" , "=" , null , "target" , false );
124+ Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), fieldName , "&" , "=" , null , null , "target" , false );
101125 processor .execute (ingestDocument );
102126 assertThat (ingestDocument .getFieldValue ("target.first" , String .class ), equalTo ("hello|second=world|second=universe" ));
103127 assertFalse (ingestDocument .hasField ("target.second" ));
104128 }
105129
106130 public void testFailValueSplitMatch () throws Exception {
107131 IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), Collections .singletonMap ("foo" , "bar" ));
108- Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), "foo" , "&" , "=" , null , "target" , false );
132+ Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), "foo" , "&" , "=" , null , null , "target" , false );
109133 Exception exception = expectThrows (IllegalArgumentException .class , () -> processor .execute (ingestDocument ));
110134 assertThat (exception .getMessage (), equalTo ("field [foo] does not contain value_split [=]" ));
111135 }
0 commit comments