Search in sources :

Example 96 with Processor

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

the class ConvertProcessorTests method testAutoConvertNotString.

public void testAutoConvertNotString() throws Exception {
    Object randomValue;
    switch(randomIntBetween(0, 2)) {
        case 0:
            float randomFloat = randomFloat();
            randomValue = randomFloat;
            break;
        case 1:
            int randomInt = randomInt();
            randomValue = randomInt;
            break;
        case 2:
            boolean randomBoolean = randomBoolean();
            randomValue = randomBoolean;
            break;
        default:
            throw new UnsupportedOperationException();
    }
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("field", randomValue));
    Processor processor = new ConvertProcessor(randomAsciiOfLength(10), "field", "field", Type.AUTO, false);
    processor.execute(ingestDocument);
    Object convertedValue = ingestDocument.getFieldValue("field", Object.class);
    assertThat(convertedValue, sameInstance(randomValue));
}
Also used : Processor(org.elasticsearch.ingest.Processor) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument)

Example 97 with Processor

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

the class ConvertProcessorTests method testTargetField.

public void testTargetField() throws Exception {
    IngestDocument ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>());
    int randomInt = randomInt();
    String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, String.valueOf(randomInt));
    String targetField = fieldName + randomAsciiOfLength(5);
    Processor processor = new ConvertProcessor(randomAsciiOfLength(10), fieldName, targetField, Type.INTEGER, false);
    processor.execute(ingestDocument);
    assertThat(ingestDocument.getFieldValue(fieldName, String.class), equalTo(String.valueOf(randomInt)));
    assertThat(ingestDocument.getFieldValue(targetField, Integer.class), equalTo(randomInt));
}
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 98 with Processor

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

the class ConvertProcessorTests method testConvertFloat.

public void testConvertFloat() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    Map<String, Float> expectedResult = new HashMap<>();
    float randomFloat = randomFloat();
    String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, randomFloat);
    expectedResult.put(fieldName, randomFloat);
    Processor processor = new ConvertProcessor(randomAsciiOfLength(10), fieldName, fieldName, Type.FLOAT, false);
    processor.execute(ingestDocument);
    assertThat(ingestDocument.getFieldValue(fieldName, Float.class), equalTo(randomFloat));
}
Also used : Processor(org.elasticsearch.ingest.Processor) HashMap(java.util.HashMap) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 99 with Processor

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

the class AbstractStringProcessorTestCase method testNonStringValueWithIgnoreMissing.

public void testNonStringValueWithIgnoreMissing() throws Exception {
    String fieldName = RandomDocumentPicks.randomFieldName(random());
    Processor processor = newProcessor(fieldName, true);
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
    ingestDocument.setFieldValue(fieldName, randomInt());
    Exception e = expectThrows(Exception.class, () -> processor.execute(ingestDocument));
    assertThat(e.getMessage(), equalTo("field [" + fieldName + "] of type [java.lang.Integer] cannot be cast to [java.lang.String]"));
}
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 100 with Processor

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

the class FailProcessorTests method test.

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

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