Search in sources :

Example 31 with IngestDocument

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

the class JsonProcessorTests method testExecute.

@SuppressWarnings("unchecked")
public void testExecute() throws Exception {
    String processorTag = randomAsciiOfLength(3);
    String randomField = randomAsciiOfLength(3);
    String randomTargetField = randomAsciiOfLength(2);
    JsonProcessor jsonProcessor = new JsonProcessor(processorTag, randomField, randomTargetField, false);
    Map<String, Object> document = new HashMap<>();
    Map<String, Object> randomJsonMap = RandomDocumentPicks.randomSource(random());
    XContentBuilder builder = JsonXContent.contentBuilder().map(randomJsonMap);
    String randomJson = XContentHelper.convertToJson(builder.bytes(), false);
    document.put(randomField, randomJson);
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    jsonProcessor.execute(ingestDocument);
    Map<String, Object> jsonified = ingestDocument.getFieldValue(randomTargetField, Map.class);
    assertIngestDocument(ingestDocument.getFieldValue(randomTargetField, Object.class), jsonified);
}
Also used : HashMap(java.util.HashMap) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 32 with IngestDocument

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

the class JsonProcessorTests method testAddToRoot.

@SuppressWarnings("unchecked")
public void testAddToRoot() throws Exception {
    String processorTag = randomAsciiOfLength(3);
    String randomTargetField = randomAsciiOfLength(2);
    JsonProcessor jsonProcessor = new JsonProcessor(processorTag, "a", randomTargetField, true);
    Map<String, Object> document = new HashMap<>();
    String json = "{\"a\": 1, \"b\": 2}";
    document.put("a", json);
    document.put("c", "see");
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    jsonProcessor.execute(ingestDocument);
    Map<String, Object> expected = new HashMap<>();
    expected.put("a", 1);
    expected.put("b", 2);
    expected.put("c", "see");
    IngestDocument expectedIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), expected);
    assertIngestDocument(ingestDocument, expectedIngestDocument);
}
Also used : HashMap(java.util.HashMap) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument)

Example 33 with IngestDocument

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

the class JsonProcessorTests method testInvalidJson.

public void testInvalidJson() {
    JsonProcessor jsonProcessor = new JsonProcessor("tag", "field", "target_field", false);
    Map<String, Object> document = new HashMap<>();
    document.put("field", "invalid json");
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    Exception exception = expectThrows(IllegalArgumentException.class, () -> jsonProcessor.execute(ingestDocument));
    assertThat(exception.getCause().getCause().getMessage(), equalTo("Unrecognized token" + " 'invalid': was expecting ('true', 'false' or 'null')\n" + " at [Source: invalid json; line: 1, column: 8]"));
}
Also used : HashMap(java.util.HashMap) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument)

Example 34 with IngestDocument

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

the class KeyValueProcessorTests method testFailFieldSplitMatch.

public void testFailFieldSplitMatch() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "first=hello|second=world|second=universe");
    Processor processor = new KeyValueProcessor(randomAsciiOfLength(10), fieldName, "&", "=", null, "target", false);
    processor.execute(ingestDocument);
    assertThat(ingestDocument.getFieldValue("target.first", String.class), equalTo("hello|second=world|second=universe"));
    assertFalse(ingestDocument.hasField("target.second"));
}
Also used : Processor(org.elasticsearch.ingest.Processor) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument)

Example 35 with IngestDocument

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

the class ForEachProcessorTests method testMetaDataAvailable.

public void testMetaDataAvailable() throws Exception {
    List<Map<String, Object>> values = new ArrayList<>();
    values.add(new HashMap<>());
    values.add(new HashMap<>());
    IngestDocument ingestDocument = new IngestDocument("_index", "_type", "_id", null, null, Collections.singletonMap("values", values));
    TestProcessor innerProcessor = new TestProcessor(id -> {
        id.setFieldValue("_ingest._value.index", id.getSourceAndMetadata().get("_index"));
        id.setFieldValue("_ingest._value.type", id.getSourceAndMetadata().get("_type"));
        id.setFieldValue("_ingest._value.id", id.getSourceAndMetadata().get("_id"));
    });
    ForEachProcessor processor = new ForEachProcessor("_tag", "values", innerProcessor);
    processor.execute(ingestDocument);
    assertThat(innerProcessor.getInvokedCounter(), equalTo(2));
    assertThat(ingestDocument.getFieldValue("values.0.index", String.class), equalTo("_index"));
    assertThat(ingestDocument.getFieldValue("values.0.type", String.class), equalTo("_type"));
    assertThat(ingestDocument.getFieldValue("values.0.id", String.class), equalTo("_id"));
    assertThat(ingestDocument.getFieldValue("values.1.index", String.class), equalTo("_index"));
    assertThat(ingestDocument.getFieldValue("values.1.type", String.class), equalTo("_type"));
    assertThat(ingestDocument.getFieldValue("values.1.id", String.class), equalTo("_id"));
}
Also used : ArrayList(java.util.ArrayList) IngestDocument(org.elasticsearch.ingest.IngestDocument) TestProcessor(org.elasticsearch.ingest.TestProcessor) HashMap(java.util.HashMap) Map(java.util.Map)

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