64
64
import org .opensearch .search .SearchHit ;
65
65
import org .opensearch .search .SearchHits ;
66
66
import org .opensearch .test .InternalSettingsPlugin ;
67
+ import org .opensearch .test .OpenSearchTestCase ;
67
68
import org .opensearch .test .ParameterizedDynamicSettingsOpenSearchIntegTestCase ;
68
69
import org .hamcrest .Matchers ;
69
70
83
84
import java .util .Set ;
84
85
import java .util .TreeMap ;
85
86
import java .util .concurrent .ExecutionException ;
87
+ import java .util .concurrent .atomic .AtomicInteger ;
86
88
import java .util .function .Function ;
89
+ import java .util .function .Supplier ;
87
90
88
91
import static org .opensearch .common .xcontent .XContentFactory .jsonBuilder ;
89
92
import static org .opensearch .index .query .QueryBuilders .functionScoreQuery ;
@@ -2611,10 +2614,11 @@ public void testSimpleSortsPoints() throws Exception {
2611
2614
assertThat (searchResponse .toString (), not (containsString ("error" )));
2612
2615
}
2613
2616
2614
- public void testSortMixedNumericFields () throws Exception {
2617
+ public void testSortMixedIntegerNumericFields () throws Exception {
2615
2618
internalCluster ().ensureAtLeastNumDataNodes (3 );
2616
- index ("long" , Long .MAX_VALUE );
2617
- index ("integer" , Integer .MAX_VALUE );
2619
+ AtomicInteger counter = new AtomicInteger ();
2620
+ index ("long" , () -> Long .MAX_VALUE - counter .getAndIncrement ());
2621
+ index ("integer" , () -> Integer .MAX_VALUE - counter .getAndIncrement ());
2618
2622
SearchResponse searchResponse = client ().prepareSearch ("long" , "integer" )
2619
2623
.setQuery (matchAllQuery ())
2620
2624
.setSize (10 )
@@ -2630,7 +2634,58 @@ public void testSortMixedNumericFields() throws Exception {
2630
2634
}
2631
2635
}
2632
2636
2633
- private void index (String type , long end ) throws Exception {
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 {
2634
2689
assertAcked (
2635
2690
prepareCreate (type ).setMapping (
2636
2691
XContentFactory .jsonBuilder ()
@@ -2645,8 +2700,12 @@ private void index(String type, long end) throws Exception {
2645
2700
);
2646
2701
ensureGreen (type );
2647
2702
for (int i = 0 ; i < 5 ; i ++) {
2648
- client ().prepareIndex (type ).setId (Integer .toString (i )).setSource ("{\" field\" : " + (end - i ) + " }" , XContentType .JSON ).get ();
2703
+ client ().prepareIndex (type )
2704
+ .setId (Integer .toString (i ))
2705
+ .setSource ("{\" field\" : " + valueSupplier .get () + " }" , XContentType .JSON )
2706
+ .get ();
2649
2707
}
2650
2708
client ().admin ().indices ().prepareRefresh (type ).get ();
2651
2709
}
2710
+
2652
2711
}
0 commit comments