@@ -59,7 +59,7 @@ public void testRename() throws Exception {
59
59
do {
60
60
newFieldName = RandomDocumentPicks .randomFieldName (random ());
61
61
} while (RandomDocumentPicks .canAddField (newFieldName , ingestDocument ) == false || newFieldName .equals (fieldName ));
62
- Processor processor = createRenameProcessor (fieldName , newFieldName , false );
62
+ Processor processor = createRenameProcessor (fieldName , newFieldName , false , false );
63
63
processor .execute (ingestDocument );
64
64
assertThat (ingestDocument .getFieldValue (newFieldName , Object .class ), equalTo (fieldValue ));
65
65
}
@@ -77,7 +77,7 @@ public void testRenameArrayElement() throws Exception {
77
77
document .put ("one" , one );
78
78
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), document );
79
79
80
- Processor processor = createRenameProcessor ("list.0" , "item" , false );
80
+ Processor processor = createRenameProcessor ("list.0" , "item" , false , false );
81
81
processor .execute (ingestDocument );
82
82
Object actualObject = ingestDocument .getSourceAndMetadata ().get ("list" );
83
83
assertThat (actualObject , instanceOf (List .class ));
@@ -90,7 +90,7 @@ public void testRenameArrayElement() throws Exception {
90
90
assertThat (actualObject , instanceOf (String .class ));
91
91
assertThat (actualObject , equalTo ("item1" ));
92
92
93
- processor = createRenameProcessor ("list.0" , "list.3" , false );
93
+ processor = createRenameProcessor ("list.0" , "list.3" , false , randomBoolean () );
94
94
try {
95
95
processor .execute (ingestDocument );
96
96
fail ("processor execute should have failed" );
@@ -105,7 +105,7 @@ public void testRenameArrayElement() throws Exception {
105
105
public void testRenameNonExistingField () throws Exception {
106
106
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), new HashMap <>());
107
107
String fieldName = RandomDocumentPicks .randomFieldName (random ());
108
- Processor processor = createRenameProcessor (fieldName , RandomDocumentPicks .randomFieldName (random ()), false );
108
+ Processor processor = createRenameProcessor (fieldName , RandomDocumentPicks .randomFieldName (random ()), false , false );
109
109
try {
110
110
processor .execute (ingestDocument );
111
111
fail ("processor execute should have failed" );
@@ -114,7 +114,7 @@ public void testRenameNonExistingField() throws Exception {
114
114
}
115
115
116
116
// when using template snippet, the resolved field path maybe empty
117
- processor = createRenameProcessor ("" , RandomDocumentPicks .randomFieldName (random ()), false );
117
+ processor = createRenameProcessor ("" , RandomDocumentPicks .randomFieldName (random ()), false , false );
118
118
try {
119
119
processor .execute (ingestDocument );
120
120
fail ("processor execute should have failed" );
@@ -127,38 +127,44 @@ public void testRenameNonExistingFieldWithIgnoreMissing() throws Exception {
127
127
IngestDocument originalIngestDocument = RandomDocumentPicks .randomIngestDocument (random (), new HashMap <>());
128
128
IngestDocument ingestDocument = new IngestDocument (originalIngestDocument );
129
129
String fieldName = RandomDocumentPicks .randomFieldName (random ());
130
- Processor processor = createRenameProcessor (fieldName , RandomDocumentPicks .randomFieldName (random ()), true );
130
+ Processor processor = createRenameProcessor (fieldName , RandomDocumentPicks .randomFieldName (random ()), true , false );
131
131
processor .execute (ingestDocument );
132
132
assertIngestDocument (originalIngestDocument , ingestDocument );
133
133
134
134
// when using template snippet, the resolved field path maybe empty
135
- processor = createRenameProcessor ("" , RandomDocumentPicks .randomFieldName (random ()), true );
135
+ processor = createRenameProcessor ("" , RandomDocumentPicks .randomFieldName (random ()), true , false );
136
136
processor .execute (ingestDocument );
137
137
assertIngestDocument (originalIngestDocument , ingestDocument );
138
138
}
139
139
140
140
public void testRenameNewFieldAlreadyExists () throws Exception {
141
141
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
142
- String fieldName = RandomDocumentPicks .randomExistingFieldName (random (), ingestDocument );
143
- Processor processor = createRenameProcessor (
144
- RandomDocumentPicks .randomExistingFieldName (random (), ingestDocument ),
145
- fieldName ,
146
- false
147
- );
142
+ String field = RandomDocumentPicks .randomExistingFieldName (random (), ingestDocument );
143
+ Object fieldValue = ingestDocument .getFieldValue (field , Object .class );
144
+ String targetField = RandomDocumentPicks .addRandomField (random (), ingestDocument , RandomDocumentPicks .randomFieldValue (random ()));
145
+
146
+ Processor processor = createRenameProcessor (field , targetField , false , false );
148
147
try {
149
148
processor .execute (ingestDocument );
150
149
fail ("processor execute should have failed" );
151
150
} catch (IllegalArgumentException e ) {
152
- assertThat (e .getMessage (), equalTo ("field [" + fieldName + "] already exists" ));
151
+ assertThat (e .getMessage (), equalTo ("field [" + targetField + "] already exists" ));
153
152
}
153
+
154
+ Processor processorWithOverrideTarget = createRenameProcessor (field , targetField , false , true );
155
+
156
+ processorWithOverrideTarget .execute (ingestDocument );
157
+ assertThat (ingestDocument .hasField (field ), equalTo (false ));
158
+ assertThat (ingestDocument .hasField (targetField ), equalTo (true ));
159
+ assertThat (ingestDocument .getFieldValue (targetField , Object .class ), equalTo (fieldValue ));
154
160
}
155
161
156
162
public void testRenameExistingFieldNullValue () throws Exception {
157
163
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), new HashMap <>());
158
164
String fieldName = RandomDocumentPicks .randomFieldName (random ());
159
165
ingestDocument .setFieldValue (fieldName , null );
160
166
String newFieldName = randomValueOtherThanMany (ingestDocument ::hasField , () -> RandomDocumentPicks .randomFieldName (random ()));
161
- Processor processor = createRenameProcessor (fieldName , newFieldName , false );
167
+ Processor processor = createRenameProcessor (fieldName , newFieldName , false , false );
162
168
processor .execute (ingestDocument );
163
169
if (newFieldName .startsWith (fieldName + '.' )) {
164
170
assertThat (ingestDocument .getFieldValue (fieldName , Object .class ), instanceOf (Map .class ));
@@ -182,7 +188,7 @@ public Object put(String key, Object value) {
182
188
source .put ("list" , Collections .singletonList ("item" ));
183
189
184
190
IngestDocument ingestDocument = new IngestDocument (source , Collections .emptyMap ());
185
- Processor processor = createRenameProcessor ("list" , "new_field" , false );
191
+ Processor processor = createRenameProcessor ("list" , "new_field" , false , false );
186
192
try {
187
193
processor .execute (ingestDocument );
188
194
fail ("processor execute should have failed" );
@@ -206,7 +212,7 @@ public Object remove(Object key) {
206
212
source .put ("list" , Collections .singletonList ("item" ));
207
213
208
214
IngestDocument ingestDocument = new IngestDocument (source , Collections .emptyMap ());
209
- Processor processor = createRenameProcessor ("list" , "new_field" , false );
215
+ Processor processor = createRenameProcessor ("list" , "new_field" , false , false );
210
216
try {
211
217
processor .execute (ingestDocument );
212
218
fail ("processor execute should have failed" );
@@ -221,12 +227,12 @@ public void testRenameLeafIntoBranch() throws Exception {
221
227
Map <String , Object > source = new HashMap <>();
222
228
source .put ("foo" , "bar" );
223
229
IngestDocument ingestDocument = new IngestDocument (source , Collections .emptyMap ());
224
- Processor processor1 = createRenameProcessor ("foo" , "foo.bar" , false );
230
+ Processor processor1 = createRenameProcessor ("foo" , "foo.bar" , false , false );
225
231
processor1 .execute (ingestDocument );
226
232
assertThat (ingestDocument .getFieldValue ("foo" , Map .class ), equalTo (Collections .singletonMap ("bar" , "bar" )));
227
233
assertThat (ingestDocument .getFieldValue ("foo.bar" , String .class ), equalTo ("bar" ));
228
234
229
- Processor processor2 = createRenameProcessor ("foo.bar" , "foo.bar.baz" , false );
235
+ Processor processor2 = createRenameProcessor ("foo.bar" , "foo.bar.baz" , false , false );
230
236
processor2 .execute (ingestDocument );
231
237
assertThat (
232
238
ingestDocument .getFieldValue ("foo" , Map .class ),
@@ -236,18 +242,19 @@ public void testRenameLeafIntoBranch() throws Exception {
236
242
assertThat (ingestDocument .getFieldValue ("foo.bar.baz" , String .class ), equalTo ("bar" ));
237
243
238
244
// for fun lets try to restore it (which don't allow today)
239
- Processor processor3 = createRenameProcessor ("foo.bar.baz" , "foo" , false );
245
+ Processor processor3 = createRenameProcessor ("foo.bar.baz" , "foo" , false , false );
240
246
Exception e = expectThrows (IllegalArgumentException .class , () -> processor3 .execute (ingestDocument ));
241
247
assertThat (e .getMessage (), equalTo ("field [foo] already exists" ));
242
248
}
243
249
244
- private RenameProcessor createRenameProcessor (String field , String targetField , boolean ignoreMissing ) {
250
+ private RenameProcessor createRenameProcessor (String field , String targetField , boolean ignoreMissing , boolean overrideTarget ) {
245
251
return new RenameProcessor (
246
252
randomAlphaOfLength (10 ),
247
253
null ,
248
254
new TestTemplateService .MockTemplateScript .Factory (field ),
249
255
new TestTemplateService .MockTemplateScript .Factory (targetField ),
250
- ignoreMissing
256
+ ignoreMissing ,
257
+ overrideTarget
251
258
);
252
259
}
253
260
}
0 commit comments