Search in sources :

Example 1 with CompoundProcessor

use of org.opensearch.ingest.CompoundProcessor 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 2 with CompoundProcessor

use of org.opensearch.ingest.CompoundProcessor in project OpenSearch by opensearch-project.

the class SimulateExecutionService method executeDocument.

void executeDocument(Pipeline pipeline, IngestDocument ingestDocument, boolean verbose, BiConsumer<SimulateDocumentResult, Exception> handler) {
    if (verbose) {
        List<SimulateProcessorResult> processorResultList = new CopyOnWriteArrayList<>();
        CompoundProcessor verbosePipelineProcessor = decorate(pipeline.getCompoundProcessor(), null, processorResultList);
        Pipeline verbosePipeline = new Pipeline(pipeline.getId(), pipeline.getDescription(), pipeline.getVersion(), verbosePipelineProcessor);
        ingestDocument.executePipeline(verbosePipeline, (result, e) -> {
            handler.accept(new SimulateDocumentVerboseResult(processorResultList), e);
        });
    } else {
        ingestDocument.executePipeline(pipeline, (result, e) -> {
            if (e == null) {
                handler.accept(new SimulateDocumentBaseResult(result), null);
            } else {
                handler.accept(new SimulateDocumentBaseResult(e), null);
            }
        });
    }
}
Also used : CompoundProcessor(org.opensearch.ingest.CompoundProcessor) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Pipeline(org.opensearch.ingest.Pipeline)

Example 3 with CompoundProcessor

use of org.opensearch.ingest.CompoundProcessor in project OpenSearch by opensearch-project.

the class SimulateExecutionServiceTests method testDropDocumentVerboseExtraProcessor.

public void testDropDocumentVerboseExtraProcessor() throws Exception {
    TestProcessor processor1 = new TestProcessor(ingestDocument -> ingestDocument.setFieldValue("field1", "value"));
    Processor processor2 = new DropProcessor.Factory().create(Collections.emptyMap(), null, null, Collections.emptyMap());
    TestProcessor processor3 = new TestProcessor(ingestDocument -> ingestDocument.setFieldValue("field2", "value"));
    Pipeline pipeline = new Pipeline("_id", "_description", version, new CompoundProcessor(processor1, processor2, processor3));
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<SimulateDocumentResult> holder = new AtomicReference<>();
    executionService.executeDocument(pipeline, ingestDocument, true, (r, e) -> {
        holder.set(r);
        latch.countDown();
    });
    latch.await();
    SimulateDocumentResult actualItemResponse = holder.get();
    assertThat(processor1.getInvokedCounter(), equalTo(1));
    assertThat(processor3.getInvokedCounter(), equalTo(0));
    assertThat(actualItemResponse, instanceOf(SimulateDocumentVerboseResult.class));
    SimulateDocumentVerboseResult verboseResult = (SimulateDocumentVerboseResult) actualItemResponse;
    assertThat(verboseResult.getProcessorResults().size(), equalTo(2));
    assertThat(verboseResult.getProcessorResults().get(0).getIngestDocument(), notNullValue());
    assertThat(verboseResult.getProcessorResults().get(0).getFailure(), nullValue());
    assertThat(verboseResult.getProcessorResults().get(1).getIngestDocument(), nullValue());
    assertThat(verboseResult.getProcessorResults().get(1).getFailure(), nullValue());
}
Also used : Processor(org.opensearch.ingest.Processor) AbstractProcessor(org.opensearch.ingest.AbstractProcessor) CompoundProcessor(org.opensearch.ingest.CompoundProcessor) TestProcessor(org.opensearch.ingest.TestProcessor) DropProcessor(org.opensearch.ingest.DropProcessor) DropProcessor(org.opensearch.ingest.DropProcessor) CompoundProcessor(org.opensearch.ingest.CompoundProcessor) TestProcessor(org.opensearch.ingest.TestProcessor) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Pipeline(org.opensearch.ingest.Pipeline)

Example 4 with CompoundProcessor

use of org.opensearch.ingest.CompoundProcessor in project OpenSearch by opensearch-project.

the class SimulateExecutionServiceTests method testExecuteVerboseItemWithOnFailure.

public void testExecuteVerboseItemWithOnFailure() throws Exception {
    TestProcessor processor1 = new TestProcessor("processor_0", "mock", null, new RuntimeException("processor failed"));
    TestProcessor processor2 = new TestProcessor("processor_1", "mock", null, ingestDocument -> {
    });
    TestProcessor processor3 = new TestProcessor("processor_2", "mock", null, ingestDocument -> {
    });
    Pipeline pipeline = new Pipeline("_id", "_description", version, new CompoundProcessor(new CompoundProcessor(false, Collections.singletonList(processor1), Collections.singletonList(processor2)), processor3));
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<SimulateDocumentResult> holder = new AtomicReference<>();
    executionService.executeDocument(pipeline, ingestDocument, true, (r, e) -> {
        holder.set(r);
        latch.countDown();
    });
    latch.await();
    SimulateDocumentResult actualItemResponse = holder.get();
    assertThat(processor1.getInvokedCounter(), equalTo(1));
    assertThat(processor2.getInvokedCounter(), equalTo(1));
    assertThat(actualItemResponse, instanceOf(SimulateDocumentVerboseResult.class));
    SimulateDocumentVerboseResult simulateDocumentVerboseResult = (SimulateDocumentVerboseResult) actualItemResponse;
    assertThat(simulateDocumentVerboseResult.getProcessorResults().size(), equalTo(3));
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(0).getProcessorTag(), equalTo("processor_0"));
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(0).getIngestDocument(), nullValue());
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(0).getFailure(), instanceOf(RuntimeException.class));
    RuntimeException runtimeException = (RuntimeException) simulateDocumentVerboseResult.getProcessorResults().get(0).getFailure();
    assertThat(runtimeException.getMessage(), equalTo("processor failed"));
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(1).getProcessorTag(), equalTo("processor_1"));
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(1).getIngestDocument(), not(sameInstance(ingestDocument)));
    IngestDocument ingestDocumentWithOnFailureMetadata = new IngestDocument(ingestDocument);
    Map<String, Object> metadata = ingestDocumentWithOnFailureMetadata.getIngestMetadata();
    metadata.put(CompoundProcessor.ON_FAILURE_PROCESSOR_TYPE_FIELD, "mock");
    metadata.put(CompoundProcessor.ON_FAILURE_PROCESSOR_TAG_FIELD, "processor_0");
    metadata.put(CompoundProcessor.ON_FAILURE_MESSAGE_FIELD, "processor failed");
    assertVerboseResult(simulateDocumentVerboseResult.getProcessorResults().get(1), pipeline.getId(), ingestDocumentWithOnFailureMetadata);
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(1).getFailure(), nullValue());
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(2).getProcessorTag(), equalTo("processor_2"));
    assertVerboseResult(simulateDocumentVerboseResult.getProcessorResults().get(2), pipeline.getId(), ingestDocument);
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(2).getFailure(), nullValue());
}
Also used : CompoundProcessor(org.opensearch.ingest.CompoundProcessor) TestProcessor(org.opensearch.ingest.TestProcessor) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Pipeline(org.opensearch.ingest.Pipeline)

Example 5 with CompoundProcessor

use of org.opensearch.ingest.CompoundProcessor in project OpenSearch by opensearch-project.

the class SimulateExecutionServiceTests method testExecuteVerboseItemExceptionWithoutOnFailure.

public void testExecuteVerboseItemExceptionWithoutOnFailure() throws Exception {
    TestProcessor processor1 = new TestProcessor("processor_0", "mock", null, ingestDocument -> {
    });
    TestProcessor processor2 = new TestProcessor("processor_1", "mock", null, new RuntimeException("processor failed"));
    TestProcessor processor3 = new TestProcessor("processor_2", "mock", null, ingestDocument -> {
    });
    Pipeline pipeline = new Pipeline("_id", "_description", version, new CompoundProcessor(processor1, processor2, processor3));
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<SimulateDocumentResult> holder = new AtomicReference<>();
    executionService.executeDocument(pipeline, ingestDocument, true, (r, e) -> {
        holder.set(r);
        latch.countDown();
    });
    latch.await();
    SimulateDocumentResult actualItemResponse = holder.get();
    assertThat(processor1.getInvokedCounter(), equalTo(1));
    assertThat(processor2.getInvokedCounter(), equalTo(1));
    assertThat(processor3.getInvokedCounter(), equalTo(0));
    assertThat(actualItemResponse, instanceOf(SimulateDocumentVerboseResult.class));
    SimulateDocumentVerboseResult simulateDocumentVerboseResult = (SimulateDocumentVerboseResult) actualItemResponse;
    assertThat(simulateDocumentVerboseResult.getProcessorResults().size(), equalTo(2));
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(0).getProcessorTag(), equalTo("processor_0"));
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(0).getFailure(), nullValue());
    assertVerboseResult(simulateDocumentVerboseResult.getProcessorResults().get(0), pipeline.getId(), ingestDocument);
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(1).getProcessorTag(), equalTo("processor_1"));
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(1).getIngestDocument(), nullValue());
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(1).getFailure(), instanceOf(RuntimeException.class));
    RuntimeException runtimeException = (RuntimeException) simulateDocumentVerboseResult.getProcessorResults().get(1).getFailure();
    assertThat(runtimeException.getMessage(), equalTo("processor failed"));
}
Also used : CompoundProcessor(org.opensearch.ingest.CompoundProcessor) TestProcessor(org.opensearch.ingest.TestProcessor) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Pipeline(org.opensearch.ingest.Pipeline)

Aggregations

CompoundProcessor (org.opensearch.ingest.CompoundProcessor)15 TestProcessor (org.opensearch.ingest.TestProcessor)14 Pipeline (org.opensearch.ingest.Pipeline)13 CountDownLatch (java.util.concurrent.CountDownLatch)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 Processor (org.opensearch.ingest.Processor)6 AbstractProcessor (org.opensearch.ingest.AbstractProcessor)4 DropProcessor (org.opensearch.ingest.DropProcessor)4 IngestDocument (org.opensearch.ingest.IngestDocument)4 IngestDocumentMatcher.assertIngestDocument (org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument)4 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 BiConsumer (java.util.function.BiConsumer)3 Matchers.equalTo (org.hamcrest.Matchers.equalTo)3 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)3 Arrays (java.util.Arrays)2 Locale (java.util.Locale)2