@@ -63,7 +63,7 @@ public Factory(
6363 List <SortBuilder <?>> sorts ,
6464 boolean needsScore
6565 ) {
66- super (contexts , weightFunction (queryFunction , sorts ), dataPartitioning , taskConcurrency , limit , needsScore );
66+ super (contexts , weightFunction (queryFunction , sorts , needsScore ), dataPartitioning , taskConcurrency , limit , needsScore );
6767 this .maxPageSize = maxPageSize ;
6868 this .sorts = sorts ;
6969 }
@@ -309,13 +309,13 @@ static final class ScoringPerShardCollector extends PerShardCollector {
309309 }
310310 }
311311
312- private static Function <ShardContext , Weight > weightFunction (Function <ShardContext , Query > queryFunction , List <SortBuilder <?>> sorts ) {
312+ private static Function <ShardContext , Weight > weightFunction (Function <ShardContext , Query > queryFunction , List <SortBuilder <?>> sorts , boolean needsScore ) {
313313 return ctx -> {
314314 final var query = queryFunction .apply (ctx );
315315 final var searcher = ctx .searcher ();
316316 try {
317317 // we create a collector with a limit of 1 to determine the appropriate score mode to use.
318- var scoreMode = newPerShardCollector (ctx , sorts , false , 1 ).collector .scoreMode ();
318+ var scoreMode = newPerShardCollector (ctx , sorts , needsScore , 1 ).collector .scoreMode ();
319319 return searcher .createWeight (searcher .rewrite (query ), scoreMode , 1 );
320320 } catch (IOException e ) {
321321 throw new UncheckedIOException (e );
@@ -332,20 +332,17 @@ private static PerShardCollector newPerShardCollector(ShardContext context, List
332332 if (needsScore == false ) {
333333 return new NonScoringPerShardCollector (context , sortAndFormats .get ().sort , limit );
334334 }
335- SortField [] sortFields = sortAndFormats .get ().sort . getSort () ;
336- if (sortFields != null && sortFields . length == 1 && sortFields [ 0 ]. needsScores () && sortFields [ 0 ]. getReverse () == false ) {
335+ Sort sort = sortAndFormats .get ().sort ;
336+ if (Sort . RELEVANCE . equals ( sort ) ) {
337337 // SORT _score DESC
338338 return new ScoringPerShardCollector (context , new TopScoreDocCollectorManager (limit , null , 0 ).newCollector ());
339339 }
340340
341341 // SORT ..., _score, ...
342- var sort = new Sort ();
343- if (sortFields != null ) {
344- var l = new ArrayList <>(Arrays .asList (sortFields ));
345- l .add (SortField .FIELD_DOC );
346- l .add (SortField .FIELD_SCORE );
347- sort = new Sort (l .toArray (SortField []::new ));
348- }
342+ var l = new ArrayList <>(Arrays .asList (sort .getSort ()));
343+ l .add (SortField .FIELD_DOC );
344+ l .add (SortField .FIELD_SCORE );
345+ sort = new Sort (l .toArray (SortField []::new ));
349346 return new ScoringPerShardCollector (context , new TopFieldCollectorManager (sort , limit , null , 0 ).newCollector ());
350347 }
351348}
0 commit comments