Search in sources :

Example 86 with IngestDocument

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"));
}
Also used : ArrayList(java.util.ArrayList) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument) TestProcessor(org.opensearch.ingest.TestProcessor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 87 with IngestDocument

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));
}
Also used : IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument) TestProcessor(org.opensearch.ingest.TestProcessor)

Example 88 with IngestDocument

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(".")));
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) TestTemplateService(org.opensearch.ingest.TestTemplateService) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Processor(org.opensearch.ingest.Processor) ArrayList(java.util.ArrayList) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) List(java.util.List) IngestDocument(org.opensearch.ingest.IngestDocument) Locale(java.util.Locale) Map(java.util.Map) Matchers.equalTo(org.hamcrest.Matchers.equalTo) BiConsumer(java.util.function.BiConsumer) CompoundProcessor(org.opensearch.ingest.CompoundProcessor) TestProcessor(org.opensearch.ingest.TestProcessor) TemplateScript(org.opensearch.script.TemplateScript) Collections(java.util.Collections) Processor(org.opensearch.ingest.Processor) CompoundProcessor(org.opensearch.ingest.CompoundProcessor) TestProcessor(org.opensearch.ingest.TestProcessor) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument)

Example 89 with IngestDocument

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));
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) TestTemplateService(org.opensearch.ingest.TestTemplateService) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Processor(org.opensearch.ingest.Processor) ArrayList(java.util.ArrayList) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) List(java.util.List) IngestDocument(org.opensearch.ingest.IngestDocument) Locale(java.util.Locale) Map(java.util.Map) Matchers.equalTo(org.hamcrest.Matchers.equalTo) BiConsumer(java.util.function.BiConsumer) CompoundProcessor(org.opensearch.ingest.CompoundProcessor) TestProcessor(org.opensearch.ingest.TestProcessor) TemplateScript(org.opensearch.script.TemplateScript) Collections(java.util.Collections) ArrayList(java.util.ArrayList) CompoundProcessor(org.opensearch.ingest.CompoundProcessor) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument) TestTemplateService(org.opensearch.ingest.TestTemplateService) TemplateScript(org.opensearch.script.TemplateScript)

Example 90 with IngestDocument

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));
}
Also used : HashMap(java.util.HashMap) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoIpCache(org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache)

Aggregations

IngestDocument (org.opensearch.ingest.IngestDocument)252 IngestDocumentMatcher.assertIngestDocument (org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument)150 Processor (org.opensearch.ingest.Processor)136 Matchers.containsString (org.hamcrest.Matchers.containsString)98 HashMap (java.util.HashMap)86 ArrayList (java.util.ArrayList)51 Map (java.util.Map)37 List (java.util.List)36 GeoIpCache (org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache)19 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)13 TestProcessor (org.opensearch.ingest.TestProcessor)12 SortOrder (org.opensearch.ingest.common.SortProcessor.SortOrder)12 Arrays (java.util.Arrays)11 Collectors (java.util.stream.Collectors)11 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)11 LinkedHashMap (java.util.LinkedHashMap)9 RandomDocumentPicks (org.opensearch.ingest.RandomDocumentPicks)9 Name (com.carrotsearch.randomizedtesting.annotations.Name)7 ParametersFactory (com.carrotsearch.randomizedtesting.annotations.ParametersFactory)7 LinkedList (java.util.LinkedList)7