use of org.opensearch.ingest.IngestDocument in project OpenSearch by opensearch-project.
the class ForEachProcessorTests method testMetadataAvailable.
public void testMetadataAvailable() throws Exception {
List<Map<String, Object>> values = new ArrayList<>();
values.add(new HashMap<>());
values.add(new HashMap<>());
IngestDocument ingestDocument = new IngestDocument("_index", "_id", null, null, null, Collections.singletonMap("values", values));
TestProcessor innerProcessor = new TestProcessor(id -> {
id.setFieldValue("_ingest._value.index", id.getSourceAndMetadata().get("_index"));
id.setFieldValue("_ingest._value.type", id.getSourceAndMetadata().get("_type"));
id.setFieldValue("_ingest._value.id", id.getSourceAndMetadata().get("_id"));
});
ForEachProcessor processor = new ForEachProcessor("_tag", null, "values", innerProcessor, false);
processor.execute(ingestDocument, (result, e) -> {
});
assertThat(innerProcessor.getInvokedCounter(), equalTo(2));
assertThat(ingestDocument.getFieldValue("values.0.index", String.class), equalTo("_index"));
assertThat(ingestDocument.getFieldValue("values.0.id", String.class), equalTo("_id"));
assertThat(ingestDocument.getFieldValue("values.1.index", String.class), equalTo("_index"));
assertThat(ingestDocument.getFieldValue("values.1.id", String.class), equalTo("_id"));
}
use of org.opensearch.ingest.IngestDocument in project OpenSearch by opensearch-project.
the class ForEachProcessorTests method testIgnoreMissing.
public void testIgnoreMissing() throws Exception {
IngestDocument originalIngestDocument = new IngestDocument("_index", "_id", null, null, null, Collections.emptyMap());
IngestDocument ingestDocument = new IngestDocument(originalIngestDocument);
TestProcessor testProcessor = new TestProcessor(doc -> {
});
ForEachProcessor processor = new ForEachProcessor("_tag", null, "_ingest._value", testProcessor, true);
processor.execute(ingestDocument, (result, e) -> {
});
assertIngestDocument(originalIngestDocument, ingestDocument);
assertThat(testProcessor.getInvokedCounter(), equalTo(0));
}
use of org.opensearch.ingest.IngestDocument in project OpenSearch by opensearch-project.
the class ForEachProcessorTests method testRandom.
public void testRandom() {
Processor innerProcessor = new Processor() {
@Override
public IngestDocument execute(IngestDocument ingestDocument) {
String existingValue = ingestDocument.getFieldValue("_ingest._value", String.class);
ingestDocument.setFieldValue("_ingest._value", existingValue + ".");
return ingestDocument;
}
@Override
public String getType() {
return null;
}
@Override
public String getTag() {
return null;
}
@Override
public String getDescription() {
return null;
}
};
int numValues = randomIntBetween(1, 10000);
List<String> values = IntStream.range(0, numValues).mapToObj(i -> "").collect(Collectors.toList());
IngestDocument ingestDocument = new IngestDocument("_index", "_id", null, null, null, Collections.singletonMap("values", values));
ForEachProcessor processor = new ForEachProcessor("_tag", null, "values", innerProcessor, false);
processor.execute(ingestDocument, (result, e) -> {
});
@SuppressWarnings("unchecked") List<String> result = ingestDocument.getFieldValue("values", List.class);
assertThat(result.size(), equalTo(numValues));
result.forEach(r -> assertThat(r, equalTo(".")));
}
use of org.opensearch.ingest.IngestDocument in project OpenSearch by opensearch-project.
the class ForEachProcessorTests method testModifyFieldsOutsideArray.
public void testModifyFieldsOutsideArray() throws Exception {
List<Object> values = new ArrayList<>();
values.add("string");
values.add(1);
values.add(null);
IngestDocument ingestDocument = new IngestDocument("_index", "_id", null, null, null, Collections.singletonMap("values", values));
TemplateScript.Factory template = new TestTemplateService.MockTemplateScript.Factory("errors");
ForEachProcessor processor = new ForEachProcessor("_tag", null, "values", new CompoundProcessor(false, org.opensearch.common.collect.List.of(new UppercaseProcessor("_tag_upper", null, "_ingest._value", false, "_ingest._value")), org.opensearch.common.collect.List.of(new AppendProcessor("_tag", null, template, (model) -> (Collections.singletonList("added")), true))), false);
processor.execute(ingestDocument, (result, e) -> {
});
List<?> result = ingestDocument.getFieldValue("values", List.class);
assertThat(result.get(0), equalTo("STRING"));
assertThat(result.get(1), equalTo(1));
assertThat(result.get(2), equalTo(null));
List<?> errors = ingestDocument.getFieldValue("errors", List.class);
assertThat(errors.size(), equalTo(2));
}
use of org.opensearch.ingest.IngestDocument in project OpenSearch by opensearch-project.
the class GeoIpProcessorTests method testAddressIsNotInTheDatabase.
public void testAddressIsNotInTheDatabase() throws Exception {
GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, new GeoIpCache(1000), false);
Map<String, Object> document = new HashMap<>();
document.put("source_field", "127.0.0.1");
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
processor.execute(ingestDocument);
assertThat(ingestDocument.getSourceAndMetadata().containsKey("target_field"), is(false));
}
Aggregations