Search in sources :

Example 91 with IngestDocument

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

the class RenameProcessorTests method testRenameLeafIntoBranch.

public void testRenameLeafIntoBranch() throws Exception {
    Map<String, Object> source = new HashMap<>();
    source.put("foo", "bar");
    IngestDocument ingestDocument = new IngestDocument(source, Collections.emptyMap());
    Processor processor1 = new RenameProcessor(randomAsciiOfLength(10), "foo", "foo.bar", false);
    processor1.execute(ingestDocument);
    assertThat(ingestDocument.getFieldValue("foo", Map.class), equalTo(Collections.singletonMap("bar", "bar")));
    assertThat(ingestDocument.getFieldValue("foo.bar", String.class), equalTo("bar"));
    Processor processor2 = new RenameProcessor(randomAsciiOfLength(10), "foo.bar", "foo.bar.baz", false);
    processor2.execute(ingestDocument);
    assertThat(ingestDocument.getFieldValue("foo", Map.class), equalTo(Collections.singletonMap("bar", Collections.singletonMap("baz", "bar"))));
    assertThat(ingestDocument.getFieldValue("foo.bar", Map.class), equalTo(Collections.singletonMap("baz", "bar")));
    assertThat(ingestDocument.getFieldValue("foo.bar.baz", String.class), equalTo("bar"));
    // for fun lets try to restore it (which don't allow today)
    Processor processor3 = new RenameProcessor(randomAsciiOfLength(10), "foo.bar.baz", "foo", false);
    Exception e = expectThrows(IllegalArgumentException.class, () -> processor3.execute(ingestDocument));
    assertThat(e.getMessage(), equalTo("field [foo] already exists"));
}
Also used : Processor(org.elasticsearch.ingest.Processor) HashMap(java.util.HashMap) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument) HashMap(java.util.HashMap) Map(java.util.Map)

Example 92 with IngestDocument

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

the class RenameProcessorTests method testRename.

public void testRename() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    String fieldName = RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument);
    Object fieldValue = ingestDocument.getFieldValue(fieldName, Object.class);
    String newFieldName;
    do {
        newFieldName = RandomDocumentPicks.randomFieldName(random());
    } while (RandomDocumentPicks.canAddField(newFieldName, ingestDocument) == false || newFieldName.equals(fieldName));
    Processor processor = new RenameProcessor(randomAsciiOfLength(10), fieldName, newFieldName, false);
    processor.execute(ingestDocument);
    assertThat(ingestDocument.getFieldValue(newFieldName, Object.class), equalTo(fieldValue));
}
Also used : Processor(org.elasticsearch.ingest.Processor) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument)

Example 93 with IngestDocument

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

the class RenameProcessorTests method testRenameArrayElement.

public void testRenameArrayElement() throws Exception {
    Map<String, Object> document = new HashMap<>();
    List<String> list = new ArrayList<>();
    list.add("item1");
    list.add("item2");
    list.add("item3");
    document.put("list", list);
    List<Map<String, String>> one = new ArrayList<>();
    one.add(Collections.singletonMap("one", "one"));
    one.add(Collections.singletonMap("two", "two"));
    document.put("one", one);
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    Processor processor = new RenameProcessor(randomAsciiOfLength(10), "list.0", "item", false);
    processor.execute(ingestDocument);
    Object actualObject = ingestDocument.getSourceAndMetadata().get("list");
    assertThat(actualObject, instanceOf(List.class));
    @SuppressWarnings("unchecked") List<String> actualList = (List<String>) actualObject;
    assertThat(actualList.size(), equalTo(2));
    assertThat(actualList.get(0), equalTo("item2"));
    assertThat(actualList.get(1), equalTo("item3"));
    actualObject = ingestDocument.getSourceAndMetadata().get("item");
    assertThat(actualObject, instanceOf(String.class));
    assertThat(actualObject, equalTo("item1"));
    processor = new RenameProcessor(randomAsciiOfLength(10), "list.0", "list.3", false);
    try {
        processor.execute(ingestDocument);
        fail("processor execute should have failed");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), equalTo("[3] is out of bounds for array with length [2] as part of path [list.3]"));
        assertThat(actualList.size(), equalTo(2));
        assertThat(actualList.get(0), equalTo("item2"));
        assertThat(actualList.get(1), equalTo("item3"));
    }
}
Also used : Processor(org.elasticsearch.ingest.Processor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 94 with IngestDocument

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

the class ForEachProcessorTests method testRestOfTheDocumentIsAvailable.

public void testRestOfTheDocumentIsAvailable() throws Exception {
    List<Map<String, Object>> values = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        Map<String, Object> object = new HashMap<>();
        object.put("field", "value");
        values.add(object);
    }
    Map<String, Object> document = new HashMap<>();
    document.put("values", values);
    document.put("flat_values", new ArrayList<>());
    document.put("other", "value");
    IngestDocument ingestDocument = new IngestDocument("_index", "_type", "_id", null, null, document);
    TemplateService ts = TestTemplateService.instance();
    ForEachProcessor processor = new ForEachProcessor("_tag", "values", new SetProcessor("_tag", ts.compile("_ingest._value.new_field"), (model) -> model.get("other")));
    processor.execute(ingestDocument);
    assertThat(ingestDocument.getFieldValue("values.0.new_field", String.class), equalTo("value"));
    assertThat(ingestDocument.getFieldValue("values.1.new_field", String.class), equalTo("value"));
    assertThat(ingestDocument.getFieldValue("values.2.new_field", String.class), equalTo("value"));
    assertThat(ingestDocument.getFieldValue("values.3.new_field", String.class), equalTo("value"));
    assertThat(ingestDocument.getFieldValue("values.4.new_field", String.class), equalTo("value"));
}
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) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IngestDocument(org.elasticsearch.ingest.IngestDocument) HashMap(java.util.HashMap) Map(java.util.Map) TemplateService(org.elasticsearch.ingest.TemplateService) TestTemplateService(org.elasticsearch.ingest.TestTemplateService)

Example 95 with IngestDocument

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

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", "_type", "_id", null, null, Collections.singletonMap("values", values));
    TemplateService ts = TestTemplateService.instance();
    ForEachProcessor processor = new ForEachProcessor("_tag", "values", new CompoundProcessor(false, Collections.singletonList(new UppercaseProcessor("_tag_upper", "_ingest._value", false)), Collections.singletonList(new AppendProcessor("_tag", ts.compile("errors"), (model) -> (Collections.singletonList("added"))))));
    processor.execute(ingestDocument);
    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 : 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) ArrayList(java.util.ArrayList) CompoundProcessor(org.elasticsearch.ingest.CompoundProcessor) IngestDocument(org.elasticsearch.ingest.IngestDocument) ArrayList(java.util.ArrayList) List(java.util.List) TemplateService(org.elasticsearch.ingest.TemplateService) TestTemplateService(org.elasticsearch.ingest.TestTemplateService)

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