Skip to content

Commit 054c3bb

Browse files
authored
Fix topDocs.totalHits assignment on scroll queries (#37180)
This change fixes an unreleased bug that assigns the wrong totalHits to scroll queries. Closes #37179
1 parent 20c2c43 commit 054c3bb

File tree

3 files changed

+6
-10
lines changed

3 files changed

+6
-10
lines changed

server/src/main/java/org/elasticsearch/search/query/QueryPhase.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ static boolean execute(SearchContext searchContext,
166166
}
167167
// ... and stop collecting after ${size} matches
168168
searchContext.terminateAfter(searchContext.size());
169-
searchContext.trackTotalHitsUpTo(SearchContext.TRACK_TOTAL_HITS_DISABLED);
170169
} else if (canEarlyTerminate(reader, searchContext.sort())) {
171170
// now this gets interesting: since the search sort is a prefix of the index sort, we can directly
172171
// skip to the desired doc
@@ -177,7 +176,6 @@ static boolean execute(SearchContext searchContext,
177176
.build();
178177
query = bq;
179178
}
180-
searchContext.trackTotalHitsUpTo(SearchContext.TRACK_TOTAL_HITS_DISABLED);
181179
}
182180
}
183181
}

server/src/main/java/org/elasticsearch/search/query/TopDocsCollectorContext.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,15 @@ private SimpleTopDocsCollectorContext(IndexReader reader,
217217
super(REASON_SEARCH_TOP_HITS, numHits);
218218
this.sortAndFormats = sortAndFormats;
219219

220-
// implicit total hit counts are valid only when there is no filter collector in the chain
221-
final int hitCount = hasFilterCollector ? -1 : shortcutTotalHitCount(reader, query);
222220
final TopDocsCollector<?> topDocsCollector;
223221
if (trackTotalHitsUpTo == SearchContext.TRACK_TOTAL_HITS_DISABLED) {
224222
// don't compute hit counts via the collector
225223
topDocsCollector = createCollector(sortAndFormats, numHits, searchAfter, 1);
226224
topDocsSupplier = new CachedSupplier<>(topDocsCollector::topDocs);
227225
totalHitsSupplier = () -> new TotalHits(0, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO);
228226
} else {
227+
// implicit total hit counts are valid only when there is no filter collector in the chain
228+
final int hitCount = hasFilterCollector ? -1 : shortcutTotalHitCount(reader, query);
229229
if (hitCount == -1) {
230230
topDocsCollector = createCollector(sortAndFormats, numHits, searchAfter, trackTotalHitsUpTo);
231231
topDocsSupplier = new CachedSupplier<>(topDocsCollector::topDocs);
@@ -293,12 +293,11 @@ private ScrollingTopDocsCollectorContext(IndexReader reader,
293293
@Override
294294
void postProcess(QuerySearchResult result) throws IOException {
295295
final TopDocs topDocs = topDocsSupplier.get();
296-
topDocs.totalHits = totalHitsSupplier.get();
297-
float maxScore = maxScoreSupplier.get();
296+
final float maxScore;
298297
if (scrollContext.totalHits == null) {
299298
// first round
300-
scrollContext.totalHits = topDocs.totalHits;
301-
scrollContext.maxScore = maxScore;
299+
topDocs.totalHits = scrollContext.totalHits = totalHitsSupplier.get();
300+
maxScore = scrollContext.maxScore = maxScoreSupplier.get();
302301
} else {
303302
// subsequent round: the total number of hits and
304303
// the maximum score were computed on the first round
@@ -367,7 +366,7 @@ static TopDocsCollectorContext createTopDocsCollectorContext(SearchContext searc
367366
// we can disable the tracking of total hits after the initial scroll query
368367
// since the total hits is preserved in the scroll context.
369368
int trackTotalHitsUpTo = searchContext.scrollContext().totalHits != null ?
370-
SearchContext.TRACK_TOTAL_HITS_DISABLED : searchContext.trackTotalHitsUpTo();
369+
SearchContext.TRACK_TOTAL_HITS_DISABLED : SearchContext.TRACK_TOTAL_HITS_ACCURATE;
371370
// no matter what the value of from is
372371
int numDocs = Math.min(searchContext.size(), totalNumDocs);
373372
return new ScrollingTopDocsCollectorContext(reader, query, searchContext.scrollContext(),

server/src/test/java/org/elasticsearch/search/stats/SearchStatsIT.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ private Set<String> nodeIdsWithIndex(String... indices) {
178178
return nodes;
179179
}
180180

181-
@AwaitsFix(bugUrl="https://github.com/elastic/elasticsearch/issues/37179")
182181
public void testOpenContexts() {
183182
String index = "test1";
184183
createIndex(index);

0 commit comments

Comments
 (0)