Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
Signed-off-by: panguixin <[email protected]>
  • Loading branch information
bugmakerrrrrr committed Jan 9, 2025
1 parent e9b1cc6 commit 46ccc33
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.opensearch.search.SearchHit;
import org.opensearch.search.SearchHits;
import org.opensearch.test.InternalSettingsPlugin;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.ParameterizedDynamicSettingsOpenSearchIntegTestCase;
import org.hamcrest.Matchers;

Expand All @@ -83,7 +84,9 @@
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.function.Supplier;

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

public void testSortMixedNumericFields() throws Exception {
public void testSortMixedIntegerNumericFields() throws Exception {
internalCluster().ensureAtLeastNumDataNodes(3);
index("long", Long.MAX_VALUE);
index("integer", Integer.MAX_VALUE);
AtomicInteger counter = new AtomicInteger();
index("long", () -> Long.MAX_VALUE - counter.getAndIncrement());
index("integer", () -> Integer.MAX_VALUE - counter.getAndIncrement());
SearchResponse searchResponse = client().prepareSearch("long", "integer")
.setQuery(matchAllQuery())
.setSize(10)
Expand All @@ -2630,7 +2634,58 @@ public void testSortMixedNumericFields() throws Exception {
}
}

private void index(String type, long end) throws Exception {
public void testSortMixedFloatingNumericFields() throws Exception {
internalCluster().ensureAtLeastNumDataNodes(3);
AtomicInteger counter = new AtomicInteger();
index("double", () -> 100.5 - counter.getAndIncrement());
counter.set(0);
index("float", () -> 200.5 - counter.getAndIncrement());
counter.set(0);
index("half_float", () -> 300.5 - counter.getAndIncrement());
SearchResponse searchResponse = client().prepareSearch("double", "float", "half_float")
.setQuery(matchAllQuery())
.setSize(15)
.addSort(SortBuilders.fieldSort("field").order(SortOrder.ASC).sortMode(SortMode.MAX))
.get();
assertNoFailures(searchResponse);
double[] sortValues = new double[15];
for (int i = 0; i < 15; i++) {
sortValues[i] = ((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).doubleValue();
}
for (int i = 1; i < 15; i++) {
assertThat(Arrays.toString(sortValues), sortValues[i - 1], lessThan(sortValues[i]));
}
}

public void testSortMixedFloatingAndIntegerNumericFields() throws Exception {
internalCluster().ensureAtLeastNumDataNodes(3);
index("long", () -> randomLongBetween(0, (long) 2E53 - 1));
index("integer", OpenSearchTestCase::randomInt);
index("double", OpenSearchTestCase::randomDouble);
index("float", () -> randomFloat());
boolean asc = randomBoolean();
SearchResponse searchResponse = client().prepareSearch("long", "integer", "double", "float")
.setQuery(matchAllQuery())
.setSize(20)
.addSort(SortBuilders.fieldSort("field").order(asc ? SortOrder.ASC : SortOrder.DESC).sortMode(SortMode.MAX))
.get();
assertNoFailures(searchResponse);
double[] sortValues = new double[20];
for (int i = 0; i < 20; i++) {
sortValues[i] = ((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).doubleValue();
}
if (asc) {
for (int i = 1; i < 20; i++) {
assertThat(Arrays.toString(sortValues), sortValues[i - 1], lessThanOrEqualTo(sortValues[i]));
}
} else {
for (int i = 1; i < 20; i++) {
assertThat(Arrays.toString(sortValues), sortValues[i - 1], greaterThanOrEqualTo(sortValues[i]));
}
}
}

private void index(String type, Supplier<Number> valueSupplier) throws Exception {
assertAcked(
prepareCreate(type).setMapping(
XContentFactory.jsonBuilder()
Expand All @@ -2645,8 +2700,12 @@ private void index(String type, long end) throws Exception {
);
ensureGreen(type);
for (int i = 0; i < 5; i++) {
client().prepareIndex(type).setId(Integer.toString(i)).setSource("{\"field\" : " + (end - i) + " }", XContentType.JSON).get();
client().prepareIndex(type)
.setId(Integer.toString(i))
.setSource("{\"field\" : " + valueSupplier.get() + " }", XContentType.JSON)
.get();
}
client().admin().indices().prepareRefresh(type).get();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import java.util.Comparator;

/**
* Sorted numeric field for wider sort types,
* to help sorting two different numeric types.
* Sorted numeric field for wider sort types, to help sorting two different numeric types.
* NOTE: the unsigned_long is not supported by widening sort since the unsigned_long could not be used with other types
*
* @opensearch.internal
*/
Expand Down

0 comments on commit 46ccc33

Please sign in to comment.