19
19
20
20
package org .elasticsearch .ingest .common ;
21
21
22
+ import org .elasticsearch .common .util .set .Sets ;
22
23
import org .elasticsearch .ingest .IngestDocument ;
23
24
import org .elasticsearch .ingest .Processor ;
24
25
import org .elasticsearch .ingest .RandomDocumentPicks ;
@@ -36,7 +37,7 @@ public class KeyValueProcessorTests extends ESTestCase {
36
37
public void test () throws Exception {
37
38
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
38
39
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 );
40
41
processor .execute (ingestDocument );
41
42
assertThat (ingestDocument .getFieldValue ("target.first" , String .class ), equalTo ("hello" ));
42
43
assertThat (ingestDocument .getFieldValue ("target.second" , List .class ), equalTo (Arrays .asList ("world" , "universe" )));
@@ -45,7 +46,7 @@ public void test() throws Exception {
45
46
public void testRootTarget () throws Exception {
46
47
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), Collections .emptyMap ());
47
48
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 );
49
50
processor .execute (ingestDocument );
50
51
assertThat (ingestDocument .getFieldValue ("first" , String .class ), equalTo ("hello" ));
51
52
assertThat (ingestDocument .getFieldValue ("second" , List .class ), equalTo (Arrays .asList ("world" , "universe" )));
@@ -54,7 +55,7 @@ public void testRootTarget() throws Exception {
54
55
public void testKeySameAsSourceField () throws Exception {
55
56
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), Collections .emptyMap ());
56
57
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 );
58
59
processor .execute (ingestDocument );
59
60
assertThat (ingestDocument .getFieldValue ("first" , List .class ), equalTo (Arrays .asList ("first=hello" , "hello" )));
60
61
}
@@ -63,15 +64,38 @@ public void testIncludeKeys() throws Exception {
63
64
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
64
65
String fieldName = RandomDocumentPicks .addRandomField (random (), ingestDocument , "first=hello&second=world&second=universe" );
65
66
Processor processor = new KeyValueProcessor (randomAlphaOfLength (10 ), fieldName , "&" , "=" ,
66
- Collections . singletonList ("first" ), "target" , false );
67
+ Sets . newHashSet ("first" ), null , "target" , false );
67
68
processor .execute (ingestDocument );
68
69
assertThat (ingestDocument .getFieldValue ("target.first" , String .class ), equalTo ("hello" ));
69
70
assertFalse (ingestDocument .hasField ("target.second" ));
70
71
}
71
72
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
+
72
95
public void testMissingField () {
73
96
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 );
75
99
IllegalArgumentException exception = expectThrows (IllegalArgumentException .class , () -> processor .execute (ingestDocument ));
76
100
assertThat (exception .getMessage (), equalTo ("field [unknown] not present as part of path [unknown]" ));
77
101
}
@@ -81,31 +105,31 @@ public void testNullValueWithIgnoreMissing() throws Exception {
81
105
IngestDocument originalIngestDocument = RandomDocumentPicks .randomIngestDocument (random (),
82
106
Collections .singletonMap (fieldName , null ));
83
107
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 );
85
109
processor .execute (ingestDocument );
86
110
assertIngestDocument (originalIngestDocument , ingestDocument );
87
111
}
88
112
89
113
public void testNonExistentWithIgnoreMissing () throws Exception {
90
114
IngestDocument originalIngestDocument = RandomDocumentPicks .randomIngestDocument (random (), Collections .emptyMap ());
91
115
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 );
93
117
processor .execute (ingestDocument );
94
118
assertIngestDocument (originalIngestDocument , ingestDocument );
95
119
}
96
120
97
121
public void testFailFieldSplitMatch () throws Exception {
98
122
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
99
123
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 );
101
125
processor .execute (ingestDocument );
102
126
assertThat (ingestDocument .getFieldValue ("target.first" , String .class ), equalTo ("hello|second=world|second=universe" ));
103
127
assertFalse (ingestDocument .hasField ("target.second" ));
104
128
}
105
129
106
130
public void testFailValueSplitMatch () throws Exception {
107
131
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 );
109
133
Exception exception = expectThrows (IllegalArgumentException .class , () -> processor .execute (ingestDocument ));
110
134
assertThat (exception .getMessage (), equalTo ("field [foo] does not contain value_split [=]" ));
111
135
}
0 commit comments