Apache Lucene open-source search software
Restore WANDScorer for TOP_SCORES + minShouldMatch > 1 (#16176)
Tianxiao Wei135b348b3709
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex e3072f1033f..1167dbf3b12 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -383,6 +383,8 @@ ImprovementsOptimizations---------------------+* GITHUB#16176: Restore WANDScorer for TOP_SCORES + minShouldMatch > 1. (Tianxiao Wei)+* GITHUB#16153: Use TernaryLongHeap in UpdateGraphsUtils for faster HNSW graph merging. (Prithvi S)* GITHUB#15861: Optimise PhraseScorer by short circuiting non competitive documents in TOP_SCORES mode. (Prithvi S)diff --git a/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.java b/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.javaindex 480a524de54..c12ca33137f 100644--- a/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.java+++ b/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.java@@ -292,7 +292,14 @@ final class BooleanScorerSupplier extends ScorerSupplier {return subs.get(Occur.SHOULD).iterator().next().bulkScorer();}- if (scoreMode == ScoreMode.TOP_SCORES && minShouldMatch <= 1) {+ if (scoreMode == ScoreMode.TOP_SCORES) {+ if (minShouldMatch > 1) {+ // Fall back to BS2/WANDScorer: it supports both block-max impact+ // pruning and minShouldMatch > 1. BooleanScorer (the fall-through+ // below) does not consult score upper bounds and would score every+ // doc in the 2048-doc window, defeating top-K pruning.+ return null;+ }List<Scorer> optionalScorers = new ArrayList<>();for (ScorerSupplier ss : subs.get(Occur.SHOULD)) {optionalScorers.add(ss.get(Long.MAX_VALUE));diff --git a/lucene/core/src/test/org/apache/lucene/search/TestBooleanScorer.java b/lucene/core/src/test/org/apache/lucene/search/TestBooleanScorer.javaindex c3f084bc5f0..6914697a037 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestBooleanScorer.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestBooleanScorer.java@@ -621,4 +621,54 @@ public class TestBooleanScorer extends LuceneTestCase {w.close();dir.close();}++ /**+ * Make sure a SHOULD-only BooleanQuery with TOP_SCORES and minShouldMatch > 1 is dispatched to+ * WandScorer+ */+ public void testTopScoresWithMinShouldMatchFallsBackToWand() throws IOException {+ Directory dir = newDirectory();+ RandomIndexWriter w = new RandomIndexWriter(random(), dir);+ for (int i = 0; i < 100; i++) {+ Document doc = new Document();+ doc.add(new StringField("foo", "bar", Store.NO));+ doc.add(new StringField("foo", "baz", Store.NO));+ doc.add(new StringField("foo", "qux", Store.NO));+ w.addDocument(doc);+ }+ IndexReader reader = w.getReader();+ IndexSearcher searcher = new IndexSearcher(reader);+ searcher.setQueryCache(null); // so that weights are not wrapped+ final LeafReaderContext ctx = reader.leaves().get(0);++ Query query =+ new BooleanQuery.Builder()+ .add(new TermQuery(new Term("foo", "bar")), Occur.SHOULD)+ .add(new TermQuery(new Term("foo", "baz")), Occur.SHOULD)+ .add(new TermQuery(new Term("foo", "qux")), Occur.SHOULD)+ .setMinimumNumberShouldMatch(2)+ .build();++ Weight weight = searcher.createWeight(searcher.rewrite(query), ScoreMode.TOP_SCORES, 1);+ ScorerSupplier ss = weight.scorerSupplier(ctx);+ assertNull(+ "TOP_SCORES + minShouldMatch > 1 must skip BooleanScorer and fall back to BS2/WAND",+ ((BooleanScorerSupplier) ss).booleanScorer());++ // Sanity: bulkScorer() itself returns a non-null BS2 (DefaultBulkScorer+ // wrapping WANDScorer).+ assertTrue(ss.bulkScorer() instanceof DefaultBulkScorer);++ // Non-TOP_SCORES with minShouldMatch > 1 still uses BooleanScorer when+ // matches are dense.+ weight = searcher.createWeight(searcher.rewrite(query), ScoreMode.COMPLETE, 1);+ ss = weight.scorerSupplier(ctx);+ assertTrue(+ "COMPLETE + minShouldMatch > 1 (dense) should still use BooleanScorer",+ ((BooleanScorerSupplier) ss).booleanScorer() instanceof BooleanScorer);++ reader.close();+ w.close();+ dir.close();+ }}