Apache Lucene open-source search software
Keep evaluating conjunction one doc-at-a-time until dynamic pruning kicks in. (#14739)
Adrien Grand17a40bd11378
diff --git a/lucene/core/src/java/org/apache/lucene/search/BlockMaxConjunctionBulkScorer.java b/lucene/core/src/java/org/apache/lucene/search/BlockMaxConjunctionBulkScorer.javaindex 7091777a262..e2e063feb5e 100644--- a/lucene/core/src/java/org/apache/lucene/search/BlockMaxConjunctionBulkScorer.java+++ b/lucene/core/src/java/org/apache/lucene/search/BlockMaxConjunctionBulkScorer.java@@ -87,18 +87,64 @@ final class BlockMaxConjunctionBulkScorer extends BulkScorer {// NOTE: windowMax is inclusiveint windowMax = Math.min(scorers[0].advanceShallow(windowMin), max - 1);- float maxWindowScore = Float.POSITIVE_INFINITY;if (0 < scorable.minCompetitiveScore) {- maxWindowScore = computeMaxScore(windowMin, windowMax);+ float maxWindowScore = computeMaxScore(windowMin, windowMax);+ scoreWindowScoreFirst(collector, acceptDocs, windowMin, windowMax + 1, maxWindowScore);+ } else {+ scoreWindowDocFirst(collector, acceptDocs, windowMin, windowMax + 1);}- scoreWindow(collector, acceptDocs, windowMin, windowMax + 1, maxWindowScore);windowMin = Math.max(lead.docID(), windowMax + 1);}return windowMin >= maxDoc ? DocIdSetIterator.NO_MORE_DOCS : windowMin;}- private void scoreWindow(+ /**+ * Score a window of doc IDs by first finding agreement between all iterators, and only then+ * compute scores and call the collector.+ */+ private void scoreWindowDocFirst(LeafCollector collector, Bits acceptDocs, int min, int max)+ throws IOException {+ int doc = lead.docID();+ if (doc < min) {+ doc = lead.advance(min);+ }++ outer:+ while (doc < max) {+ if (acceptDocs == null || acceptDocs.get(doc)) {+ for (int i = 1; i < iterators.length; ++i) {+ DocIdSetIterator iterator = iterators[i];+ int otherDoc = iterator.docID();+ if (otherDoc < doc) {+ otherDoc = iterator.advance(doc);+ }+ if (doc != otherDoc) {+ doc = lead.advance(otherDoc);+ continue outer;+ }+ }++ double score = 0;+ for (Scorable scorable : scorables) {+ score += scorable.score();+ }+ scorable.score = (float) score;+ collector.collect(doc);+ }+ doc = lead.nextDoc();+ }+ }++ /**+ * Score a window of doc IDs by computing matches and scores on the lead costly clause, then+ * iterate other clauses one by one to remove documents that do not match and increase the global+ * score by the score of the current clause. This is often faster when a minimum competitive score+ * is set, as score computations can be more efficient (e.g. thanks to vectorization) and because+ * we can skip advancing other clauses if the global score so far is not high enough for a doc to+ * have a chance of being competitive.+ */+ private void scoreWindowScoreFirst(LeafCollector collector, Bits acceptDocs, int min, int max, float maxWindowScore)throws IOException {if (maxWindowScore < scorable.minCompetitiveScore) {@@ -120,13 +166,11 @@ final class BlockMaxConjunctionBulkScorer extends BulkScorer {docAndScoreAccBuffer.copyFrom(docAndScoreBuffer);for (int i = 1; i < scorers.length; ++i) {- if (scorable.minCompetitiveScore > 0) {- ScorerUtil.filterCompetitiveHits(- docAndScoreAccBuffer,- sumOfOtherClauses[i],- scorable.minCompetitiveScore,- scorers.length);- }+ ScorerUtil.filterCompetitiveHits(+ docAndScoreAccBuffer,+ sumOfOtherClauses[i],+ scorable.minCompetitiveScore,+ scorers.length);ScorerUtil.applyRequiredClause(docAndScoreAccBuffer, iterators[i], scorables[i]);}