@@ -274,12 +274,12 @@ public RankDocsQuery(
274274 this .minScore = minScore ;
275275 }
276276
277- private RankDocsQuery (RankDoc [] docs , Query topQuery , Query tailQuery , boolean onlyRankDocs ) {
277+ private RankDocsQuery (RankDoc [] docs , Query topQuery , Query tailQuery , boolean onlyRankDocs , float minScore ) {
278278 this .docs = docs ;
279279 this .topQuery = topQuery ;
280280 this .tailQuery = tailQuery ;
281281 this .onlyRankDocs = onlyRankDocs ;
282- this .minScore = DEFAULT_MIN_SCORE ;
282+ this .minScore = minScore ;
283283 }
284284
285285 private static int binarySearch (RankDoc [] docs , int fromIndex , int toIndex , int key ) {
@@ -312,7 +312,11 @@ public RankDoc[] rankDocs() {
312312 @ Override
313313 public Query rewrite (IndexSearcher searcher ) throws IOException {
314314 if (tailQuery == null ) {
315- return topQuery ;
315+ var topRewrite = topQuery .rewrite (searcher );
316+ if (topRewrite != topQuery ) {
317+ return new RankDocsQuery (this .docs , topRewrite , null , this .onlyRankDocs , this .minScore );
318+ }
319+ return this ;
316320 }
317321 boolean hasChanged = false ;
318322 var topRewrite = topQuery .rewrite (searcher );
@@ -323,22 +327,33 @@ public Query rewrite(IndexSearcher searcher) throws IOException {
323327 if (tailRewrite != tailQuery ) {
324328 hasChanged = true ;
325329 }
326- return hasChanged ? new RankDocsQuery (docs , topRewrite , tailRewrite , onlyRankDocs ) : this ;
330+ return hasChanged ? new RankDocsQuery (this . docs , topRewrite , tailRewrite , this . onlyRankDocs , this . minScore ) : this ;
327331 }
328332
329333 @ Override
330334 public Weight createWeight (IndexSearcher searcher , ScoreMode scoreMode , float boost ) throws IOException {
331- if (tailQuery == null ) {
332- throw new IllegalArgumentException ("[tailQuery] should not be null; maybe missing a rewrite?" );
335+ Query combinedQuery ;
336+ if (onlyRankDocs ) {
337+ combinedQuery = topQuery ;
338+ } else {
339+ if (tailQuery == null ) {
340+ combinedQuery = topQuery ;
341+ } else {
342+ var combined = new BooleanQuery .Builder ().add (topQuery , BooleanClause .Occur .SHOULD )
343+ .add (tailQuery , BooleanClause .Occur .FILTER )
344+ .build ();
345+ combinedQuery = combined ;
346+ }
333347 }
334- var combined = new BooleanQuery .Builder ().add (topQuery , onlyRankDocs ? BooleanClause .Occur .MUST : BooleanClause .Occur .SHOULD )
335- .add (tailQuery , BooleanClause .Occur .FILTER )
336- .build ();
348+
337349 var topWeight = topQuery .createWeight (searcher , scoreMode , boost );
338- var combinedWeight = searcher .rewrite (combined ).createWeight (searcher , scoreMode , boost );
350+ var combinedWeight = searcher .rewrite (combinedQuery ).createWeight (searcher , scoreMode , boost );
339351 return new Weight (this ) {
340352 @ Override
341353 public int count (LeafReaderContext context ) throws IOException {
354+ if (onlyRankDocs ) {
355+ return topWeight .count (context );
356+ }
342357 return combinedWeight .count (context );
343358 }
344359
@@ -359,22 +374,23 @@ public Matches matches(LeafReaderContext context, int doc) throws IOException {
359374
360375 @ Override
361376 public ScorerSupplier scorerSupplier (LeafReaderContext context ) throws IOException {
362- ScorerSupplier supplier = combinedWeight .scorerSupplier (context );
363- if (minScore != DEFAULT_MIN_SCORE ) {
377+ ScorerSupplier baseSupplier = onlyRankDocs ? topWeight .scorerSupplier (context ) : combinedWeight .scorerSupplier (context );
378+
379+ if (minScore != DEFAULT_MIN_SCORE && baseSupplier != null ) {
364380 return new ScorerSupplier () {
365381 @ Override
366382 public Scorer get (long leadCost ) throws IOException {
367- Scorer scorer = supplier .get (leadCost );
368- return new MinScoreScorer (scorer , minScore );
383+ Scorer scorer = baseSupplier .get (leadCost );
384+ return scorer == null ? null : new MinScoreScorer (scorer , minScore );
369385 }
370386
371387 @ Override
372388 public long cost () {
373- return supplier .cost ();
389+ return baseSupplier .cost ();
374390 }
375391 };
376392 }
377- return supplier ;
393+ return baseSupplier ;
378394 }
379395 };
380396 }
0 commit comments