Search in sources :

Example 26 with IngestDocument

use of org.elasticsearch.ingest.IngestDocument in project elasticsearch by elastic.

the class ForEachProcessorTests method testExecuteWithFailure.

public void testExecuteWithFailure() throws Exception {
    IngestDocument ingestDocument = new IngestDocument("_index", "_type", "_id", null, null, Collections.singletonMap("values", Arrays.asList("a", "b", "c")));
    TestProcessor testProcessor = new TestProcessor(id -> {
        if ("c".equals(id.getFieldValue("_ingest._value", String.class))) {
            throw new RuntimeException("failure");
        }
    });
    ForEachProcessor processor = new ForEachProcessor("_tag", "values", testProcessor);
    try {
        processor.execute(ingestDocument);
        fail("exception expected");
    } catch (RuntimeException e) {
        assertThat(e.getMessage(), equalTo("failure"));
    }
    assertThat(testProcessor.getInvokedCounter(), equalTo(3));
    assertThat(ingestDocument.getFieldValue("values", List.class), equalTo(Arrays.asList("a", "b", "c")));
    testProcessor = new TestProcessor(id -> {
        String value = id.getFieldValue("_ingest._value", String.class);
        if ("c".equals(value)) {
            throw new RuntimeException("failure");
        } else {
            id.setFieldValue("_ingest._value", value.toUpperCase(Locale.ROOT));
        }
    });
    Processor onFailureProcessor = new TestProcessor(ingestDocument1 -> {
    });
    processor = new ForEachProcessor("_tag", "values", new CompoundProcessor(false, Arrays.asList(testProcessor), Arrays.asList(onFailureProcessor)));
    processor.execute(ingestDocument);
    assertThat(testProcessor.getInvokedCounter(), equalTo(3));
    assertThat(ingestDocument.getFieldValue("values", List.class), equalTo(Arrays.asList("A", "B", "c")));
}
Also used : Arrays(java.util.Arrays) Processor(org.elasticsearch.ingest.Processor) TestProcessor(org.elasticsearch.ingest.TestProcessor) HashMap(java.util.HashMap) CompoundProcessor(org.elasticsearch.ingest.CompoundProcessor) ArrayList(java.util.ArrayList) List(java.util.List) TemplateService(org.elasticsearch.ingest.TemplateService) Locale(java.util.Locale) Map(java.util.Map) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TestTemplateService(org.elasticsearch.ingest.TestTemplateService) ESTestCase(org.elasticsearch.test.ESTestCase) Collections(java.util.Collections) IngestDocument(org.elasticsearch.ingest.IngestDocument) Processor(org.elasticsearch.ingest.Processor) TestProcessor(org.elasticsearch.ingest.TestProcessor) CompoundProcessor(org.elasticsearch.ingest.CompoundProcessor) CompoundProcessor(org.elasticsearch.ingest.CompoundProcessor) IngestDocument(org.elasticsearch.ingest.IngestDocument) TestProcessor(org.elasticsearch.ingest.TestProcessor) ArrayList(java.util.ArrayList) List(java.util.List)

Example 27 with IngestDocument

use of org.elasticsearch.ingest.IngestDocument in project elasticsearch by elastic.

the class RenameProcessorTests method testRenameAtomicOperationSetFails.

public void testRenameAtomicOperationSetFails() throws Exception {
    Map<String, Object> source = new HashMap<String, Object>() {

        @Override
        public Object put(String key, Object value) {
            if (key.equals("new_field")) {
                throw new UnsupportedOperationException();
            }
            return super.put(key, value);
        }
    };
    source.put("list", Collections.singletonList("item"));
    IngestDocument ingestDocument = new IngestDocument(source, Collections.emptyMap());
    Processor processor = new RenameProcessor(randomAsciiOfLength(10), "list", "new_field", false);
    try {
        processor.execute(ingestDocument);
        fail("processor execute should have failed");
    } catch (UnsupportedOperationException e) {
        //the set failed, the old field has not been removed
        assertThat(ingestDocument.getSourceAndMetadata().containsKey("list"), equalTo(true));
        assertThat(ingestDocument.getSourceAndMetadata().containsKey("new_field"), equalTo(false));
    }
}
Also used : Processor(org.elasticsearch.ingest.Processor) HashMap(java.util.HashMap) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument)

Example 28 with IngestDocument

use of org.elasticsearch.ingest.IngestDocument in project elasticsearch by elastic.

the class RenameProcessorTests method testRenameAtomicOperationRemoveFails.

public void testRenameAtomicOperationRemoveFails() throws Exception {
    Map<String, Object> source = new HashMap<String, Object>() {

        @Override
        public Object remove(Object key) {
            if (key.equals("list")) {
                throw new UnsupportedOperationException();
            }
            return super.remove(key);
        }
    };
    source.put("list", Collections.singletonList("item"));
    IngestDocument ingestDocument = new IngestDocument(source, Collections.emptyMap());
    Processor processor = new RenameProcessor(randomAsciiOfLength(10), "list", "new_field", false);
    try {
        processor.execute(ingestDocument);
        fail("processor execute should have failed");
    } catch (UnsupportedOperationException e) {
        //the set failed, the old field has not been removed
        assertThat(ingestDocument.getSourceAndMetadata().containsKey("list"), equalTo(true));
        assertThat(ingestDocument.getSourceAndMetadata().containsKey("new_field"), equalTo(false));
    }
}
Also used : Processor(org.elasticsearch.ingest.Processor) HashMap(java.util.HashMap) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument)

Example 29 with IngestDocument

use of org.elasticsearch.ingest.IngestDocument in project elasticsearch by elastic.

the class ScriptProcessorTests method testScripting.

public void testScripting() throws Exception {
    int randomBytesIn = randomInt();
    int randomBytesOut = randomInt();
    int randomBytesTotal = randomBytesIn + randomBytesOut;
    ScriptService scriptService = mock(ScriptService.class);
    Script script = new Script("_script");
    ExecutableScript executableScript = mock(ExecutableScript.class);
    when(scriptService.executable(any(Script.class), any())).thenReturn(executableScript);
    Map<String, Object> document = new HashMap<>();
    document.put("bytes_in", randomInt());
    document.put("bytes_out", randomInt());
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    doAnswer(invocationOnMock -> {
        ingestDocument.setFieldValue("bytes_total", randomBytesTotal);
        return null;
    }).when(executableScript).run();
    ScriptProcessor processor = new ScriptProcessor(randomAsciiOfLength(10), script, scriptService);
    processor.execute(ingestDocument);
    assertThat(ingestDocument.getSourceAndMetadata(), hasKey("bytes_in"));
    assertThat(ingestDocument.getSourceAndMetadata(), hasKey("bytes_out"));
    assertThat(ingestDocument.getSourceAndMetadata(), hasKey("bytes_total"));
    assertThat(ingestDocument.getSourceAndMetadata().get("bytes_total"), is(randomBytesTotal));
}
Also used : ScriptService(org.elasticsearch.script.ScriptService) Script(org.elasticsearch.script.Script) ExecutableScript(org.elasticsearch.script.ExecutableScript) HashMap(java.util.HashMap) ExecutableScript(org.elasticsearch.script.ExecutableScript) IngestDocument(org.elasticsearch.ingest.IngestDocument)

Example 30 with IngestDocument

use of org.elasticsearch.ingest.IngestDocument in project elasticsearch by elastic.

the class JoinProcessorTests method testJoinNonExistingField.

public void testJoinNonExistingField() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
    String fieldName = RandomDocumentPicks.randomFieldName(random());
    Processor processor = new JoinProcessor(randomAsciiOfLength(10), fieldName, "-");
    try {
        processor.execute(ingestDocument);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("not present as part of path [" + fieldName + "]"));
    }
}
Also used : Processor(org.elasticsearch.ingest.Processor) IngestDocument(org.elasticsearch.ingest.IngestDocument) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

IngestDocument (org.elasticsearch.ingest.IngestDocument)170 IngestDocumentMatcher.assertIngestDocument (org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument)105 Processor (org.elasticsearch.ingest.Processor)97 Matchers.containsString (org.hamcrest.Matchers.containsString)57 HashMap (java.util.HashMap)52 ArrayList (java.util.ArrayList)33 List (java.util.List)27 Map (java.util.Map)22 InputStream (java.io.InputStream)12 GZIPInputStream (java.util.zip.GZIPInputStream)11 SortOrder (org.elasticsearch.ingest.common.SortProcessor.SortOrder)11 TestProcessor (org.elasticsearch.ingest.TestProcessor)9 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 IOException (java.io.IOException)6 CompoundProcessor (org.elasticsearch.ingest.CompoundProcessor)6 TestTemplateService (org.elasticsearch.ingest.TestTemplateService)6 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)4 StreamInput (org.elasticsearch.common.io.stream.StreamInput)4 Pipeline (org.elasticsearch.ingest.Pipeline)4 Type (org.elasticsearch.ingest.common.ConvertProcessor.Type)4