Search in sources :

Example 96 with IngestDocument

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

the class ForEachProcessorTests method testNestedForEach.

public void testNestedForEach() throws Exception {
    List<Map<String, Object>> values = new ArrayList<>();
    List<Object> innerValues = new ArrayList<>();
    innerValues.add("abc");
    innerValues.add("def");
    Map<String, Object> value = new HashMap<>();
    value.put("values2", innerValues);
    values.add(value);
    innerValues = new ArrayList<>();
    innerValues.add("ghi");
    innerValues.add("jkl");
    value = new HashMap<>();
    value.put("values2", innerValues);
    values.add(value);
    IngestDocument ingestDocument = new IngestDocument("_index", "_type", "_id", null, null, Collections.singletonMap("values1", values));
    TestProcessor testProcessor = new TestProcessor(doc -> doc.setFieldValue("_ingest._value", doc.getFieldValue("_ingest._value", String.class).toUpperCase(Locale.ENGLISH)));
    ForEachProcessor processor = new ForEachProcessor("_tag", "values1", new ForEachProcessor("_tag", "_ingest._value.values2", testProcessor));
    processor.execute(ingestDocument);
    List result = ingestDocument.getFieldValue("values1.0.values2", List.class);
    assertThat(result.get(0), equalTo("ABC"));
    assertThat(result.get(1), equalTo("DEF"));
    result = ingestDocument.getFieldValue("values1.1.values2", List.class);
    assertThat(result.get(0), equalTo("GHI"));
    assertThat(result.get(1), equalTo("JKL"));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IngestDocument(org.elasticsearch.ingest.IngestDocument) TestProcessor(org.elasticsearch.ingest.TestProcessor) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 97 with IngestDocument

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

the class ForEachProcessorTests method testScalarValueAllowsUnderscoreValueFieldToRemainAccessible.

public void testScalarValueAllowsUnderscoreValueFieldToRemainAccessible() throws Exception {
    List<Object> values = new ArrayList<>();
    values.add("please");
    values.add("change");
    values.add("me");
    Map<String, Object> source = new HashMap<>();
    source.put("_value", "new_value");
    source.put("values", values);
    IngestDocument ingestDocument = new IngestDocument("_index", "_type", "_id", null, null, source);
    TestProcessor processor = new TestProcessor(doc -> doc.setFieldValue("_ingest._value", doc.getFieldValue("_source._value", String.class)));
    ForEachProcessor forEachProcessor = new ForEachProcessor("_tag", "values", processor);
    forEachProcessor.execute(ingestDocument);
    List result = ingestDocument.getFieldValue("values", List.class);
    assertThat(result.get(0), equalTo("new_value"));
    assertThat(result.get(1), equalTo("new_value"));
    assertThat(result.get(2), equalTo("new_value"));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IngestDocument(org.elasticsearch.ingest.IngestDocument) TestProcessor(org.elasticsearch.ingest.TestProcessor) ArrayList(java.util.ArrayList) List(java.util.List)

Example 98 with IngestDocument

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

the class GrokProcessorTests method testNotStringFieldWithIgnoreMissing.

public void testNotStringFieldWithIgnoreMissing() {
    String fieldName = RandomDocumentPicks.randomFieldName(random());
    IngestDocument doc = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
    doc.setFieldValue(fieldName, 1);
    GrokProcessor processor = new GrokProcessor(randomAsciiOfLength(10), Collections.singletonMap("ONE", "1"), Collections.singletonList("%{ONE:one}"), fieldName, false, true);
    Exception e = expectThrows(Exception.class, () -> processor.execute(doc));
    assertThat(e.getMessage(), equalTo("field [" + fieldName + "] of type [java.lang.Integer] cannot be cast to [java.lang.String]"));
}
Also used : IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument)

Example 99 with IngestDocument

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

the class GrokProcessorTests method testNullField.

public void testNullField() {
    String fieldName = RandomDocumentPicks.randomFieldName(random());
    IngestDocument doc = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
    doc.setFieldValue(fieldName, null);
    GrokProcessor processor = new GrokProcessor(randomAsciiOfLength(10), Collections.singletonMap("ONE", "1"), Collections.singletonList("%{ONE:one}"), fieldName, false, false);
    Exception e = expectThrows(Exception.class, () -> processor.execute(doc));
    assertThat(e.getMessage(), equalTo("field [" + fieldName + "] is null, cannot process it."));
}
Also used : IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument)

Example 100 with IngestDocument

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

the class GrokProcessorTests method testMissingField.

public void testMissingField() {
    String fieldName = "foo.bar";
    IngestDocument doc = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
    GrokProcessor processor = new GrokProcessor(randomAsciiOfLength(10), Collections.singletonMap("ONE", "1"), Collections.singletonList("%{ONE:one}"), fieldName, false, false);
    Exception e = expectThrows(Exception.class, () -> processor.execute(doc));
    assertThat(e.getMessage(), equalTo("field [foo] not present as part of path [foo.bar]"));
}
Also used : IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument)

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