Skip to content

Commit

Permalink
Fix search_after null_pointer_exception
Browse files Browse the repository at this point in the history
Signed-off-by: Gao Binlong <[email protected]>
  • Loading branch information
gaobinlong committed Sep 19, 2024
1 parent 605d96d commit d8a0a84
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions server/src/main/java/org/opensearch/search/SearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,9 @@ public static boolean canMatchSearchAfter(
&& primarySortField.missing() == null
&& Objects.equals(trackTotalHitsUpto, SearchContext.TRACK_TOTAL_HITS_DISABLED)) {
final Object searchAfterPrimary = searchAfter.fields[0];
if (searchAfterPrimary == null) {
return true;
}
if (primarySortField.order() == SortOrder.DESC) {
if (minMax.compareMin(searchAfterPrimary) > 0) {
// In Desc order, if segment/shard minimum is gt search_after, the segment/shard won't be competitive
Expand Down
14 changes: 14 additions & 0 deletions server/src/test/java/org/opensearch/search/SearchServiceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2259,4 +2259,18 @@ public void testCanMatchSearchAfterDescLessThanMinWithTrackTotalhits() throws IO
primarySort.order(SortOrder.DESC);
assertEquals(SearchService.canMatchSearchAfter(searchAfter, minMax, primarySort, 1000), true);
}

/**
* Test canMatchSearchAfter with null value, even if min/max is out of range
* Min = 0L, Max = 9L, search_after = null
* Expected result is canMatch = true
*/
public void testCanMatchSearchAfterWithNullValue() throws IOException {
FieldDoc searchAfter = new FieldDoc(0, 0, new Long[] { null });
MinAndMax<?> minMax = new MinAndMax<Long>(0L, 9L);
FieldSortBuilder primarySort = new FieldSortBuilder("test");
primarySort.order(SortOrder.DESC);
// Should be true with null value
assertEquals(SearchService.canMatchSearchAfter(searchAfter, minMax, primarySort, SearchContext.TRACK_TOTAL_HITS_DISABLED), true);
}
}

0 comments on commit d8a0a84

Please sign in to comment.