Search in sources :

Example 16 with Processor

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

the class DotExpanderProcessorTests method testEscapeFields_valueField.

public void testEscapeFields_valueField() throws Exception {
    Map<String, Object> source = new HashMap<>();
    source.put("foo.bar", "baz1");
    source.put("foo", "baz2");
    IngestDocument document1 = new IngestDocument(source, Collections.emptyMap());
    Processor processor1 = new DotExpanderProcessor("_tag", null, "foo.bar");
    // foo already exists and if a leaf field and therefor can't be replaced by a map field:
    Exception e = expectThrows(IllegalArgumentException.class, () -> processor1.execute(document1));
    assertThat(e.getMessage(), equalTo("cannot expend [foo.bar], because [foo] is not an object field, but a value field"));
    // so because foo is no branch field but a value field the `foo.bar` field can't be expanded
    // into [foo].[bar], so foo should be renamed first into `[foo].[bar]:
    IngestDocument document = new IngestDocument(source, Collections.emptyMap());
    Processor processor = new RenameProcessor("_tag", "foo", "foo.bar", false);
    processor.execute(document);
    processor = new DotExpanderProcessor("_tag", null, "foo.bar");
    processor.execute(document);
    assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1));
    assertThat(document.getFieldValue("foo.bar.0", String.class), equalTo("baz2"));
    assertThat(document.getFieldValue("foo.bar.1", String.class), equalTo("baz1"));
    source = new HashMap<>();
    source.put("foo.bar", "baz1");
    document = new IngestDocument(source, Collections.emptyMap());
    processor = new DotExpanderProcessor("_tag", null, "foo.bar");
    processor.execute(document);
    assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1));
    assertThat(document.getFieldValue("foo.bar", String.class), equalTo("baz1"));
    source = new HashMap<>();
    source.put("foo.bar.baz", "baz1");
    source.put("foo", new HashMap<>(Collections.singletonMap("bar", new HashMap<>())));
    document = new IngestDocument(source, Collections.emptyMap());
    processor = new DotExpanderProcessor("_tag", null, "foo.bar.baz");
    processor.execute(document);
    assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1));
    assertThat(document.getFieldValue("foo.bar", Map.class).size(), equalTo(1));
    assertThat(document.getFieldValue("foo.bar.baz", String.class), equalTo("baz1"));
    source = new HashMap<>();
    source.put("foo.bar.baz", "baz1");
    source.put("foo", new HashMap<>(Collections.singletonMap("bar", "baz2")));
    IngestDocument document2 = new IngestDocument(source, Collections.emptyMap());
    Processor processor2 = new DotExpanderProcessor("_tag", null, "foo.bar.baz");
    e = expectThrows(IllegalArgumentException.class, () -> processor2.execute(document2));
    assertThat(e.getMessage(), equalTo("cannot expend [foo.bar.baz], because [foo.bar] is not an object field, but a value field"));
}
Also used : Processor(org.elasticsearch.ingest.Processor) HashMap(java.util.HashMap) IngestDocument(org.elasticsearch.ingest.IngestDocument)

Example 17 with Processor

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

the class ForEachProcessorFactoryTests method testCreateWithMissingField.

public void testCreateWithMissingField() throws Exception {
    Processor processor = new TestProcessor(ingestDocument -> {
    });
    Map<String, Processor.Factory> registry = new HashMap<>();
    registry.put("_name", (r, t, c) -> processor);
    ForEachProcessor.Factory forEachFactory = new ForEachProcessor.Factory();
    Map<String, Object> config = new HashMap<>();
    config.put("processor", Collections.singletonList(Collections.singletonMap("_name", Collections.emptyMap())));
    Exception exception = expectThrows(Exception.class, () -> forEachFactory.create(registry, null, config));
    assertThat(exception.getMessage(), equalTo("[field] required property is missing"));
}
Also used : Processor(org.elasticsearch.ingest.Processor) TestProcessor(org.elasticsearch.ingest.TestProcessor) HashMap(java.util.HashMap) TestProcessor(org.elasticsearch.ingest.TestProcessor) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException)

Example 18 with Processor

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

the class ForEachProcessorFactoryTests method testCreateWithTooManyProcessorTypes.

public void testCreateWithTooManyProcessorTypes() throws Exception {
    Processor processor = new TestProcessor(ingestDocument -> {
    });
    Map<String, Processor.Factory> registry = new HashMap<>();
    registry.put("_first", (r, t, c) -> processor);
    registry.put("_second", (r, t, c) -> processor);
    ForEachProcessor.Factory forEachFactory = new ForEachProcessor.Factory();
    Map<String, Object> config = new HashMap<>();
    config.put("field", "_field");
    Map<String, Object> processorTypes = new HashMap<>();
    processorTypes.put("_first", Collections.emptyMap());
    processorTypes.put("_second", Collections.emptyMap());
    config.put("processor", processorTypes);
    Exception exception = expectThrows(ElasticsearchParseException.class, () -> forEachFactory.create(registry, null, config));
    assertThat(exception.getMessage(), equalTo("[processor] Must specify exactly one processor type"));
}
Also used : Processor(org.elasticsearch.ingest.Processor) TestProcessor(org.elasticsearch.ingest.TestProcessor) HashMap(java.util.HashMap) TestProcessor(org.elasticsearch.ingest.TestProcessor) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException)

Example 19 with Processor

use of org.elasticsearch.ingest.Processor 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 20 with Processor

use of org.elasticsearch.ingest.Processor 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)

Aggregations

Processor (org.elasticsearch.ingest.Processor)101 IngestDocument (org.elasticsearch.ingest.IngestDocument)97 IngestDocumentMatcher.assertIngestDocument (org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument)57 Matchers.containsString (org.hamcrest.Matchers.containsString)48 ArrayList (java.util.ArrayList)25 List (java.util.List)24 HashMap (java.util.HashMap)13 SortOrder (org.elasticsearch.ingest.common.SortProcessor.SortOrder)11 TestProcessor (org.elasticsearch.ingest.TestProcessor)7 TestTemplateService (org.elasticsearch.ingest.TestTemplateService)6 Map (java.util.Map)5 CompoundProcessor (org.elasticsearch.ingest.CompoundProcessor)5 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)4 Type (org.elasticsearch.ingest.common.ConvertProcessor.Type)4 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 Locale (java.util.Locale)3 TemplateService (org.elasticsearch.ingest.TemplateService)3 ESTestCase (org.elasticsearch.test.ESTestCase)3 Matchers.equalTo (org.hamcrest.Matchers.equalTo)3