Search in sources :

Example 1 with TemplateService

use of org.elasticsearch.ingest.TemplateService 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 2 with TemplateService

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

ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Locale (java.util.Locale)2 Map (java.util.Map)2 CompoundProcessor (org.elasticsearch.ingest.CompoundProcessor)2 IngestDocument (org.elasticsearch.ingest.IngestDocument)2 Processor (org.elasticsearch.ingest.Processor)2 TemplateService (org.elasticsearch.ingest.TemplateService)2 TestProcessor (org.elasticsearch.ingest.TestProcessor)2 TestTemplateService (org.elasticsearch.ingest.TestTemplateService)2 ESTestCase (org.elasticsearch.test.ESTestCase)2 Matchers.equalTo (org.hamcrest.Matchers.equalTo)2