@@ -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 (),
0 commit comments