Search in sources :

Example 1 with IngestDocument

use of org.opensearch.ingest.IngestDocument in project OpenSearch by opensearch-project.

the class DotExpanderProcessorTests method testEscapeFields_path.

public void testEscapeFields_path() throws Exception {
    Map<String, Object> source = new HashMap<>();
    source.put("foo", new HashMap<>(Collections.singletonMap("bar.baz", "value")));
    IngestDocument document = new IngestDocument(source, Collections.emptyMap());
    DotExpanderProcessor processor = new DotExpanderProcessor("_tag", null, "foo", "bar.baz");
    processor.execute(document);
    assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1));
    assertThat(document.getFieldValue("foo.bar", Map.class).size(), equalTo(1));
    assertThat(document.getFieldValue("foo.bar.baz", String.class), equalTo("value"));
    source = new HashMap<>();
    source.put("field", new HashMap<>(Collections.singletonMap("foo.bar.baz", "value")));
    document = new IngestDocument(source, Collections.emptyMap());
    processor = new DotExpanderProcessor("_tag", null, "field", "foo.bar.baz");
    processor.execute(document);
    assertThat(document.getFieldValue("field.foo", Map.class).size(), equalTo(1));
    assertThat(document.getFieldValue("field.foo.bar", Map.class).size(), equalTo(1));
    assertThat(document.getFieldValue("field.foo.bar.baz", String.class), equalTo("value"));
}
Also used : HashMap(java.util.HashMap) IngestDocument(org.opensearch.ingest.IngestDocument)

Example 2 with IngestDocument

use of org.opensearch.ingest.IngestDocument in project OpenSearch by opensearch-project.

the class DotExpanderProcessorTests method testEscapeFields_valueField.

public void testEscapeFields_valueField() throws Exception {
    Map<String, Object> source = new HashMap<>();
    source.put("foo.bar", "baz1");
    source.put("foo", "baz2");
    IngestDocument document1 = new IngestDocument(source, Collections.emptyMap());
    Processor processor1 = new DotExpanderProcessor("_tag", null, null, "foo.bar");
    // foo already exists and if a leaf field and therefor can't be replaced by a map field:
    Exception e = expectThrows(IllegalArgumentException.class, () -> processor1.execute(document1));
    assertThat(e.getMessage(), equalTo("cannot expend [foo.bar], because [foo] is not an object field, but a value field"));
    // so because foo is no branch field but a value field the `foo.bar` field can't be expanded
    // into [foo].[bar], so foo should be renamed first into `[foo].[bar]:
    IngestDocument document = new IngestDocument(source, Collections.emptyMap());
    Processor processor = new RenameProcessor("_tag", null, new TestTemplateService.MockTemplateScript.Factory("foo"), new TestTemplateService.MockTemplateScript.Factory("foo.bar"), false);
    processor.execute(document);
    processor = new DotExpanderProcessor("_tag", null, null, "foo.bar");
    processor.execute(document);
    assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1));
    assertThat(document.getFieldValue("foo.bar.0", String.class), equalTo("baz2"));
    assertThat(document.getFieldValue("foo.bar.1", String.class), equalTo("baz1"));
    source = new HashMap<>();
    source.put("foo.bar", "baz1");
    document = new IngestDocument(source, Collections.emptyMap());
    processor = new DotExpanderProcessor("_tag", null, null, "foo.bar");
    processor.execute(document);
    assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1));
    assertThat(document.getFieldValue("foo.bar", String.class), equalTo("baz1"));
    source = new HashMap<>();
    source.put("foo.bar.baz", "baz1");
    source.put("foo", new HashMap<>(Collections.singletonMap("bar", new HashMap<>())));
    document = new IngestDocument(source, Collections.emptyMap());
    processor = new DotExpanderProcessor("_tag", null, null, "foo.bar.baz");
    processor.execute(document);
    assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1));
    assertThat(document.getFieldValue("foo.bar", Map.class).size(), equalTo(1));
    assertThat(document.getFieldValue("foo.bar.baz", String.class), equalTo("baz1"));
    source = new HashMap<>();
    source.put("foo.bar.baz", "baz1");
    source.put("foo", new HashMap<>(Collections.singletonMap("bar", "baz2")));
    IngestDocument document2 = new IngestDocument(source, Collections.emptyMap());
    Processor processor2 = new DotExpanderProcessor("_tag", null, null, "foo.bar.baz");
    e = expectThrows(IllegalArgumentException.class, () -> processor2.execute(document2));
    assertThat(e.getMessage(), equalTo("cannot expend [foo.bar.baz], because [foo.bar] is not an object field, but a value field"));
}
Also used : Processor(org.opensearch.ingest.Processor) HashMap(java.util.HashMap) IngestDocument(org.opensearch.ingest.IngestDocument)

Example 3 with IngestDocument

use of org.opensearch.ingest.IngestDocument in project OpenSearch by opensearch-project.

the class DotExpanderProcessorTests method testEscapeFields.

public void testEscapeFields() throws Exception {
    Map<String, Object> source = new HashMap<>();
    source.put("foo.bar", "baz1");
    IngestDocument document = new IngestDocument(source, Collections.emptyMap());
    DotExpanderProcessor processor = new DotExpanderProcessor("_tag", null, null, "foo.bar");
    processor.execute(document);
    assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1));
    assertThat(document.getFieldValue("foo.bar", String.class), equalTo("baz1"));
    source = new HashMap<>();
    source.put("foo.bar.baz", "value");
    document = new IngestDocument(source, Collections.emptyMap());
    processor = new DotExpanderProcessor("_tag", null, null, "foo.bar.baz");
    processor.execute(document);
    assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1));
    assertThat(document.getFieldValue("foo.bar", Map.class).size(), equalTo(1));
    assertThat(document.getFieldValue("foo.bar.baz", String.class), equalTo("value"));
    source = new HashMap<>();
    source.put("foo.bar", "baz1");
    source.put("foo", new HashMap<>(Collections.singletonMap("bar", "baz2")));
    document = new IngestDocument(source, Collections.emptyMap());
    processor = new DotExpanderProcessor("_tag", null, null, "foo.bar");
    processor.execute(document);
    assertThat(document.getSourceAndMetadata().size(), equalTo(1));
    assertThat(document.getFieldValue("foo.bar", List.class).size(), equalTo(2));
    assertThat(document.getFieldValue("foo.bar.0", String.class), equalTo("baz2"));
    assertThat(document.getFieldValue("foo.bar.1", String.class), equalTo("baz1"));
    source = new HashMap<>();
    source.put("foo.bar", "2");
    source.put("foo", new HashMap<>(Collections.singletonMap("bar", 1)));
    document = new IngestDocument(source, Collections.emptyMap());
    processor = new DotExpanderProcessor("_tag", null, null, "foo.bar");
    processor.execute(document);
    assertThat(document.getSourceAndMetadata().size(), equalTo(1));
    assertThat(document.getFieldValue("foo.bar", List.class).size(), equalTo(2));
    assertThat(document.getFieldValue("foo.bar.0", Integer.class), equalTo(1));
    assertThat(document.getFieldValue("foo.bar.1", String.class), equalTo("2"));
}
Also used : HashMap(java.util.HashMap) IngestDocument(org.opensearch.ingest.IngestDocument)

Example 4 with IngestDocument

use of org.opensearch.ingest.IngestDocument in project OpenSearch by opensearch-project.

the class FailProcessorTests method test.

public void test() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    String message = randomAlphaOfLength(10);
    Processor processor = new FailProcessor(randomAlphaOfLength(10), null, new TestTemplateService.MockTemplateScript.Factory(message));
    try {
        processor.execute(ingestDocument);
        fail("fail processor should throw an exception");
    } catch (FailProcessorException e) {
        assertThat(e.getMessage(), equalTo(message));
    }
}
Also used : Processor(org.opensearch.ingest.Processor) IngestDocument(org.opensearch.ingest.IngestDocument)

Example 5 with IngestDocument

use of org.opensearch.ingest.IngestDocument in project OpenSearch by opensearch-project.

the class GrokProcessorTests method testNoMatchingPatternName.

public void testNoMatchingPatternName() {
    String fieldName = RandomDocumentPicks.randomFieldName(random());
    IngestDocument doc = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
    doc.setFieldValue(fieldName, "23");
    Exception e = expectThrows(IllegalArgumentException.class, () -> new GrokProcessor(randomAlphaOfLength(10), null, Collections.singletonMap("ONE", "1"), Collections.singletonList("%{NOTONE:not_one}"), fieldName, false, false, MatcherWatchdog.noop()));
    assertThat(e.getMessage(), equalTo("Unable to find pattern [NOTONE] in Grok's pattern dictionary"));
}
Also used : IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument)

Aggregations

IngestDocument (org.opensearch.ingest.IngestDocument)252 IngestDocumentMatcher.assertIngestDocument (org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument)150 Processor (org.opensearch.ingest.Processor)136 Matchers.containsString (org.hamcrest.Matchers.containsString)98 HashMap (java.util.HashMap)86 ArrayList (java.util.ArrayList)51 Map (java.util.Map)37 List (java.util.List)36 GeoIpCache (org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache)19 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)13 TestProcessor (org.opensearch.ingest.TestProcessor)12 SortOrder (org.opensearch.ingest.common.SortProcessor.SortOrder)12 Arrays (java.util.Arrays)11 Collectors (java.util.stream.Collectors)11 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)11 LinkedHashMap (java.util.LinkedHashMap)9 RandomDocumentPicks (org.opensearch.ingest.RandomDocumentPicks)9 Name (com.carrotsearch.randomizedtesting.annotations.Name)7 ParametersFactory (com.carrotsearch.randomizedtesting.annotations.ParametersFactory)7 LinkedList (java.util.LinkedList)7