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);
}
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);
}
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]"));
}
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"));
}
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"));
}
Aggregations