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));
}
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"));
}
}
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));
}
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));
}
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);
}
Aggregations