|
32 | 32 |
|
33 | 33 | package org.opensearch.ingest.common;
|
34 | 34 |
|
| 35 | +import org.opensearch.common.lucene.uid.Versions; |
35 | 36 | import org.opensearch.index.VersionType;
|
36 | 37 | import org.opensearch.ingest.IngestDocument;
|
37 | 38 | import org.opensearch.ingest.Processor;
|
@@ -181,4 +182,100 @@ public void testRemoveMetadataField() throws Exception {
|
181 | 182 | }
|
182 | 183 | }
|
183 | 184 | }
|
| 185 | + |
| 186 | + public void testRemoveDocumentId() throws Exception { |
| 187 | + Map<String, Object> config = new HashMap<>(); |
| 188 | + config.put("field", IngestDocument.Metadata.ID.getFieldName()); |
| 189 | + String processorTag = randomAlphaOfLength(10); |
| 190 | + |
| 191 | + // test remove _id when _version_type is external |
| 192 | + IngestDocument ingestDocumentWithExternalVersionType = new IngestDocument( |
| 193 | + RandomDocumentPicks.randomString(random()), |
| 194 | + RandomDocumentPicks.randomString(random()), |
| 195 | + RandomDocumentPicks.randomString(random()), |
| 196 | + 1L, |
| 197 | + VersionType.EXTERNAL, |
| 198 | + RandomDocumentPicks.randomSource(random()) |
| 199 | + ); |
| 200 | + |
| 201 | + Processor processorForExternalVersionType = new RemoveProcessor.Factory(TestTemplateService.instance()).create( |
| 202 | + null, |
| 203 | + processorTag, |
| 204 | + null, |
| 205 | + config |
| 206 | + ); |
| 207 | + assertThrows( |
| 208 | + "cannot remove metadata field [_id] when specifying external version for the document, version: " |
| 209 | + + 1 |
| 210 | + + ", version_type: " |
| 211 | + + VersionType.EXTERNAL, |
| 212 | + IllegalArgumentException.class, |
| 213 | + () -> processorForExternalVersionType.execute(ingestDocumentWithExternalVersionType) |
| 214 | + ); |
| 215 | + |
| 216 | + // test remove _id when _version_type is external_gte |
| 217 | + config.put("field", IngestDocument.Metadata.ID.getFieldName()); |
| 218 | + IngestDocument ingestDocumentWithExternalGTEVersionType = new IngestDocument( |
| 219 | + RandomDocumentPicks.randomString(random()), |
| 220 | + RandomDocumentPicks.randomString(random()), |
| 221 | + RandomDocumentPicks.randomString(random()), |
| 222 | + 1L, |
| 223 | + VersionType.EXTERNAL_GTE, |
| 224 | + RandomDocumentPicks.randomSource(random()) |
| 225 | + ); |
| 226 | + |
| 227 | + Processor processorForExternalGTEVersionType = new RemoveProcessor.Factory(TestTemplateService.instance()).create( |
| 228 | + null, |
| 229 | + processorTag, |
| 230 | + null, |
| 231 | + config |
| 232 | + ); |
| 233 | + assertThrows( |
| 234 | + "cannot remove metadata field [_id] when specifying external version for the document, version: " |
| 235 | + + 1 |
| 236 | + + ", version_type: " |
| 237 | + + VersionType.EXTERNAL_GTE, |
| 238 | + IllegalArgumentException.class, |
| 239 | + () -> processorForExternalGTEVersionType.execute(ingestDocumentWithExternalGTEVersionType) |
| 240 | + ); |
| 241 | + |
| 242 | + // test remove _id when _version_type is internal |
| 243 | + config.put("field", IngestDocument.Metadata.ID.getFieldName()); |
| 244 | + IngestDocument ingestDocumentWithInternalVersionType = new IngestDocument( |
| 245 | + RandomDocumentPicks.randomString(random()), |
| 246 | + RandomDocumentPicks.randomString(random()), |
| 247 | + RandomDocumentPicks.randomString(random()), |
| 248 | + Versions.MATCH_ANY, |
| 249 | + VersionType.INTERNAL, |
| 250 | + RandomDocumentPicks.randomSource(random()) |
| 251 | + ); |
| 252 | + |
| 253 | + Processor processorForInternalVersionType = new RemoveProcessor.Factory(TestTemplateService.instance()).create( |
| 254 | + null, |
| 255 | + processorTag, |
| 256 | + null, |
| 257 | + config |
| 258 | + ); |
| 259 | + processorForInternalVersionType.execute(ingestDocumentWithInternalVersionType); |
| 260 | + assertThat(ingestDocumentWithInternalVersionType.hasField(IngestDocument.Metadata.ID.getFieldName()), equalTo(false)); |
| 261 | + |
| 262 | + // test remove _id when _version_type is null |
| 263 | + config.put("field", IngestDocument.Metadata.ID.getFieldName()); |
| 264 | + IngestDocument ingestDocumentWithNoVersionType = new IngestDocument( |
| 265 | + RandomDocumentPicks.randomString(random()), |
| 266 | + RandomDocumentPicks.randomString(random()), |
| 267 | + RandomDocumentPicks.randomString(random()), |
| 268 | + null, |
| 269 | + null, |
| 270 | + RandomDocumentPicks.randomSource(random()) |
| 271 | + ); |
| 272 | + Processor processorForNullVersionType = new RemoveProcessor.Factory(TestTemplateService.instance()).create( |
| 273 | + null, |
| 274 | + processorTag, |
| 275 | + null, |
| 276 | + config |
| 277 | + ); |
| 278 | + processorForNullVersionType.execute(ingestDocumentWithNoVersionType); |
| 279 | + assertThat(ingestDocumentWithNoVersionType.hasField(IngestDocument.Metadata.ID.getFieldName()), equalTo(false)); |
| 280 | + } |
184 | 281 | }
|
0 commit comments