Search in sources :

Example 81 with IngestDocument

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

the class UserAgentProcessorTests method testSpider.

@SuppressWarnings("unchecked")
public void testSpider() throws Exception {
    Map<String, Object> document = new HashMap<>();
    document.put("source_field", "Mozilla/5.0 (compatible; EasouSpider; +http://www.easou.com/search/spider.html)");
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    processor.execute(ingestDocument);
    Map<String, Object> data = ingestDocument.getSourceAndMetadata();
    assertThat(data, hasKey("target_field"));
    Map<String, Object> target = (Map<String, Object>) data.get("target_field");
    assertThat(target.get("name"), is("EasouSpider"));
    assertNull(target.get("version"));
    assertNull(target.get("os"));
    Map<String, String> device = new HashMap<>();
    device.put("name", "Spider");
    assertThat(target.get("device"), is(device));
}
Also used : HashMap(java.util.HashMap) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument) HashMap(java.util.HashMap) Map(java.util.Map)

Example 82 with IngestDocument

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

the class UserAgentProcessorTests method testNullValueWithIgnoreMissing.

public void testNullValueWithIgnoreMissing() throws Exception {
    UserAgentProcessor processor = new UserAgentProcessor(randomAlphaOfLength(10), null, "source_field", "target_field", null, EnumSet.allOf(UserAgentProcessor.Property.class), true, true);
    IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("source_field", null));
    IngestDocument ingestDocument = new IngestDocument(originalIngestDocument);
    processor.execute(ingestDocument);
    assertIngestDocument(originalIngestDocument, ingestDocument);
}
Also used : IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument)

Example 83 with IngestDocument

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

the class RemoveProcessorTests method testIgnoreMissing.

public void testIgnoreMissing() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
    String fieldName = RandomDocumentPicks.randomFieldName(random());
    Map<String, Object> config = new HashMap<>();
    config.put("field", fieldName);
    config.put("ignore_missing", true);
    String processorTag = randomAlphaOfLength(10);
    Processor processor = new RemoveProcessor.Factory(TestTemplateService.instance()).create(null, processorTag, null, config);
    processor.execute(ingestDocument);
}
Also used : Processor(org.opensearch.ingest.Processor) HashMap(java.util.HashMap) IngestDocument(org.opensearch.ingest.IngestDocument) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 84 with IngestDocument

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

the class RenameProcessorTests method testRenameAtomicOperationRemoveFails.

public void testRenameAtomicOperationRemoveFails() throws Exception {
    Map<String, Object> source = new HashMap<String, Object>() {

        @Override
        public Object remove(Object key) {
            if (key.equals("list")) {
                throw new UnsupportedOperationException();
            }
            return super.remove(key);
        }
    };
    source.put("list", Collections.singletonList("item"));
    IngestDocument ingestDocument = new IngestDocument(source, Collections.emptyMap());
    Processor processor = createRenameProcessor("list", "new_field", false);
    try {
        processor.execute(ingestDocument);
        fail("processor execute should have failed");
    } catch (UnsupportedOperationException e) {
        // the set failed, the old field has not been removed
        assertThat(ingestDocument.getSourceAndMetadata().containsKey("list"), equalTo(true));
        assertThat(ingestDocument.getSourceAndMetadata().containsKey("new_field"), equalTo(false));
    }
}
Also used : Processor(org.opensearch.ingest.Processor) HashMap(java.util.HashMap) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument)

Example 85 with IngestDocument

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

the class RenameProcessorTests method testRenameArrayElement.

public void testRenameArrayElement() throws Exception {
    Map<String, Object> document = new HashMap<>();
    List<String> list = new ArrayList<>();
    list.add("item1");
    list.add("item2");
    list.add("item3");
    document.put("list", list);
    List<Map<String, String>> one = new ArrayList<>();
    one.add(Collections.singletonMap("one", "one"));
    one.add(Collections.singletonMap("two", "two"));
    document.put("one", one);
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    Processor processor = createRenameProcessor("list.0", "item", false);
    processor.execute(ingestDocument);
    Object actualObject = ingestDocument.getSourceAndMetadata().get("list");
    assertThat(actualObject, instanceOf(List.class));
    @SuppressWarnings("unchecked") List<String> actualList = (List<String>) actualObject;
    assertThat(actualList.size(), equalTo(2));
    assertThat(actualList.get(0), equalTo("item2"));
    assertThat(actualList.get(1), equalTo("item3"));
    actualObject = ingestDocument.getSourceAndMetadata().get("item");
    assertThat(actualObject, instanceOf(String.class));
    assertThat(actualObject, equalTo("item1"));
    processor = createRenameProcessor("list.0", "list.3", false);
    try {
        processor.execute(ingestDocument);
        fail("processor execute should have failed");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), equalTo("[3] is out of bounds for array with length [2] as part of path [list.3]"));
        assertThat(actualList.size(), equalTo(2));
        assertThat(actualList.get(0), equalTo("item2"));
        assertThat(actualList.get(1), equalTo("item3"));
    }
}
Also used : Processor(org.opensearch.ingest.Processor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

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