Skip to content

Commit c52c5ab

Browse files
committed
add more tests
Signed-off-by: panguixin <[email protected]>
1 parent c2c1003 commit c52c5ab

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

server/src/internalClusterTest/java/org/opensearch/search/sort/FieldSortIT.java

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import org.opensearch.search.SearchHit;
6565
import org.opensearch.search.SearchHits;
6666
import org.opensearch.test.InternalSettingsPlugin;
67+
import org.opensearch.test.OpenSearchTestCase;
6768
import org.opensearch.test.ParameterizedDynamicSettingsOpenSearchIntegTestCase;
6869
import org.hamcrest.Matchers;
6970

@@ -83,7 +84,9 @@
8384
import java.util.Set;
8485
import java.util.TreeMap;
8586
import java.util.concurrent.ExecutionException;
87+
import java.util.concurrent.atomic.AtomicInteger;
8688
import java.util.function.Function;
89+
import java.util.function.Supplier;
8790

8891
import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder;
8992
import static org.opensearch.index.query.QueryBuilders.functionScoreQuery;
@@ -2611,10 +2614,11 @@ public void testSimpleSortsPoints() throws Exception {
26112614
assertThat(searchResponse.toString(), not(containsString("error")));
26122615
}
26132616

2614-
public void testSortMixedNumericFields() throws Exception {
2617+
public void testSortMixedIntegerNumericFields() throws Exception {
26152618
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());
26182622
SearchResponse searchResponse = client().prepareSearch("long", "integer")
26192623
.setQuery(matchAllQuery())
26202624
.setSize(10)
@@ -2630,7 +2634,58 @@ public void testSortMixedNumericFields() throws Exception {
26302634
}
26312635
}
26322636

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 {
26342689
assertAcked(
26352690
prepareCreate(type).setMapping(
26362691
XContentFactory.jsonBuilder()
@@ -2645,8 +2700,12 @@ private void index(String type, long end) throws Exception {
26452700
);
26462701
ensureGreen(type);
26472702
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();
26492707
}
26502708
client().admin().indices().prepareRefresh(type).get();
26512709
}
2710+
26522711
}

server/src/main/java/org/opensearch/search/sort/SortedWiderNumericSortField.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import java.util.Comparator;
2525

2626
/**
27-
* Sorted numeric field for wider sort types,
28-
* to help sorting two different numeric types.
27+
* Sorted numeric field for wider sort types, to help sorting two different numeric types.
28+
* NOTE: the unsigned_long is not supported by widening sort since the unsigned_long could not be used with other types
2929
*
3030
* @opensearch.internal
3131
*/

0 commit comments

Comments
 (0)