Search in sources :

Example 46 with IngestDocument

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

the class RemoveProcessorTests method testRemoveFields.

public void testRemoveFields() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    String field = RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument);
    Processor processor = new RemoveProcessor(randomAlphaOfLength(10), null, Collections.singletonList(new TestTemplateService.MockTemplateScript.Factory(field)), false);
    processor.execute(ingestDocument);
    assertThat(ingestDocument.hasField(field), equalTo(false));
}
Also used : Processor(org.opensearch.ingest.Processor) IngestDocument(org.opensearch.ingest.IngestDocument) TestTemplateService(org.opensearch.ingest.TestTemplateService) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 47 with IngestDocument

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

the class DateIndexNameProcessorTests method testUnix.

public void testUnix() throws Exception {
    Function<String, ZonedDateTime> function = DateFormat.Unix.getFunction(null, ZoneOffset.UTC, null);
    DateIndexNameProcessor dateProcessor = createProcessor("_field", Collections.singletonList(function), ZoneOffset.UTC, "events-", "m", "yyyyMMdd");
    IngestDocument document = new IngestDocument("_index", "_id", null, null, null, Collections.singletonMap("_field", "1000.5"));
    dateProcessor.execute(document);
    assertThat(document.getSourceAndMetadata().get("_index"), equalTo("<events-{19700101||/m{yyyyMMdd|UTC}}>"));
}
Also used : ZonedDateTime(java.time.ZonedDateTime) IngestDocument(org.opensearch.ingest.IngestDocument)

Example 48 with IngestDocument

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

the class DateProcessorTests method testJavaPatternDefaultYear.

public void testJavaPatternDefaultYear() {
    String format = randomFrom("dd/MM", "8dd/MM");
    DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10), null, templatize(ZoneId.of("Europe/Amsterdam")), templatize(Locale.ENGLISH), "date_as_string", Collections.singletonList(format), "date_as_date");
    Map<String, Object> document = new HashMap<>();
    document.put("date_as_string", "12/06");
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    dateProcessor.execute(ingestDocument);
    assertThat(ingestDocument.getFieldValue("date_as_date", String.class), equalTo(ZonedDateTime.now(ZoneId.of("Europe/Amsterdam")).getYear() + "-06-12T00:00:00.000+02:00"));
}
Also used : HashMap(java.util.HashMap) IngestDocument(org.opensearch.ingest.IngestDocument) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 49 with IngestDocument

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

the class DateProcessorTests method testJavaPattern.

public void testJavaPattern() {
    DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10), null, templatize(ZoneId.of("Europe/Amsterdam")), templatize(Locale.ENGLISH), "date_as_string", Collections.singletonList("yyyy dd MM HH:mm:ss"), "date_as_date");
    Map<String, Object> document = new HashMap<>();
    document.put("date_as_string", "2010 12 06 11:05:15");
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    dateProcessor.execute(ingestDocument);
    assertThat(ingestDocument.getFieldValue("date_as_date", String.class), equalTo("2010-06-12T11:05:15.000+02:00"));
}
Also used : HashMap(java.util.HashMap) IngestDocument(org.opensearch.ingest.IngestDocument) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 50 with IngestDocument

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

the class DateProcessorTests method testJavaPatternMultipleFormats.

public void testJavaPatternMultipleFormats() {
    List<String> matchFormats = new ArrayList<>();
    matchFormats.add("yyyy dd MM");
    matchFormats.add("dd/MM/yyyy");
    matchFormats.add("dd-MM-yyyy");
    DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10), null, templatize(ZoneId.of("Europe/Amsterdam")), templatize(Locale.ENGLISH), "date_as_string", matchFormats, "date_as_date");
    Map<String, Object> document = new HashMap<>();
    document.put("date_as_string", "2010 12 06");
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    dateProcessor.execute(ingestDocument);
    assertThat(ingestDocument.getFieldValue("date_as_date", String.class), equalTo("2010-06-12T00:00:00.000+02:00"));
    document = new HashMap<>();
    document.put("date_as_string", "12/06/2010");
    ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    dateProcessor.execute(ingestDocument);
    assertThat(ingestDocument.getFieldValue("date_as_date", String.class), equalTo("2010-06-12T00:00:00.000+02:00"));
    document = new HashMap<>();
    document.put("date_as_string", "12-06-2010");
    ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    dateProcessor.execute(ingestDocument);
    assertThat(ingestDocument.getFieldValue("date_as_date", String.class), equalTo("2010-06-12T00:00:00.000+02:00"));
    document = new HashMap<>();
    document.put("date_as_string", "2010");
    ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    try {
        dateProcessor.execute(ingestDocument);
        fail("processor should have failed due to not supported date format");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("unable to parse date [2010]"));
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IngestDocument(org.opensearch.ingest.IngestDocument) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

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