|
49 | 49 | import org.opensearch.common.Numbers;
|
50 | 50 | import org.opensearch.common.settings.Settings;
|
51 | 51 | import org.opensearch.common.xcontent.XContentFactory;
|
| 52 | +import org.opensearch.common.xcontent.XContentType; |
52 | 53 | import org.opensearch.core.rest.RestStatus;
|
53 | 54 | import org.opensearch.core.xcontent.MediaTypeRegistry;
|
54 | 55 | import org.opensearch.core.xcontent.XContentBuilder;
|
|
63 | 64 | import org.opensearch.search.SearchHit;
|
64 | 65 | import org.opensearch.search.SearchHits;
|
65 | 66 | import org.opensearch.test.InternalSettingsPlugin;
|
| 67 | +import org.opensearch.test.OpenSearchTestCase; |
66 | 68 | import org.opensearch.test.ParameterizedDynamicSettingsOpenSearchIntegTestCase;
|
67 | 69 | import org.hamcrest.Matchers;
|
68 | 70 |
|
|
82 | 84 | import java.util.Set;
|
83 | 85 | import java.util.TreeMap;
|
84 | 86 | import java.util.concurrent.ExecutionException;
|
| 87 | +import java.util.concurrent.atomic.AtomicInteger; |
85 | 88 | import java.util.function.Function;
|
| 89 | +import java.util.function.Supplier; |
86 | 90 |
|
87 | 91 | import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder;
|
88 | 92 | import static org.opensearch.index.query.QueryBuilders.functionScoreQuery;
|
@@ -2609,4 +2613,99 @@ public void testSimpleSortsPoints() throws Exception {
|
2609 | 2613 |
|
2610 | 2614 | assertThat(searchResponse.toString(), not(containsString("error")));
|
2611 | 2615 | }
|
| 2616 | + |
| 2617 | + public void testSortMixedIntegerNumericFields() throws Exception { |
| 2618 | + internalCluster().ensureAtLeastNumDataNodes(3); |
| 2619 | + AtomicInteger counter = new AtomicInteger(); |
| 2620 | + index("long", () -> Long.MAX_VALUE - counter.getAndIncrement()); |
| 2621 | + index("integer", () -> Integer.MAX_VALUE - counter.getAndIncrement()); |
| 2622 | + SearchResponse searchResponse = client().prepareSearch("long", "integer") |
| 2623 | + .setQuery(matchAllQuery()) |
| 2624 | + .setSize(10) |
| 2625 | + .addSort(SortBuilders.fieldSort("field").order(SortOrder.ASC).sortMode(SortMode.MAX)) |
| 2626 | + .get(); |
| 2627 | + assertNoFailures(searchResponse); |
| 2628 | + long[] sortValues = new long[10]; |
| 2629 | + for (int i = 0; i < 10; i++) { |
| 2630 | + sortValues[i] = ((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).longValue(); |
| 2631 | + } |
| 2632 | + for (int i = 1; i < 10; i++) { |
| 2633 | + assertThat(Arrays.toString(sortValues), sortValues[i - 1], lessThan(sortValues[i])); |
| 2634 | + } |
| 2635 | + } |
| 2636 | + |
| 2637 | + public void testSortMixedFloatingNumericFields() throws Exception { |
| 2638 | + internalCluster().ensureAtLeastNumDataNodes(3); |
| 2639 | + AtomicInteger counter = new AtomicInteger(); |
| 2640 | + index("double", () -> 100.5 - counter.getAndIncrement()); |
| 2641 | + counter.set(0); |
| 2642 | + index("float", () -> 200.5 - counter.getAndIncrement()); |
| 2643 | + counter.set(0); |
| 2644 | + index("half_float", () -> 300.5 - counter.getAndIncrement()); |
| 2645 | + SearchResponse searchResponse = client().prepareSearch("double", "float", "half_float") |
| 2646 | + .setQuery(matchAllQuery()) |
| 2647 | + .setSize(15) |
| 2648 | + .addSort(SortBuilders.fieldSort("field").order(SortOrder.ASC).sortMode(SortMode.MAX)) |
| 2649 | + .get(); |
| 2650 | + assertNoFailures(searchResponse); |
| 2651 | + double[] sortValues = new double[15]; |
| 2652 | + for (int i = 0; i < 15; i++) { |
| 2653 | + sortValues[i] = ((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).doubleValue(); |
| 2654 | + } |
| 2655 | + for (int i = 1; i < 15; i++) { |
| 2656 | + assertThat(Arrays.toString(sortValues), sortValues[i - 1], lessThan(sortValues[i])); |
| 2657 | + } |
| 2658 | + } |
| 2659 | + |
| 2660 | + public void testSortMixedFloatingAndIntegerNumericFields() throws Exception { |
| 2661 | + internalCluster().ensureAtLeastNumDataNodes(3); |
| 2662 | + index("long", () -> randomLongBetween(0, (long) 2E53 - 1)); |
| 2663 | + index("integer", OpenSearchTestCase::randomInt); |
| 2664 | + index("double", OpenSearchTestCase::randomDouble); |
| 2665 | + index("float", () -> randomFloat()); |
| 2666 | + boolean asc = randomBoolean(); |
| 2667 | + SearchResponse searchResponse = client().prepareSearch("long", "integer", "double", "float") |
| 2668 | + .setQuery(matchAllQuery()) |
| 2669 | + .setSize(20) |
| 2670 | + .addSort(SortBuilders.fieldSort("field").order(asc ? SortOrder.ASC : SortOrder.DESC).sortMode(SortMode.MAX)) |
| 2671 | + .get(); |
| 2672 | + assertNoFailures(searchResponse); |
| 2673 | + double[] sortValues = new double[20]; |
| 2674 | + for (int i = 0; i < 20; i++) { |
| 2675 | + sortValues[i] = ((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).doubleValue(); |
| 2676 | + } |
| 2677 | + if (asc) { |
| 2678 | + for (int i = 1; i < 20; i++) { |
| 2679 | + assertThat(Arrays.toString(sortValues), sortValues[i - 1], lessThanOrEqualTo(sortValues[i])); |
| 2680 | + } |
| 2681 | + } else { |
| 2682 | + for (int i = 1; i < 20; i++) { |
| 2683 | + assertThat(Arrays.toString(sortValues), sortValues[i - 1], greaterThanOrEqualTo(sortValues[i])); |
| 2684 | + } |
| 2685 | + } |
| 2686 | + } |
| 2687 | + |
| 2688 | + private void index(String type, Supplier<Number> valueSupplier) throws Exception { |
| 2689 | + assertAcked( |
| 2690 | + prepareCreate(type).setMapping( |
| 2691 | + XContentFactory.jsonBuilder() |
| 2692 | + .startObject() |
| 2693 | + .startObject("properties") |
| 2694 | + .startObject("field") |
| 2695 | + .field("type", type) |
| 2696 | + .endObject() |
| 2697 | + .endObject() |
| 2698 | + .endObject() |
| 2699 | + ).setSettings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 0)) |
| 2700 | + ); |
| 2701 | + ensureGreen(type); |
| 2702 | + for (int i = 0; i < 5; i++) { |
| 2703 | + client().prepareIndex(type) |
| 2704 | + .setId(Integer.toString(i)) |
| 2705 | + .setSource("{\"field\" : " + valueSupplier.get() + " }", XContentType.JSON) |
| 2706 | + .get(); |
| 2707 | + } |
| 2708 | + client().admin().indices().prepareRefresh(type).get(); |
| 2709 | + } |
| 2710 | + |
2612 | 2711 | }
|
0 commit comments