Skip to content

Commit

Permalink
Fix concurrent search NPE with track_total_hits, size=0, and terminat…
Browse files Browse the repository at this point in the history
…e_after
  • Loading branch information
Jay Deng committed Sep 16, 2023
1 parent d34b352 commit f6a97ed
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,45 @@ public void testSimpleTerminateAfterCount() throws Exception {
assertFalse(searchResponse.isTerminatedEarly());
}

public void testSimpleTerminateAfterCountWithSizeAndTrackHits() throws Exception {
prepareCreate("test").setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0)).get();
ensureGreen();
int max = randomIntBetween(3, 29);
List<IndexRequestBuilder> docbuilders = new ArrayList<>(max);

for (int i = 1; i <= max; i++) {
String id = String.valueOf(i);
docbuilders.add(client().prepareIndex("test").setId(id).setSource("field", i));
}

indexRandom(true, docbuilders);
ensureGreen();
refresh();

SearchResponse searchResponse;
for (int i = 1; i < max; i++) {
searchResponse = client().prepareSearch("test")
.setQuery(QueryBuilders.matchAllQuery())
.setTerminateAfter(i)
.setSize(0)
.setTrackTotalHits(true)
.get();
assertEquals(0, searchResponse.getFailedShards());
assertHitCount(searchResponse, i);
assertTrue(searchResponse.isTerminatedEarly());

searchResponse = client().prepareSearch("test")
.setQuery(QueryBuilders.matchAllQuery())
.setTerminateAfter(i)
.setSize(randomIntBetween(1, max))
.setTrackTotalHits(true)
.get();
assertEquals(0, searchResponse.getFailedShards());
assertHitCount(searchResponse, i);
assertTrue(searchResponse.isTerminatedEarly());
}
}

public void testSimpleIndexSortEarlyTerminate() throws Exception {
prepareCreate("test").setSettings(
Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0).put("index.sort.field", "rank")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ private EmptyTopDocsCollectorContext(
Query query,
@Nullable SortAndFormats sortAndFormats,
int trackTotalHitsUpTo,
boolean hasFilterCollector
boolean hasFilterCollector,
int numDocs
) throws IOException {
super(REASON_SEARCH_COUNT, 0);
super(REASON_SEARCH_COUNT, numDocs);
this.sort = sortAndFormats == null ? null : sortAndFormats.sort;
this.trackTotalHitsUpTo = trackTotalHitsUpTo;
if (this.trackTotalHitsUpTo == SearchContext.TRACK_TOTAL_HITS_DISABLED) {
Expand Down Expand Up @@ -189,6 +190,12 @@ CollectorManager<?, ReduceableSearchResult> createManager(CollectorManager<?, Re
trackTotalHitsUpTo,
false
);
} else {
manager = new EarlyTerminatingCollectorManager<>(
new TotalHitCountCollectorManager.Empty(new TotalHits(numHits, TotalHits.Relation.EQUAL_TO), sort),
numHits,
false
);
}
} else {
manager = new EarlyTerminatingCollectorManager<>(
Expand Down Expand Up @@ -778,7 +785,8 @@ public static TopDocsCollectorContext createTopDocsCollectorContext(SearchContex
query,
searchContext.sort(),
searchContext.trackTotalHitsUpTo(),
hasFilterCollector
hasFilterCollector,
totalNumDocs
);
} else if (searchContext.scrollContext() != null) {
// we can disable the tracking of total hits after the initial scroll query
Expand Down

0 comments on commit f6a97ed

Please sign in to comment.