Search in sources :

Example 56 with IngestDocument

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

the class CsvProcessorTests method processDocument.

private IngestDocument processDocument(String[] headers, String csv, boolean trim, Object emptyValue) {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    Arrays.stream(headers).filter(ingestDocument::hasField).forEach(ingestDocument::removeField);
    String fieldName = randomAlphaOfLength(11);
    ingestDocument.setFieldValue(fieldName, csv);
    char quoteChar = quote.isEmpty() ? '"' : quote.charAt(0);
    CsvProcessor processor = new CsvProcessor(randomAlphaOfLength(5), null, fieldName, headers, trim, separator, quoteChar, false, emptyValue);
    processor.execute(ingestDocument);
    return ingestDocument;
}
Also used : IngestDocument(org.opensearch.ingest.IngestDocument)

Example 57 with IngestDocument

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

the class CsvProcessorTests method testEscapedQuote.

public void testEscapedQuote() {
    int numItems = randomIntBetween(2, 10);
    Map<String, String> items = new LinkedHashMap<>();
    for (int i = 0; i < numItems; i++) {
        items.put(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10) + quote + quote + randomAlphaOfLengthBetween(5, 10) + quote + quote);
    }
    String[] headers = items.keySet().toArray(new String[numItems]);
    String csv = items.values().stream().map(v -> quote + v + quote).collect(Collectors.joining(separator + ""));
    IngestDocument ingestDocument = processDocument(headers, csv);
    items.forEach((key, value) -> assertEquals(value.replace(quote + quote, quote), ingestDocument.getFieldValue(key, String.class)));
}
Also used : LinkedHashMap(java.util.LinkedHashMap) Arrays(java.util.Arrays) ParametersFactory(com.carrotsearch.randomizedtesting.annotations.ParametersFactory) IngestDocument(org.opensearch.ingest.IngestDocument) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Map(java.util.Map) Name(com.carrotsearch.randomizedtesting.annotations.Name) HashMap(java.util.HashMap) RandomDocumentPicks(org.opensearch.ingest.RandomDocumentPicks) LinkedList(java.util.LinkedList) Collectors(java.util.stream.Collectors) IngestDocument(org.opensearch.ingest.IngestDocument) LinkedHashMap(java.util.LinkedHashMap)

Example 58 with IngestDocument

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

the class CsvProcessorTests method testExactNumberOfFields.

public void testExactNumberOfFields() {
    int numItems = randomIntBetween(2, 10);
    Map<String, String> items = new LinkedHashMap<>();
    for (int i = 0; i < numItems; i++) {
        items.put(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10));
    }
    String[] headers = items.keySet().toArray(new String[numItems]);
    String csv = items.values().stream().map(v -> quote + v + quote).collect(Collectors.joining(separator + ""));
    IngestDocument ingestDocument = processDocument(headers, csv);
    items.forEach((key, value) -> assertEquals(value, ingestDocument.getFieldValue(key, String.class)));
}
Also used : LinkedHashMap(java.util.LinkedHashMap) Arrays(java.util.Arrays) ParametersFactory(com.carrotsearch.randomizedtesting.annotations.ParametersFactory) IngestDocument(org.opensearch.ingest.IngestDocument) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Map(java.util.Map) Name(com.carrotsearch.randomizedtesting.annotations.Name) HashMap(java.util.HashMap) RandomDocumentPicks(org.opensearch.ingest.RandomDocumentPicks) LinkedList(java.util.LinkedList) Collectors(java.util.stream.Collectors) IngestDocument(org.opensearch.ingest.IngestDocument) LinkedHashMap(java.util.LinkedHashMap)

Example 59 with IngestDocument

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

the class GrokProcessorTests method testMultiplePatternsWithMatchReturn.

public void testMultiplePatternsWithMatchReturn() throws Exception {
    String fieldName = RandomDocumentPicks.randomFieldName(random());
    IngestDocument doc = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
    doc.setFieldValue(fieldName, "2");
    Map<String, String> patternBank = new HashMap<>();
    patternBank.put("ONE", "1");
    patternBank.put("TWO", "2");
    patternBank.put("THREE", "3");
    GrokProcessor processor = new GrokProcessor(randomAlphaOfLength(10), null, patternBank, Arrays.asList("%{ONE:one}", "%{TWO:two}", "%{THREE:three}"), fieldName, false, false, MatcherWatchdog.noop());
    processor.execute(doc);
    assertThat(doc.hasField("one"), equalTo(false));
    assertThat(doc.getFieldValue("two", String.class), equalTo("2"));
    assertThat(doc.hasField("three"), equalTo(false));
}
Also used : HashMap(java.util.HashMap) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument)

Example 60 with IngestDocument

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

the class GrokProcessorTests method testIgnoreCase.

public void testIgnoreCase() throws Exception {
    String fieldName = RandomDocumentPicks.randomFieldName(random());
    IngestDocument doc = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
    doc.setFieldValue(fieldName, "A");
    GrokProcessor processor = new GrokProcessor(randomAlphaOfLength(10), null, Collections.emptyMap(), Collections.singletonList("(?<a>(?i)A)"), fieldName, false, false, MatcherWatchdog.noop());
    processor.execute(doc);
    assertThat(doc.getFieldValue("a", String.class), equalTo("A"));
}
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