2121
2222import com .carrotsearch .hppc .IntArrayList ;
2323import com .carrotsearch .hppc .ObjectObjectHashMap ;
24-
2524import org .apache .lucene .index .Term ;
2625import org .apache .lucene .search .CollectionStatistics ;
2726import org .apache .lucene .search .FieldDoc ;
@@ -154,7 +153,7 @@ public AggregatedDfs aggregateDfs(Collection<DfsSearchResult> results) {
154153 * @param from the offset into the search results top docs
155154 * @param size the number of hits to return from the merged top docs
156155 */
157- public SortedTopDocs sortDocs (boolean ignoreFrom , Collection <? extends SearchPhaseResult > results ,
156+ static SortedTopDocs sortDocs (boolean ignoreFrom , Collection <? extends SearchPhaseResult > results ,
158157 final Collection <TopDocs > bufferedTopDocs , final TopDocsStats topDocsStats , int from , int size ) {
159158 if (results .isEmpty ()) {
160159 return SortedTopDocs .EMPTY ;
@@ -214,7 +213,7 @@ public SortedTopDocs sortDocs(boolean ignoreFrom, Collection<? extends SearchPha
214213 }
215214 final boolean isSortedByField ;
216215 final SortField [] sortFields ;
217- if (mergedTopDocs != null && mergedTopDocs instanceof TopFieldDocs ) {
216+ if (mergedTopDocs instanceof TopFieldDocs ) {
218217 TopFieldDocs fieldDocs = (TopFieldDocs ) mergedTopDocs ;
219218 isSortedByField = (fieldDocs instanceof CollapseTopFieldDocs &&
220219 fieldDocs .fields .length == 1 && fieldDocs .fields [0 ].getType () == SortField .Type .SCORE ) == false ;
@@ -230,11 +229,10 @@ public SortedTopDocs sortDocs(boolean ignoreFrom, Collection<? extends SearchPha
230229 }
231230 }
232231
233- TopDocs mergeTopDocs (Collection <TopDocs > results , int topN , int from ) {
232+ static TopDocs mergeTopDocs (Collection <TopDocs > results , int topN , int from ) {
234233 if (results .isEmpty ()) {
235234 return null ;
236235 }
237- assert results .isEmpty () == false ;
238236 final boolean setShardIndex = false ;
239237 final TopDocs topDocs = results .stream ().findFirst ().get ();
240238 final TopDocs mergedTopDocs ;
@@ -259,12 +257,8 @@ TopDocs mergeTopDocs(Collection<TopDocs> results, int topN, int from) {
259257 }
260258
261259 private static void setShardIndex (TopDocs topDocs , int shardIndex ) {
260+ assert topDocs .scoreDocs .length == 0 || topDocs .scoreDocs [0 ].shardIndex == -1 : "shardIndex is already set" ;
262261 for (ScoreDoc doc : topDocs .scoreDocs ) {
263- if (doc .shardIndex != -1 ) {
264- // once there is a single shard index initialized all others will be initialized too
265- // there are many asserts down in lucene land that this is actually true. we can shortcut it here.
266- return ;
267- }
268262 doc .shardIndex = shardIndex ;
269263 }
270264 }
@@ -283,7 +277,6 @@ public ScoreDoc[] getLastEmittedDocPerShard(ReducedQueryPhase reducedQueryPhase,
283277 }
284278 }
285279 return lastEmittedDocPerShard ;
286-
287280 }
288281
289282 /**
@@ -402,15 +395,15 @@ private SearchHits getHits(ReducedQueryPhase reducedQueryPhase, boolean ignoreFr
402395 hits .add (searchHit );
403396 }
404397 }
405- return new SearchHits (hits .toArray (new SearchHit [hits . size () ]), reducedQueryPhase .totalHits , reducedQueryPhase .maxScore );
398+ return new SearchHits (hits .toArray (new SearchHit [0 ]), reducedQueryPhase .totalHits , reducedQueryPhase .maxScore );
406399 }
407400
408401 /**
409402 * Reduces the given query results and consumes all aggregations and profile results.
410403 * @param queryResults a list of non-null query shard results
411404 */
412- public ReducedQueryPhase reducedQueryPhase (Collection <? extends SearchPhaseResult > queryResults , boolean isScrollRequest ) {
413- return reducedQueryPhase (queryResults , isScrollRequest , true );
405+ public ReducedQueryPhase reducedScrollQueryPhase (Collection <? extends SearchPhaseResult > queryResults ) {
406+ return reducedQueryPhase (queryResults , true , true );
414407 }
415408
416409 /**
@@ -422,7 +415,6 @@ public ReducedQueryPhase reducedQueryPhase(Collection<? extends SearchPhaseResul
422415 return reducedQueryPhase (queryResults , null , new ArrayList <>(), new TopDocsStats (trackTotalHits ), 0 , isScrollRequest );
423416 }
424417
425-
426418 /**
427419 * Reduces the given query results and consumes all aggregations and profile results.
428420 * @param queryResults a list of non-null query shard results
@@ -507,15 +499,13 @@ private ReducedQueryPhase reducedQueryPhase(Collection<? extends SearchPhaseResu
507499 final InternalAggregations aggregations = aggregationsList .isEmpty () ? null : reduceAggs (aggregationsList ,
508500 firstResult .pipelineAggregators (), reduceContext );
509501 final SearchProfileShardResults shardResults = profileResults .isEmpty () ? null : new SearchProfileShardResults (profileResults );
510- final SortedTopDocs scoreDocs = this . sortDocs (isScrollRequest , queryResults , bufferedTopDocs , topDocsStats , from , size );
502+ final SortedTopDocs scoreDocs = sortDocs (isScrollRequest , queryResults , bufferedTopDocs , topDocsStats , from , size );
511503 final TotalHits totalHits = topDocsStats .getTotalHits ();
512504 return new ReducedQueryPhase (totalHits , topDocsStats .fetchHits , topDocsStats .maxScore ,
513505 timedOut , terminatedEarly , suggest , aggregations , shardResults , scoreDocs .scoreDocs , scoreDocs .sortFields ,
514- firstResult != null ? firstResult .sortValueFormats () : null ,
515- numReducePhases , scoreDocs .isSortedByField , size , from , firstResult == null );
506+ firstResult .sortValueFormats (), numReducePhases , scoreDocs .isSortedByField , size , from , false );
516507 }
517508
518-
519509 /**
520510 * Performs an intermediate reduce phase on the aggregations. For instance with this reduce phase never prune information
521511 * that relevant for the final reduce step. For final reduce see {@link #reduceAggs(List, List, ReduceContext)}
@@ -526,7 +516,7 @@ private InternalAggregations reduceAggsIncrementally(List<InternalAggregations>
526516 null , reduceContext );
527517 }
528518
529- private InternalAggregations reduceAggs (List <InternalAggregations > aggregationsList ,
519+ private static InternalAggregations reduceAggs (List <InternalAggregations > aggregationsList ,
530520 List <SiblingPipelineAggregator > pipelineAggregators , ReduceContext reduceContext ) {
531521 InternalAggregations aggregations = InternalAggregations .reduce (aggregationsList , reduceContext );
532522 if (pipelineAggregators != null ) {
@@ -657,7 +647,6 @@ private QueryPhaseResultConsumer(SearchPhaseController controller, int expectedR
657647 this .hasTopDocs = hasTopDocs ;
658648 this .hasAggs = hasAggs ;
659649 this .bufferSize = bufferSize ;
660-
661650 }
662651
663652 @ Override
@@ -675,10 +664,9 @@ private synchronized void consumeInternal(QuerySearchResult querySearchResult) {
675664 aggsBuffer [0 ] = reducedAggs ;
676665 }
677666 if (hasTopDocs ) {
678- TopDocs reducedTopDocs = controller . mergeTopDocs (Arrays .asList (topDocsBuffer ),
667+ TopDocs reducedTopDocs = mergeTopDocs (Arrays .asList (topDocsBuffer ),
679668 // we have to merge here in the same way we collect on a shard
680- querySearchResult .from () + querySearchResult .size ()
681- , 0 );
669+ querySearchResult .from () + querySearchResult .size (), 0 );
682670 Arrays .fill (topDocsBuffer , null );
683671 topDocsBuffer [0 ] = reducedTopDocs ;
684672 }
@@ -692,7 +680,7 @@ private synchronized void consumeInternal(QuerySearchResult querySearchResult) {
692680 if (hasTopDocs ) {
693681 final TopDocsAndMaxScore topDocs = querySearchResult .consumeTopDocs (); // can't be null
694682 topDocsStats .add (topDocs );
695- SearchPhaseController . setShardIndex (topDocs .topDocs , querySearchResult .getShardIndex ());
683+ setShardIndex (topDocs .topDocs , querySearchResult .getShardIndex ());
696684 topDocsBuffer [i ] = topDocs .topDocs ;
697685 }
698686 }
@@ -705,7 +693,6 @@ private synchronized List<TopDocs> getRemainingTopDocs() {
705693 return hasTopDocs ? Arrays .asList (topDocsBuffer ).subList (0 , index ) : null ;
706694 }
707695
708-
709696 @ Override
710697 public ReducedQueryPhase reduce () {
711698 return controller .reducedQueryPhase (results .asList (), getRemainingAggs (), getRemainingTopDocs (), topDocsStats ,
@@ -739,7 +726,7 @@ InitialSearchPhase.ArraySearchPhaseResults<SearchPhaseResult> newSearchPhaseResu
739726 return new QueryPhaseResultConsumer (this , numShards , request .getBatchedReduceSize (), hasTopDocs , hasAggs );
740727 }
741728 }
742- return new InitialSearchPhase .ArraySearchPhaseResults (numShards ) {
729+ return new InitialSearchPhase .ArraySearchPhaseResults < SearchPhaseResult > (numShards ) {
743730 @ Override
744731 public ReducedQueryPhase reduce () {
745732 return reducedQueryPhase (results .asList (), isScrollRequest , trackTotalHits );
0 commit comments