Search in sources :

Example 81 with Processor

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

the class AbstractStringProcessorTestCase method testNonStringValue.

public void testNonStringValue() throws Exception {
    String fieldName = RandomDocumentPicks.randomFieldName(random());
    Processor processor = newProcessor(fieldName, false);
    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 82 with Processor

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

the class AppendProcessorTests method testAppendMetadata.

public void testAppendMetadata() throws Exception {
    //here any metadata field value becomes a list, which won't make sense in most of the cases,
    // but support for append is streamlined like for set so we test it
    IngestDocument.MetaData randomMetaData = randomFrom(IngestDocument.MetaData.values());
    List<String> values = new ArrayList<>();
    Processor appendProcessor;
    if (randomBoolean()) {
        String value = randomAsciiOfLengthBetween(1, 10);
        values.add(value);
        appendProcessor = createAppendProcessor(randomMetaData.getFieldName(), value);
    } else {
        int valuesSize = randomIntBetween(0, 10);
        for (int i = 0; i < valuesSize; i++) {
            values.add(randomAsciiOfLengthBetween(1, 10));
        }
        appendProcessor = createAppendProcessor(randomMetaData.getFieldName(), values);
    }
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    Object initialValue = ingestDocument.getSourceAndMetadata().get(randomMetaData.getFieldName());
    appendProcessor.execute(ingestDocument);
    List list = ingestDocument.getFieldValue(randomMetaData.getFieldName(), List.class);
    if (initialValue == null) {
        assertThat(list, equalTo(values));
    } else {
        assertThat(list.size(), equalTo(values.size() + 1));
        assertThat(list.get(0), equalTo(initialValue));
        for (int i = 1; i < list.size(); i++) {
            assertThat(list.get(i), equalTo(values.get(i - 1)));
        }
    }
}
Also used : Processor(org.elasticsearch.ingest.Processor) ArrayList(java.util.ArrayList) IngestDocument(org.elasticsearch.ingest.IngestDocument) ArrayList(java.util.ArrayList) List(java.util.List)

Example 83 with Processor

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

the class AppendProcessorTests method testAppendValuesToNonExistingList.

public void testAppendValuesToNonExistingList() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
    String field = RandomDocumentPicks.randomFieldName(random());
    Scalar scalar = randomFrom(Scalar.values());
    List<Object> values = new ArrayList<>();
    Processor appendProcessor;
    if (randomBoolean()) {
        Object value = scalar.randomValue();
        values.add(value);
        appendProcessor = createAppendProcessor(field, value);
    } else {
        int valuesSize = randomIntBetween(0, 10);
        for (int i = 0; i < valuesSize; i++) {
            values.add(scalar.randomValue());
        }
        appendProcessor = createAppendProcessor(field, values);
    }
    appendProcessor.execute(ingestDocument);
    List list = ingestDocument.getFieldValue(field, List.class);
    assertThat(list, not(sameInstance(values)));
    assertThat(list, equalTo(values));
}
Also used : Processor(org.elasticsearch.ingest.Processor) ArrayList(java.util.ArrayList) IngestDocument(org.elasticsearch.ingest.IngestDocument) ArrayList(java.util.ArrayList) List(java.util.List)

Example 84 with Processor

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

the class AppendProcessorTests method testConvertScalarToList.

public void testConvertScalarToList() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    Scalar scalar = randomFrom(Scalar.values());
    Object initialValue = scalar.randomValue();
    String field = RandomDocumentPicks.addRandomField(random(), ingestDocument, initialValue);
    List<Object> values = new ArrayList<>();
    Processor appendProcessor;
    if (randomBoolean()) {
        Object value = scalar.randomValue();
        values.add(value);
        appendProcessor = createAppendProcessor(field, value);
    } else {
        int valuesSize = randomIntBetween(0, 10);
        for (int i = 0; i < valuesSize; i++) {
            values.add(scalar.randomValue());
        }
        appendProcessor = createAppendProcessor(field, values);
    }
    appendProcessor.execute(ingestDocument);
    List fieldValue = ingestDocument.getFieldValue(field, List.class);
    assertThat(fieldValue.size(), equalTo(values.size() + 1));
    assertThat(fieldValue.get(0), equalTo(initialValue));
    for (int i = 1; i < values.size() + 1; i++) {
        assertThat(fieldValue.get(i), equalTo(values.get(i - 1)));
    }
}
Also used : Processor(org.elasticsearch.ingest.Processor) ArrayList(java.util.ArrayList) IngestDocument(org.elasticsearch.ingest.IngestDocument) ArrayList(java.util.ArrayList) List(java.util.List)

Example 85 with Processor

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

the class ConvertProcessorTests method testConvertInt.

public void testConvertInt() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    int randomInt = randomInt();
    String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, randomInt);
    Processor processor = new ConvertProcessor(randomAsciiOfLength(10), fieldName, fieldName, Type.INTEGER, false);
    processor.execute(ingestDocument);
    assertThat(ingestDocument.getFieldValue(fieldName, 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)

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