Apache Lucene open-source search software
Support two-phase filters in MaxScoreBulkScorer bitset path (#16520)
kkewweie26731b006ee
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 0346c940703..bc43ec7da15 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -510,6 +510,8 @@ Optimizations* GITHUB#16582: Rewrite sorted-numeric doc-values range queries that cover every value of a field to aFieldExistsQuery, and collapse an IndexOrDocValuesQuery whose two sides rewrite equally. (Houston Putman)+* GITHUB#16520: Support two-phase filters in MaxScoreBulkScorer bitset path. (Ke Wei)+Bug Fixes---------------------diff --git a/lucene/core/src/java/org/apache/lucene/search/MaxScoreBulkScorer.java b/lucene/core/src/java/org/apache/lucene/search/MaxScoreBulkScorer.javaindex bb39bca70ea..c61eb154a36 100644--- a/lucene/core/src/java/org/apache/lucene/search/MaxScoreBulkScorer.java+++ b/lucene/core/src/java/org/apache/lucene/search/MaxScoreBulkScorer.java@@ -70,7 +70,7 @@ final class MaxScoreBulkScorer extends BulkScorer {essentialQueue = DisiPriorityQueue.ofMaxSize(allScorers.length);maxScoreSums = new double[allScorers.length];- if (this.filter != null && this.filter.twoPhaseView == null && maxDoc >= INNER_WINDOW_SIZE) {+ if (this.filter != null && maxDoc >= INNER_WINDOW_SIZE) {long minScorerCost = allScorers[0].cost;for (int j = 1; j < allScorers.length; j++) {minScorerCost = Math.min(minScorerCost, allScorers[j].cost);@@ -234,7 +234,11 @@ final class MaxScoreBulkScorer extends BulkScorer {filter.doc = filter.approximation.advance(innerWindowMin);}if (filter.doc < innerWindowMax) {- filter.approximation.intoBitSet(innerWindowMax, filterMatchesBits.bits, innerWindowMin);+ if (filter.twoPhaseView != null) {+ filter.twoPhaseView.intoBitSet(innerWindowMax, filterMatchesBits.bits, innerWindowMin);+ } else {+ filter.approximation.intoBitSet(innerWindowMax, filterMatchesBits.bits, innerWindowMin);+ }filter.doc = filter.approximation.docID();}}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestMaxScoreBulkScorer.java b/lucene/core/src/test/org/apache/lucene/search/TestMaxScoreBulkScorer.javaindex e484f9ffbc0..507ee069930 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestMaxScoreBulkScorer.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestMaxScoreBulkScorer.java@@ -22,6 +22,7 @@ import java.util.Collections;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.Field.Store;+import org.apache.lucene.document.SortedNumericDocValuesField;import org.apache.lucene.document.StringField;import org.apache.lucene.document.TextField;import org.apache.lucene.index.DirectoryReader;@@ -1111,7 +1112,7 @@ public class TestMaxScoreBulkScorer extends LuceneTestCase {Query filterQuery =new CountingFilterQuery(- new TermQuery(new Term("filter", "yes")), intoBitSetCalls, advanceCalls);+ new TermQuery(new Term("filter", "yes")), intoBitSetCalls, null, advanceCalls);BooleanQuery innerOr =new BooleanQuery.Builder()@@ -1214,7 +1215,7 @@ public class TestMaxScoreBulkScorer extends LuceneTestCase {Query filterQuery =new CountingFilterQuery(- new TermQuery(new Term("filter", "yes")), intoBitSetCalls, advanceCalls);+ new TermQuery(new Term("filter", "yes")), intoBitSetCalls, null, advanceCalls);BooleanQuery innerOr =new BooleanQuery.Builder()@@ -1281,11 +1282,14 @@ public class TestMaxScoreBulkScorer extends LuceneTestCase {private static class CountingFilterQuery extends Query {private final Query delegate;private final int[] intoBitSetCalls;+ private final int[] twoPhaseIntoBitSetCalls;private final int[] advanceCalls;- CountingFilterQuery(Query delegate, int[] intoBitSetCalls, int[] advanceCalls) {+ CountingFilterQuery(+ Query delegate, int[] intoBitSetCalls, int[] twoPhaseIntoBitSetCalls, int[] advanceCalls) {this.delegate = delegate;this.intoBitSetCalls = intoBitSetCalls;+ this.twoPhaseIntoBitSetCalls = twoPhaseIntoBitSetCalls;this.advanceCalls = advanceCalls;}@@ -1302,6 +1306,29 @@ public class TestMaxScoreBulkScorer extends LuceneTestCase {@Overridepublic Scorer get(long leadCost) throws IOException {Scorer innerScorer = innerSS.get(leadCost);+ TwoPhaseIterator innerTwoPhase = innerScorer.twoPhaseIterator();+ if (innerTwoPhase != null) {+ TwoPhaseIterator countingTwoPhase =+ new TwoPhaseIterator(innerTwoPhase.approximation()) {+ @Override+ public boolean matches() throws IOException {+ return innerTwoPhase.matches();+ }++ @Override+ public float matchCost() {+ return innerTwoPhase.matchCost();+ }++ @Override+ public void intoBitSet(int upTo, FixedBitSet bitSet, int offset)+ throws IOException {+ twoPhaseIntoBitSetCalls[0]++;+ innerTwoPhase.intoBitSet(upTo, bitSet, offset);+ }+ };+ return new ConstantScoreScorer(0f, scoreMode, countingTwoPhase);+ }DocIdSetIterator innerIter = innerScorer.iterator();DocIdSetIterator countingIter =new FilterDocIdSetIterator(innerIter) {@@ -1447,6 +1474,43 @@ public class TestMaxScoreBulkScorer extends LuceneTestCase {dir.close();}+ public void testTwoPhaseFilterUsesBitSet() throws IOException {+ Directory dir = newDirectory();+ IndexWriter w = new IndexWriter(dir, new IndexWriterConfig());+ for (int i = 0; i < 10000; i++) {+ Document doc = new Document();+ doc.add(new TextField("body", "dense1", Field.Store.NO));+ doc.add(new TextField("body", "dense2", Field.Store.NO));+ doc.add(SortedNumericDocValuesField.indexedField("filter", i % 20));+ w.addDocument(doc);+ }+ w.close();++ DirectoryReader reader = DirectoryReader.open(dir);+ IndexSearcher searcher = new IndexSearcher(reader);+ searcher.setQueryCache(null);++ BooleanQuery innerOr =+ new BooleanQuery.Builder()+ .add(new TermQuery(new Term("body", "dense1")), Occur.SHOULD)+ .add(new TermQuery(new Term("body", "dense2")), Occur.SHOULD)+ .build();++ int[] twoPhaseIntoBitSetCalls = {0};+ Query delegateFilterQuery = SortedNumericDocValuesField.newSlowRangeQuery("filter", 1, 1);+ Query filterQuery =+ new CountingFilterQuery(delegateFilterQuery, null, twoPhaseIntoBitSetCalls, new int[1]);+ BooleanQuery outerQuery =+ new BooleanQuery.Builder().add(innerOr, Occur.MUST).add(filterQuery, Occur.FILTER).build();+ TopDocs topDocs = searcher.search(outerQuery, 1000);+ assertEquals(500, topDocs.totalHits.value());+ assertTrue(+ "Expected twoPhaseIntoBitSetCalls() to be called on the two-phase filter",+ twoPhaseIntoBitSetCalls[0] > 0);+ reader.close();+ dir.close();+ }+/*** A query wrapper that counts the smallest target/upTo ever passed to advanceShallow()/* getMaxScore() on its scorer. This lets us detect whether MaxScoreBulkScorer computed score