Search in sources :

Example 6 with SortOrder

use of org.elasticsearch.ingest.common.SortProcessor.SortOrder in project elasticsearch by elastic.

the class SortProcessorTests method testSortDoubles.

public void testSortDoubles() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    int numItems = randomIntBetween(1, 10);
    List<Double> fieldValue = new ArrayList<>(numItems);
    List<Double> expectedResult = new ArrayList<>(numItems);
    for (int j = 0; j < numItems; j++) {
        Double value = randomDoubleBetween(0.0, 100.0, true);
        fieldValue.add(value);
        expectedResult.add(value);
    }
    Collections.sort(expectedResult);
    SortOrder order = randomBoolean() ? SortOrder.ASCENDING : SortOrder.DESCENDING;
    if (order.equals(SortOrder.DESCENDING)) {
        Collections.reverse(expectedResult);
    }
    String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
    Processor processor = new SortProcessor(randomAsciiOfLength(10), fieldName, order);
    processor.execute(ingestDocument);
    assertEquals(ingestDocument.getFieldValue(fieldName, List.class), expectedResult);
}
Also used : Processor(org.elasticsearch.ingest.Processor) ArrayList(java.util.ArrayList) IngestDocument(org.elasticsearch.ingest.IngestDocument) SortOrder(org.elasticsearch.ingest.common.SortProcessor.SortOrder) List(java.util.List) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 7 with SortOrder

use of org.elasticsearch.ingest.common.SortProcessor.SortOrder in project elasticsearch by elastic.

the class SortProcessorTests method testSortNonExistingField.

public void testSortNonExistingField() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
    String fieldName = RandomDocumentPicks.randomFieldName(random());
    SortOrder order = randomBoolean() ? SortOrder.ASCENDING : SortOrder.DESCENDING;
    Processor processor = new SortProcessor(randomAsciiOfLength(10), fieldName, order);
    try {
        processor.execute(ingestDocument);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("not present as part of path [" + fieldName + "]"));
    }
}
Also used : Processor(org.elasticsearch.ingest.Processor) IngestDocument(org.elasticsearch.ingest.IngestDocument) SortOrder(org.elasticsearch.ingest.common.SortProcessor.SortOrder) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 8 with SortOrder

use of org.elasticsearch.ingest.common.SortProcessor.SortOrder in project elasticsearch by elastic.

the class SortProcessorTests method testSortBooleans.

public void testSortBooleans() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    int numItems = randomIntBetween(1, 10);
    List<Boolean> fieldValue = new ArrayList<>(numItems);
    List<Boolean> expectedResult = new ArrayList<>(numItems);
    for (int j = 0; j < numItems; j++) {
        Boolean value = randomBoolean();
        fieldValue.add(value);
        expectedResult.add(value);
    }
    Collections.sort(expectedResult);
    SortOrder order = randomBoolean() ? SortOrder.ASCENDING : SortOrder.DESCENDING;
    if (order.equals(SortOrder.DESCENDING)) {
        Collections.reverse(expectedResult);
    }
    String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
    Processor processor = new SortProcessor(randomAsciiOfLength(10), fieldName, order);
    processor.execute(ingestDocument);
    assertEquals(ingestDocument.getFieldValue(fieldName, List.class), expectedResult);
}
Also used : Processor(org.elasticsearch.ingest.Processor) ArrayList(java.util.ArrayList) IngestDocument(org.elasticsearch.ingest.IngestDocument) SortOrder(org.elasticsearch.ingest.common.SortProcessor.SortOrder) List(java.util.List) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 9 with SortOrder

use of org.elasticsearch.ingest.common.SortProcessor.SortOrder in project elasticsearch by elastic.

the class SortProcessorTests method testSortBytes.

public void testSortBytes() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    int numItems = randomIntBetween(1, 10);
    List<Byte> fieldValue = new ArrayList<>(numItems);
    List<Byte> expectedResult = new ArrayList<>(numItems);
    for (int j = 0; j < numItems; j++) {
        Byte value = randomByte();
        fieldValue.add(value);
        expectedResult.add(value);
    }
    Collections.sort(expectedResult);
    SortOrder order = randomBoolean() ? SortOrder.ASCENDING : SortOrder.DESCENDING;
    if (order.equals(SortOrder.DESCENDING)) {
        Collections.reverse(expectedResult);
    }
    String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
    Processor processor = new SortProcessor(randomAsciiOfLength(10), fieldName, order);
    processor.execute(ingestDocument);
    assertEquals(ingestDocument.getFieldValue(fieldName, List.class), expectedResult);
}
Also used : Processor(org.elasticsearch.ingest.Processor) ArrayList(java.util.ArrayList) IngestDocument(org.elasticsearch.ingest.IngestDocument) SortOrder(org.elasticsearch.ingest.common.SortProcessor.SortOrder) List(java.util.List) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 10 with SortOrder

use of org.elasticsearch.ingest.common.SortProcessor.SortOrder in project elasticsearch by elastic.

the class SortProcessorTests method testSortNullValue.

public void testSortNullValue() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("field", null));
    SortOrder order = randomBoolean() ? SortOrder.ASCENDING : SortOrder.DESCENDING;
    Processor processor = new SortProcessor(randomAsciiOfLength(10), "field", order);
    try {
        processor.execute(ingestDocument);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), equalTo("field [field] is null, cannot sort."));
    }
}
Also used : Processor(org.elasticsearch.ingest.Processor) IngestDocument(org.elasticsearch.ingest.IngestDocument) SortOrder(org.elasticsearch.ingest.common.SortProcessor.SortOrder)

Aggregations

IngestDocument (org.elasticsearch.ingest.IngestDocument)11 Processor (org.elasticsearch.ingest.Processor)11 SortOrder (org.elasticsearch.ingest.common.SortProcessor.SortOrder)11 Matchers.containsString (org.hamcrest.Matchers.containsString)10 ArrayList (java.util.ArrayList)8 List (java.util.List)8