Search in sources :

Example 91 with Processor

use of org.elasticsearch.ingest.Processor in project elasticsearch by elastic.

the class ConvertProcessorTests method testConvertString.

public void testConvertString() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    Object fieldValue;
    String expectedFieldValue;
    switch(randomIntBetween(0, 2)) {
        case 0:
            float randomFloat = randomFloat();
            fieldValue = randomFloat;
            expectedFieldValue = Float.toString(randomFloat);
            break;
        case 1:
            int randomInt = randomInt();
            fieldValue = randomInt;
            expectedFieldValue = Integer.toString(randomInt);
            break;
        case 2:
            boolean randomBoolean = randomBoolean();
            fieldValue = randomBoolean;
            expectedFieldValue = Boolean.toString(randomBoolean);
            break;
        default:
            throw new UnsupportedOperationException();
    }
    String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
    Processor processor = new ConvertProcessor(randomAsciiOfLength(10), fieldName, fieldName, Type.STRING, false);
    processor.execute(ingestDocument);
    assertThat(ingestDocument.getFieldValue(fieldName, String.class), equalTo(expectedFieldValue));
}
Also used : Processor(org.elasticsearch.ingest.Processor) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 92 with Processor

use of org.elasticsearch.ingest.Processor in project elasticsearch by elastic.

the class ConvertProcessorTests method testConvertFloatError.

public void testConvertFloatError() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
    String fieldName = RandomDocumentPicks.randomFieldName(random());
    String value = "string-" + randomAsciiOfLengthBetween(1, 10);
    ingestDocument.setFieldValue(fieldName, value);
    Processor processor = new ConvertProcessor(randomAsciiOfLength(10), fieldName, fieldName, Type.FLOAT, false);
    try {
        processor.execute(ingestDocument);
        fail("processor execute should have failed");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), equalTo("unable to convert [" + value + "] to float"));
    }
}
Also used : Processor(org.elasticsearch.ingest.Processor) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 93 with Processor

use of org.elasticsearch.ingest.Processor in project elasticsearch by elastic.

the class ConvertProcessorTests method testConvertBooleanList.

public void testConvertBooleanList() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    int numItems = randomIntBetween(1, 10);
    List<String> fieldValue = new ArrayList<>();
    List<Boolean> expectedList = new ArrayList<>();
    for (int j = 0; j < numItems; j++) {
        boolean randomBoolean = randomBoolean();
        String booleanString = Boolean.toString(randomBoolean);
        if (randomBoolean) {
            booleanString = booleanString.toUpperCase(Locale.ROOT);
        }
        fieldValue.add(booleanString);
        expectedList.add(randomBoolean);
    }
    String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
    Processor processor = new ConvertProcessor(randomAsciiOfLength(10), fieldName, fieldName, Type.BOOLEAN, false);
    processor.execute(ingestDocument);
    assertThat(ingestDocument.getFieldValue(fieldName, List.class), equalTo(expectedList));
}
Also used : Processor(org.elasticsearch.ingest.Processor) ArrayList(java.util.ArrayList) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument) ArrayList(java.util.ArrayList) List(java.util.List) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 94 with Processor

use of org.elasticsearch.ingest.Processor in project elasticsearch by elastic.

the class ConvertProcessorTests method testAutoConvertStringNotMatched.

public void testAutoConvertStringNotMatched() throws Exception {
    String value = "notAnIntFloatOrBool";
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("field", value));
    Processor processor = new ConvertProcessor(randomAsciiOfLength(10), "field", "field", Type.AUTO, false);
    processor.execute(ingestDocument);
    Object convertedValue = ingestDocument.getFieldValue("field", Object.class);
    assertThat(convertedValue, sameInstance(value));
}
Also used : Processor(org.elasticsearch.ingest.Processor) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 95 with Processor

use of org.elasticsearch.ingest.Processor in project elasticsearch by elastic.

the class ConvertProcessorTests method testConvertNullFieldWithIgnoreMissing.

public void testConvertNullFieldWithIgnoreMissing() throws Exception {
    IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("field", null));
    IngestDocument ingestDocument = new IngestDocument(originalIngestDocument);
    Type type = randomFrom(Type.values());
    Processor processor = new ConvertProcessor(randomAsciiOfLength(10), "field", "field", type, true);
    processor.execute(ingestDocument);
    assertIngestDocument(originalIngestDocument, ingestDocument);
}
Also used : Type(org.elasticsearch.ingest.common.ConvertProcessor.Type) Processor(org.elasticsearch.ingest.Processor) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument)

Aggregations

Processor (org.elasticsearch.ingest.Processor)101 IngestDocument (org.elasticsearch.ingest.IngestDocument)97 IngestDocumentMatcher.assertIngestDocument (org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument)57 Matchers.containsString (org.hamcrest.Matchers.containsString)48 ArrayList (java.util.ArrayList)25 List (java.util.List)24 HashMap (java.util.HashMap)13 SortOrder (org.elasticsearch.ingest.common.SortProcessor.SortOrder)11 TestProcessor (org.elasticsearch.ingest.TestProcessor)7 TestTemplateService (org.elasticsearch.ingest.TestTemplateService)6 Map (java.util.Map)5 CompoundProcessor (org.elasticsearch.ingest.CompoundProcessor)5 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)4 Type (org.elasticsearch.ingest.common.ConvertProcessor.Type)4 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 Locale (java.util.Locale)3 TemplateService (org.elasticsearch.ingest.TemplateService)3 ESTestCase (org.elasticsearch.test.ESTestCase)3 Matchers.equalTo (org.hamcrest.Matchers.equalTo)3