@@ -170,7 +170,7 @@ static SortedTopDocs sortDocs(boolean ignoreFrom, Collection<? extends SearchPha
170
170
if (queryResult .hasConsumedTopDocs () == false ) { // already consumed?
171
171
final TopDocsAndMaxScore td = queryResult .consumeTopDocs ();
172
172
assert td != null ;
173
- topDocsStats .add (td );
173
+ topDocsStats .add (td , queryResult . searchTimedOut (), queryResult . terminatedEarly () );
174
174
// make sure we set the shard index before we add it - the consumer didn't do that yet
175
175
if (td .topDocs .scoreDocs .length > 0 ) {
176
176
setShardIndex (td .topDocs , queryResult .getShardIndex ());
@@ -439,12 +439,10 @@ private ReducedQueryPhase reducedQueryPhase(Collection<? extends SearchPhaseResu
439
439
boolean performFinalReduce ) {
440
440
assert numReducePhases >= 0 : "num reduce phases must be >= 0 but was: " + numReducePhases ;
441
441
numReducePhases ++; // increment for this phase
442
- boolean timedOut = false ;
443
- Boolean terminatedEarly = null ;
444
442
if (queryResults .isEmpty ()) { // early terminate we have nothing to reduce
445
443
final TotalHits totalHits = topDocsStats .getTotalHits ();
446
- return new ReducedQueryPhase (totalHits , topDocsStats .fetchHits , topDocsStats .maxScore ,
447
- timedOut , terminatedEarly , null , null , null , SortedTopDocs .EMPTY , null , numReducePhases , 0 , 0 , true );
444
+ return new ReducedQueryPhase (totalHits , topDocsStats .fetchHits , topDocsStats .getMaxScore () ,
445
+ false , null , null , null , null , SortedTopDocs .EMPTY , null , numReducePhases , 0 , 0 , true );
448
446
}
449
447
final QuerySearchResult firstResult = queryResults .stream ().findFirst ().get ().queryResult ();
450
448
final boolean hasSuggest = firstResult .suggest () != null ;
@@ -476,16 +474,6 @@ private ReducedQueryPhase reducedQueryPhase(Collection<? extends SearchPhaseResu
476
474
QuerySearchResult result = entry .queryResult ();
477
475
from = result .from ();
478
476
size = result .size ();
479
- if (result .searchTimedOut ()) {
480
- timedOut = true ;
481
- }
482
- if (result .terminatedEarly () != null ) {
483
- if (terminatedEarly == null ) {
484
- terminatedEarly = result .terminatedEarly ();
485
- } else if (result .terminatedEarly ()) {
486
- terminatedEarly = true ;
487
- }
488
- }
489
477
if (hasSuggest ) {
490
478
assert result .suggest () != null ;
491
479
for (Suggestion <? extends Suggestion .Entry <? extends Suggestion .Entry .Option >> suggestion : result .suggest ()) {
@@ -508,8 +496,8 @@ private ReducedQueryPhase reducedQueryPhase(Collection<? extends SearchPhaseResu
508
496
final SearchProfileShardResults shardResults = profileResults .isEmpty () ? null : new SearchProfileShardResults (profileResults );
509
497
final SortedTopDocs sortedTopDocs = sortDocs (isScrollRequest , queryResults , bufferedTopDocs , topDocsStats , from , size );
510
498
final TotalHits totalHits = topDocsStats .getTotalHits ();
511
- return new ReducedQueryPhase (totalHits , topDocsStats .fetchHits , topDocsStats .maxScore ,
512
- timedOut , terminatedEarly , suggest , aggregations , shardResults , sortedTopDocs ,
499
+ return new ReducedQueryPhase (totalHits , topDocsStats .fetchHits , topDocsStats .getMaxScore () ,
500
+ topDocsStats . timedOut , topDocsStats . terminatedEarly , suggest , aggregations , shardResults , sortedTopDocs ,
513
501
firstResult .sortValueFormats (), numReducePhases , size , from , false );
514
502
}
515
503
@@ -577,11 +565,7 @@ public static final class ReducedQueryPhase {
577
565
}
578
566
this .totalHits = totalHits ;
579
567
this .fetchHits = fetchHits ;
580
- if (Float .isInfinite (maxScore )) {
581
- this .maxScore = Float .NaN ;
582
- } else {
583
- this .maxScore = maxScore ;
584
- }
568
+ this .maxScore = maxScore ;
585
569
this .timedOut = timedOut ;
586
570
this .terminatedEarly = terminatedEarly ;
587
571
this .suggest = suggest ;
@@ -682,7 +666,7 @@ private synchronized void consumeInternal(QuerySearchResult querySearchResult) {
682
666
}
683
667
if (hasTopDocs ) {
684
668
final TopDocsAndMaxScore topDocs = querySearchResult .consumeTopDocs (); // can't be null
685
- topDocsStats .add (topDocs );
669
+ topDocsStats .add (topDocs , querySearchResult . searchTimedOut (), querySearchResult . terminatedEarly () );
686
670
setShardIndex (topDocs .topDocs , querySearchResult .getShardIndex ());
687
671
topDocsBuffer [i ] = topDocs .topDocs ;
688
672
}
@@ -744,18 +728,20 @@ static final class TopDocsStats {
744
728
private long totalHits ;
745
729
private TotalHits .Relation totalHitsRelation ;
746
730
long fetchHits ;
747
- float maxScore = Float .NEGATIVE_INFINITY ;
748
-
749
- TopDocsStats () {
750
- this (SearchContext .TRACK_TOTAL_HITS_ACCURATE );
751
- }
731
+ private float maxScore = Float .NEGATIVE_INFINITY ;
732
+ boolean timedOut ;
733
+ Boolean terminatedEarly ;
752
734
753
735
TopDocsStats (int trackTotalHitsUpTo ) {
754
736
this .trackTotalHitsUpTo = trackTotalHitsUpTo ;
755
737
this .totalHits = 0 ;
756
738
this .totalHitsRelation = Relation .EQUAL_TO ;
757
739
}
758
740
741
+ float getMaxScore () {
742
+ return Float .isInfinite (maxScore ) ? Float .NaN : maxScore ;
743
+ }
744
+
759
745
TotalHits getTotalHits () {
760
746
if (trackTotalHitsUpTo == SearchContext .TRACK_TOTAL_HITS_DISABLED ) {
761
747
return null ;
@@ -766,7 +752,7 @@ TotalHits getTotalHits() {
766
752
if (totalHits < trackTotalHitsUpTo ) {
767
753
return new TotalHits (totalHits , totalHitsRelation );
768
754
} else {
769
- /**
755
+ /*
770
756
* The user requested to count the total hits up to <code>trackTotalHitsUpTo</code>
771
757
* so we return this lower bound when the total hits is greater than this value.
772
758
* This can happen when multiple shards are merged since the limit to track total hits
@@ -777,7 +763,7 @@ TotalHits getTotalHits() {
777
763
}
778
764
}
779
765
780
- void add (TopDocsAndMaxScore topDocs ) {
766
+ void add (TopDocsAndMaxScore topDocs , boolean timedOut , Boolean terminatedEarly ) {
781
767
if (trackTotalHitsUpTo != SearchContext .TRACK_TOTAL_HITS_DISABLED ) {
782
768
totalHits += topDocs .topDocs .totalHits .value ;
783
769
if (topDocs .topDocs .totalHits .relation == Relation .GREATER_THAN_OR_EQUAL_TO ) {
@@ -788,6 +774,16 @@ void add(TopDocsAndMaxScore topDocs) {
788
774
if (!Float .isNaN (topDocs .maxScore )) {
789
775
maxScore = Math .max (maxScore , topDocs .maxScore );
790
776
}
777
+ if (timedOut ) {
778
+ this .timedOut = true ;
779
+ }
780
+ if (terminatedEarly != null ) {
781
+ if (this .terminatedEarly == null ) {
782
+ this .terminatedEarly = terminatedEarly ;
783
+ } else if (terminatedEarly ) {
784
+ this .terminatedEarly = true ;
785
+ }
786
+ }
791
787
}
792
788
}
793
789
0 commit comments