jpountz commented on code in PR #1018: URL: https://github.com/apache/lucene/pull/1018#discussion_r922675268
########## lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java: ########## @@ -191,6 +191,69 @@ public long cost() { // or null if it is not applicable // pkg-private for forcing use of BooleanScorer in tests BulkScorer optionalBulkScorer(LeafReaderContext context) throws IOException { + if (scoreMode == ScoreMode.TOP_SCORES) { + if (query.getMinimumNumberShouldMatch() > 1 || weightedClauses.size() > 2) { + return null; + } + + List<ScorerSupplier> optional = new ArrayList<>(); + for (WeightedBooleanClause wc : weightedClauses) { + Weight w = wc.weight; + BooleanClause c = wc.clause; + if (c.getOccur() != Occur.SHOULD) { + continue; + } + ScorerSupplier scorer = w.scorerSupplier(context); + if (scorer != null) { + optional.add(scorer); + } + } + + if (optional.size() <= 1) { + return null; + } + + List<Scorer> optionalScorers = new ArrayList<>(); + for (ScorerSupplier ss : optional) { + optionalScorers.add(ss.get(Long.MAX_VALUE)); + } + + return new BulkScorer() { + final Scorer bmmScorer = new BlockMaxMaxscoreScorer(BooleanWeight.this, optionalScorers); + final int maxDoc = context.reader().maxDoc(); + final DocIdSetIterator iterator = bmmScorer.iterator(); + + @Override + public int score(LeafCollector collector, Bits acceptDocs, int min, int max) + throws IOException { + max = Math.min(max, maxDoc); Review Comment: I don't think we need this, do tests fail without it? ########## lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java: ########## @@ -191,6 +191,69 @@ public long cost() { // or null if it is not applicable // pkg-private for forcing use of BooleanScorer in tests BulkScorer optionalBulkScorer(LeafReaderContext context) throws IOException { + if (scoreMode == ScoreMode.TOP_SCORES) { + if (query.getMinimumNumberShouldMatch() > 1 || weightedClauses.size() > 2) { + return null; + } + + List<ScorerSupplier> optional = new ArrayList<>(); + for (WeightedBooleanClause wc : weightedClauses) { + Weight w = wc.weight; + BooleanClause c = wc.clause; + if (c.getOccur() != Occur.SHOULD) { + continue; + } + ScorerSupplier scorer = w.scorerSupplier(context); + if (scorer != null) { + optional.add(scorer); + } + } + + if (optional.size() <= 1) { + return null; + } + + List<Scorer> optionalScorers = new ArrayList<>(); + for (ScorerSupplier ss : optional) { + optionalScorers.add(ss.get(Long.MAX_VALUE)); + } + + return new BulkScorer() { + final Scorer bmmScorer = new BlockMaxMaxscoreScorer(BooleanWeight.this, optionalScorers); + final int maxDoc = context.reader().maxDoc(); + final DocIdSetIterator iterator = bmmScorer.iterator(); + + @Override + public int score(LeafCollector collector, Bits acceptDocs, int min, int max) + throws IOException { + max = Math.min(max, maxDoc); + collector.setScorer(bmmScorer); + + for (int doc = min; doc < max; ) { + int advancedDoc = iterator.advance(doc); + if (advancedDoc == DocIdSetIterator.NO_MORE_DOCS) { + return DocIdSetIterator.NO_MORE_DOCS; + } else if (advancedDoc >= max) { + return max; + } + + if (acceptDocs == null || acceptDocs.get(advancedDoc)) { + collector.collect(advancedDoc); + } + + doc = advancedDoc + 1; + } + + return max == maxDoc ? DocIdSetIterator.NO_MORE_DOCS : max; Review Comment: Maybe we could remove the end condition from the for loop, so that we would hit the `if (advanceDoc >= max)` condition instead, and remove the above line? ########## lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java: ########## @@ -191,6 +191,69 @@ public long cost() { // or null if it is not applicable // pkg-private for forcing use of BooleanScorer in tests BulkScorer optionalBulkScorer(LeafReaderContext context) throws IOException { + if (scoreMode == ScoreMode.TOP_SCORES) { + if (query.getMinimumNumberShouldMatch() > 1 || weightedClauses.size() > 2) { + return null; + } + + List<ScorerSupplier> optional = new ArrayList<>(); + for (WeightedBooleanClause wc : weightedClauses) { + Weight w = wc.weight; + BooleanClause c = wc.clause; + if (c.getOccur() != Occur.SHOULD) { + continue; + } + ScorerSupplier scorer = w.scorerSupplier(context); + if (scorer != null) { + optional.add(scorer); + } + } + + if (optional.size() <= 1) { + return null; + } + + List<Scorer> optionalScorers = new ArrayList<>(); + for (ScorerSupplier ss : optional) { + optionalScorers.add(ss.get(Long.MAX_VALUE)); + } + + return new BulkScorer() { + final Scorer bmmScorer = new BlockMaxMaxscoreScorer(BooleanWeight.this, optionalScorers); + final int maxDoc = context.reader().maxDoc(); + final DocIdSetIterator iterator = bmmScorer.iterator(); + + @Override + public int score(LeafCollector collector, Bits acceptDocs, int min, int max) + throws IOException { + max = Math.min(max, maxDoc); + collector.setScorer(bmmScorer); + + for (int doc = min; doc < max; ) { + int advancedDoc = iterator.advance(doc); + if (advancedDoc == DocIdSetIterator.NO_MORE_DOCS) { + return DocIdSetIterator.NO_MORE_DOCS; + } else if (advancedDoc >= max) { + return max; + } Review Comment: I *think* the entire above `if` statement could become: ``` if (advanceDoc >= max) { return advanceDoc; } ``` since `max` is guaranteed to be less than `maxDoc`. ########## lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java: ########## @@ -191,6 +191,69 @@ public long cost() { // or null if it is not applicable // pkg-private for forcing use of BooleanScorer in tests BulkScorer optionalBulkScorer(LeafReaderContext context) throws IOException { + if (scoreMode == ScoreMode.TOP_SCORES) { + if (query.getMinimumNumberShouldMatch() > 1 || weightedClauses.size() > 2) { + return null; + } + + List<ScorerSupplier> optional = new ArrayList<>(); + for (WeightedBooleanClause wc : weightedClauses) { + Weight w = wc.weight; + BooleanClause c = wc.clause; + if (c.getOccur() != Occur.SHOULD) { + continue; + } + ScorerSupplier scorer = w.scorerSupplier(context); + if (scorer != null) { + optional.add(scorer); + } + } + + if (optional.size() <= 1) { + return null; + } + + List<Scorer> optionalScorers = new ArrayList<>(); + for (ScorerSupplier ss : optional) { + optionalScorers.add(ss.get(Long.MAX_VALUE)); + } + + return new BulkScorer() { Review Comment: OK to keep it. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org