Search in sources :

Example 1 with SortOrder

use of org.opensearch.ingest.common.SortProcessor.SortOrder in project OpenSearch by opensearch-project.

the class SortProcessorTests method testSortStrings.

public void testSortStrings() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    int numItems = randomIntBetween(1, 10);
    List<String> fieldValue = new ArrayList<>(numItems);
    List<String> expectedResult = new ArrayList<>(numItems);
    for (int j = 0; j < numItems; j++) {
        String value = randomAlphaOfLengthBetween(1, 10);
        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(randomAlphaOfLength(10), null, fieldName, order, fieldName);
    processor.execute(ingestDocument);
    assertEquals(ingestDocument.getFieldValue(fieldName, List.class), expectedResult);
}
Also used : Processor(org.opensearch.ingest.Processor) ArrayList(java.util.ArrayList) IngestDocument(org.opensearch.ingest.IngestDocument) SortOrder(org.opensearch.ingest.common.SortProcessor.SortOrder) List(java.util.List) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 2 with SortOrder

use of org.opensearch.ingest.common.SortProcessor.SortOrder in project OpenSearch by opensearch-project.

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(randomAlphaOfLength(10), null, fieldName, order, fieldName);
    processor.execute(ingestDocument);
    assertEquals(ingestDocument.getFieldValue(fieldName, List.class), expectedResult);
}
Also used : Processor(org.opensearch.ingest.Processor) ArrayList(java.util.ArrayList) IngestDocument(org.opensearch.ingest.IngestDocument) SortOrder(org.opensearch.ingest.common.SortProcessor.SortOrder) List(java.util.List) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 3 with SortOrder

use of org.opensearch.ingest.common.SortProcessor.SortOrder in project OpenSearch by opensearch-project.

the class SortProcessorTests method testSortFloats.

public void testSortFloats() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    int numItems = randomIntBetween(1, 10);
    List<Float> fieldValue = new ArrayList<>(numItems);
    List<Float> expectedResult = new ArrayList<>(numItems);
    for (int j = 0; j < numItems; j++) {
        Float value = randomFloat();
        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(randomAlphaOfLength(10), null, fieldName, order, fieldName);
    processor.execute(ingestDocument);
    assertEquals(ingestDocument.getFieldValue(fieldName, List.class), expectedResult);
}
Also used : Processor(org.opensearch.ingest.Processor) ArrayList(java.util.ArrayList) IngestDocument(org.opensearch.ingest.IngestDocument) SortOrder(org.opensearch.ingest.common.SortProcessor.SortOrder) List(java.util.List) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 4 with SortOrder

use of org.opensearch.ingest.common.SortProcessor.SortOrder in project OpenSearch by opensearch-project.

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(randomAlphaOfLength(10), null, fieldName, order, fieldName);
    try {
        processor.execute(ingestDocument);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("not present as part of path [" + fieldName + "]"));
    }
}
Also used : Processor(org.opensearch.ingest.Processor) IngestDocument(org.opensearch.ingest.IngestDocument) SortOrder(org.opensearch.ingest.common.SortProcessor.SortOrder) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 5 with SortOrder

use of org.opensearch.ingest.common.SortProcessor.SortOrder in project OpenSearch by opensearch-project.

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(randomAlphaOfLength(10), null, fieldName, order, fieldName);
    processor.execute(ingestDocument);
    assertEquals(ingestDocument.getFieldValue(fieldName, List.class), expectedResult);
}
Also used : Processor(org.opensearch.ingest.Processor) ArrayList(java.util.ArrayList) IngestDocument(org.opensearch.ingest.IngestDocument) SortOrder(org.opensearch.ingest.common.SortProcessor.SortOrder) List(java.util.List) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

IngestDocument (org.opensearch.ingest.IngestDocument)12 Processor (org.opensearch.ingest.Processor)12 SortOrder (org.opensearch.ingest.common.SortProcessor.SortOrder)12 Matchers.containsString (org.hamcrest.Matchers.containsString)11 ArrayList (java.util.ArrayList)9 List (java.util.List)9