Search in sources :

Example 6 with SortOrder

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

the class SortProcessorTests method testSortShorts.

public void testSortShorts() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    int numItems = randomIntBetween(1, 10);
    List<Short> fieldValue = new ArrayList<>(numItems);
    List<Short> expectedResult = new ArrayList<>(numItems);
    for (int j = 0; j < numItems; j++) {
        Short value = randomShort();
        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 7 with SortOrder

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

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(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 8 with SortOrder

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

the class SortProcessorTests method testSortWithTargetFieldLeavesOriginalUntouched.

public void testSortWithTargetFieldLeavesOriginalUntouched() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap());
    List<Integer> fieldValue = Arrays.asList(1, 5, 4);
    List<Integer> expectedResult = new ArrayList<>(fieldValue);
    Collections.sort(expectedResult);
    SortOrder order = randomBoolean() ? SortOrder.ASCENDING : SortOrder.DESCENDING;
    if (order.equals(SortOrder.DESCENDING)) {
        Collections.reverse(expectedResult);
    }
    String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, new ArrayList<>(fieldValue));
    String targetFieldName = fieldName + "foo";
    Processor processor = new SortProcessor(randomAlphaOfLength(10), null, fieldName, order, targetFieldName);
    processor.execute(ingestDocument);
    assertEquals(ingestDocument.getFieldValue(targetFieldName, List.class), expectedResult);
    assertEquals(ingestDocument.getFieldValue(fieldName, List.class), fieldValue);
}
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 9 with SortOrder

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

the class SortProcessorTests method testSortIntegers.

public void testSortIntegers() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    int numItems = randomIntBetween(1, 10);
    List<Integer> fieldValue = new ArrayList<>(numItems);
    List<Integer> expectedResult = new ArrayList<>(numItems);
    for (int j = 0; j < numItems; j++) {
        Integer value = randomIntBetween(1, 100);
        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 10 with SortOrder

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

the class SortProcessorTests method testSortMixedStrings.

public void testSortMixedStrings() throws Exception {
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    int numItems = randomIntBetween(1, 10);
    List<String> fieldValue = new ArrayList<>(numItems);
    List<String> expectedResult = new ArrayList<>(numItems);
    String value;
    for (int j = 0; j < numItems; j++) {
        if (randomBoolean()) {
            value = String.valueOf(randomIntBetween(0, 100));
        } else {
            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)

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