Skip to content

Apache Lucene open-source search software

Load dense filters into bitset in MaxScoreBulkScorer (#16069)

Prithvi S57c564f1f3fb
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 68f6828ed51..57ddfd2dc8a 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -460,6 +460,8 @@ Optimizations
* GITHUB#16147: Optimize column batch parent field with bulk fill. (Tim Brooks)
+* GITHUB#15991: Load dense filters into bitset in MaxScoreBulkScorer. (Prithvi S)
+
Bug Fixes
---------------------
* GITHUB#15754: Fix HTMLStripCharFilter to prevent tags from incorrectly consuming subsequent
diff --git a/lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/MaxScoreBulkScorerFilterBenchmark.java b/lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/MaxScoreBulkScorerFilterBenchmark.java
new file mode 100644
index 00000000000..4a7e37b10da
--- /dev/null
+++ b/lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/MaxScoreBulkScorerFilterBenchmark.java
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.benchmark.jmh;
+
+import java.io.IOException;
+import java.util.concurrent.TimeUnit;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.document.StringField;
+import org.apache.lucene.document.TextField;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.BooleanClause;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.BulkScorer;
+import org.apache.lucene.search.LeafCollector;
+import org.apache.lucene.search.Scorable;
+import org.apache.lucene.search.ScoreMode;
+import org.apache.lucene.search.TermQuery;
+import org.apache.lucene.search.Weight;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.MMapDirectory;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.TearDown;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.infra.Blackhole;
+
+/**
+ * Benchmark for MaxScoreBulkScorer filter bitset optimization. Measures throughput of scoring a
+ * disjunction with a dense filter at the BulkScorer level, avoiding IndexSearcher overhead.
+ *
+ * <p>Designed to stress the old leap-frog filter path: many dense, staggered clauses so the
+ * priority queue is large and shuffles frequently, combined with a dense filter so the old path
+ * does many filter.advance() calls per inner window.
+ */
+@BenchmarkMode(Mode.Throughput)
+@OutputTimeUnit(TimeUnit.MILLISECONDS)
+@State(Scope.Benchmark)
+@Warmup(iterations = 3, time = 2)
+@Measurement(iterations = 5, time = 3)
+@Fork(
+ value = 1,
+ jvmArgsAppend = {"-Xmx2g", "-Xms2g"})
+public class MaxScoreBulkScorerFilterBenchmark {
+
+ /** Number of scoring SHOULD clauses */
+ @Param({"10", "20"})
+ public int numClauses;
+
+ /** Filter density: 1.0 = match-all, 0.8 = 80% dense */
+ @Param({"1.0", "0.8"})
+ public double filterDensity;
+
+ private Directory dir;
+ private IndexReader reader;
+ private Weight weightWithFilter;
+ private LeafReaderContext leaf;
+
+ @Setup(Level.Trial)
+ public void setUp() throws IOException {
+ dir = new MMapDirectory(java.nio.file.Files.createTempDirectory("benchmark"));
+ int numDocs = 500_000;
+ try (IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig())) {
+ for (int i = 0; i < numDocs; i++) {
+ Document doc = new Document();
+ // Dense, heavily staggered clauses: each term ~15% density with unique period/offset
+ // so multiple clauses often match the same doc, forcing PQ activity.
+ for (int t = 0; t < numClauses; t++) {
+ int period = 7 + t * 11; // 7, 18, 29, 40, ... ~14% avg density
+ int offset = (t * 7919) % period;
+ if ((i + offset) % period == 0) {
+ doc.add(new TextField("text", "word" + t, Field.Store.NO));
+ }
+ }
+ // Evenly distributed filter
+ if (filterDensity >= 1.0) {
+ doc.add(new StringField("filter", "yes", Field.Store.NO));
+ } else {
+ int filterThreshold = (int) (filterDensity * 1000);
+ if ((i * 7919) % 1000 < filterThreshold) {
+ doc.add(new StringField("filter", "yes", Field.Store.NO));
+ } else {
+ doc.add(new StringField("filter", "no", Field.Store.NO));
+ }
+ }
+ writer.addDocument(doc);
+ }
+ }
+ reader = DirectoryReader.open(dir);
+ leaf = reader.leaves().get(0);
+
+ BooleanQuery.Builder bqWithFilter = new BooleanQuery.Builder();
+ bqWithFilter.setMinimumNumberShouldMatch(1);
+ for (int t = 0; t < numClauses; t++) {
+ bqWithFilter.add(new TermQuery(new Term("text", "word" + t)), BooleanClause.Occur.SHOULD);
+ }
+ if (filterDensity >= 1.0) {
+ bqWithFilter.add(
+ new org.apache.lucene.search.MatchAllDocsQuery(), BooleanClause.Occur.FILTER);
+ } else {
+ bqWithFilter.add(new TermQuery(new Term("filter", "yes")), BooleanClause.Occur.FILTER);
+ }
+
+ org.apache.lucene.search.IndexSearcher searcher =
+ new org.apache.lucene.search.IndexSearcher(reader);
+ weightWithFilter =
+ searcher.createWeight(searcher.rewrite(bqWithFilter.build()), ScoreMode.TOP_SCORES, 1f);
+ }
+
+ @TearDown(Level.Trial)
+ public void tearDown() throws IOException {
+ reader.close();
+ dir.close();
+ }
+
+ @Benchmark
+ public void benchmarkWithFilter(Blackhole bh) throws IOException {
+ BulkScorer scorer = weightWithFilter.bulkScorer(leaf);
+ scorer.score(
+ new LeafCollector() {
+ @Override
+ public void setScorer(Scorable scorer) throws IOException {}
+
+ @Override
+ public void collect(int doc) throws IOException {
+ bh.consume(doc);
+ }
+ },
+ leaf.reader().getLiveDocs(),
+ 0,
+ leaf.reader().maxDoc());
+ }
+}
diff --git a/lucene/core/src/java/org/apache/lucene/search/ConstantScoreScorer.java b/lucene/core/src/java/org/apache/lucene/search/ConstantScoreScorer.java
index 4ae8ef09017..e428191c413 100644
--- a/lucene/core/src/java/org/apache/lucene/search/ConstantScoreScorer.java
+++ b/lucene/core/src/java/org/apache/lucene/search/ConstantScoreScorer.java
@@ -19,6 +19,7 @@ package org.apache.lucene.search;
import java.io.IOException;
import java.util.Arrays;
import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.FixedBitSet;
/**
* A constant-scoring {@link Scorer}.
@@ -54,6 +55,12 @@ public final class ConstantScoreScorer extends Scorer {
public long cost() {
return delegate.cost();
}
+
+ @Override
+ public void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException {
+ delegate.intoBitSet(upTo, bitSet, offset);
+ doc = delegate.docID();
+ }
}
private final float score;
diff --git a/lucene/core/src/java/org/apache/lucene/search/MaxScoreBulkScorer.java b/lucene/core/src/java/org/apache/lucene/search/MaxScoreBulkScorer.java
index 81ebc6d0cfe..1e1a12ab4a0 100644
--- a/lucene/core/src/java/org/apache/lucene/search/MaxScoreBulkScorer.java
+++ b/lucene/core/src/java/org/apache/lucene/search/MaxScoreBulkScorer.java
@@ -49,6 +49,7 @@ final class MaxScoreBulkScorer extends BulkScorer {
private final FixedBitSet windowMatches = new FixedBitSet(INNER_WINDOW_SIZE);
private final double[] windowScores = new double[INNER_WINDOW_SIZE];
+ private FixedBitSet filterMatches = null;
private final DocAndFloatFeatureBuffer docAndScoreBuffer = new DocAndFloatFeatureBuffer();
private final DocAndScoreAccBuffer docAndScoreAccBuffer;
@@ -70,6 +71,13 @@ final class MaxScoreBulkScorer extends BulkScorer {
maxScoreSums = new double[allScorers.length];
docAndScoreAccBuffer = new DocAndScoreAccBuffer();
docAndScoreAccBuffer.growNoCopy(INNER_WINDOW_SIZE);
+
+ if (this.filter != null
+ && this.filter.twoPhaseView == null
+ && maxDoc >= INNER_WINDOW_SIZE
+ && this.filter.cost >= maxDoc / DenseConjunctionBulkScorer.DENSITY_THRESHOLD_INVERSE) {
+ this.filterMatches = new FixedBitSet(INNER_WINDOW_SIZE);
+ }
}
// Number of outer windows that have been evaluated
@@ -166,10 +174,6 @@ final class MaxScoreBulkScorer extends BulkScorer {
private void scoreInnerWindowWithFilter(
LeafCollector collector, Bits acceptDocs, int max, DisiWrapper filter) throws IOException {
-
- // TODO: Sometimes load the filter into a bitset and use the more optimized execution paths with
- // this bitset as `acceptDocs`
-
DisiWrapper top = essentialQueue.top();
assert top.doc < max;
while (top.doc < filter.doc) {
@@ -184,6 +188,40 @@ final class MaxScoreBulkScorer extends BulkScorer {
int innerWindowMax = MathUtil.unsignedMin(max, innerWindowMin + INNER_WINDOW_SIZE);
docAndScoreAccBuffer.size = 0;
+ if (filterMatches == null) {
+ fillScoreBufferViaLeapFrog(top, acceptDocs, innerWindowMax);
+ } else {
+ fillScoreBufferViaBitSet(top, acceptDocs, innerWindowMax);
+ }
+
+ scoreNonEssentialClauses(collector, docAndScoreAccBuffer, firstEssentialScorer);
+ }
+
+ private void fillScoreBufferViaBitSet(DisiWrapper top, Bits acceptDocs, int innerWindowMax)
+ throws IOException {
+ filterMatches.clear();
+ int innerWindowMin = top.doc;
+ if (filter.doc < innerWindowMax) {
+ if (filter.doc < innerWindowMin) {
+ filter.doc = filter.approximation.advance(innerWindowMin);
+ }
+ if (filter.doc < innerWindowMax) {
+ filter.approximation.intoBitSet(innerWindowMax, filterMatches, innerWindowMin);
+ filter.doc = filter.approximation.docID();
+ }
+ }
+ if (acceptDocs != null) {
+ acceptDocs.applyMask(filterMatches, innerWindowMin);
+ }
+
+ int innerWindowSize = innerWindowMax - innerWindowMin;
+ // Collect matches of essential clauses into a bitset, checking filter via bitset lookup
+ collectEssentialScoresIntoWindow(top, innerWindowMax, innerWindowMin, null, filterMatches);
+ flushWindowToDocAndScoreAccBuffer(innerWindowMin, innerWindowSize);
+ }
+
+ private void fillScoreBufferViaLeapFrog(DisiWrapper top, Bits acceptDocs, int innerWindowMax)
+ throws IOException {
while (top.doc < innerWindowMax) {
assert filter.doc <= top.doc; // invariant
if (filter.doc < top.doc) {
@@ -217,43 +255,32 @@ final class MaxScoreBulkScorer extends BulkScorer {
}
}
}
-
- scoreNonEssentialClauses(collector, docAndScoreAccBuffer, firstEssentialScorer);
}
- private void scoreInnerWindowSingleEssentialClause(
- LeafCollector collector, Bits acceptDocs, int upTo) throws IOException {
- DisiWrapper top = essentialQueue.top();
-
- // single essential clause in this window, we can iterate it directly and skip the bitset.
- // this is a common case for 2-clauses queries
- for (top.scorer.nextDocsAndScores(upTo, acceptDocs, docAndScoreBuffer);
- docAndScoreBuffer.size > 0;
- top.scorer.nextDocsAndScores(upTo, acceptDocs, docAndScoreBuffer)) {
-
- docAndScoreAccBuffer.copyFrom(docAndScoreBuffer);
- scoreNonEssentialClauses(collector, docAndScoreAccBuffer, firstEssentialScorer);
- }
-
- top.doc = top.iterator.docID();
- essentialQueue.updateTop();
- }
-
- private void scoreInnerWindowMultipleEssentialClauses(
- LeafCollector collector, Bits acceptDocs, int max) throws IOException {
- DisiWrapper top = essentialQueue.top();
-
- int innerWindowMin = top.doc;
- int innerWindowMax = MathUtil.unsignedMin(max, innerWindowMin + INNER_WINDOW_SIZE);
- int innerWindowSize = innerWindowMax - innerWindowMin;
-
- // Collect matches of essential clauses into a bitset
+ /**
+ * Collect matches of essential clauses into {@link #windowMatches} and {@link #windowScores}. The
+ * caller is responsible for populating {@link #docAndScoreAccBuffer} from the window afterwards.
+ *
+ * @param acceptDocs docs to accept, passed to {@link Scorer#nextDocsAndScores}
+ * @param filterMatches if non-null, only docs whose corresponding bit is set in this bitset will
+ * be collected; if null, all docs are collected
+ */
+ private void collectEssentialScoresIntoWindow(
+ DisiWrapper top,
+ int innerWindowMax,
+ int innerWindowMin,
+ Bits acceptDocs,
+ FixedBitSet filterMatches)
+ throws IOException {
do {
for (top.scorer.nextDocsAndScores(innerWindowMax, acceptDocs, docAndScoreBuffer);
docAndScoreBuffer.size > 0;
top.scorer.nextDocsAndScores(innerWindowMax, acceptDocs, docAndScoreBuffer)) {
for (int index = 0; index < docAndScoreBuffer.size; ++index) {
final int doc = docAndScoreBuffer.docs[index];
+ if (filterMatches != null && filterMatches.get(doc - innerWindowMin) == false) {
+ continue;
+ }
final float score = docAndScoreBuffer.features[index];
final int i = doc - innerWindowMin;
windowMatches.set(i);
@@ -264,7 +291,11 @@ final class MaxScoreBulkScorer extends BulkScorer {
top.doc = top.iterator.docID();
top = essentialQueue.updateTop();
} while (top.doc < innerWindowMax);
+ }
+ /** Flush {@link #windowMatches} and {@link #windowScores} into {@link #docAndScoreAccBuffer}. */
+ private void flushWindowToDocAndScoreAccBuffer(int innerWindowMin, int innerWindowSize)
+ throws IOException {
docAndScoreAccBuffer.size = 0;
windowMatches.forEach(
0,
@@ -277,6 +308,37 @@ final class MaxScoreBulkScorer extends BulkScorer {
windowScores[index] = 0d;
});
windowMatches.clear(0, innerWindowSize);
+ }
+
+ private void scoreInnerWindowSingleEssentialClause(
+ LeafCollector collector, Bits acceptDocs, int upTo) throws IOException {
+ DisiWrapper top = essentialQueue.top();
+
+ // single essential clause in this window, we can iterate it directly and skip the bitset.
+ // this is a common case for 2-clauses queries
+ for (top.scorer.nextDocsAndScores(upTo, acceptDocs, docAndScoreBuffer);
+ docAndScoreBuffer.size > 0;
+ top.scorer.nextDocsAndScores(upTo, acceptDocs, docAndScoreBuffer)) {
+
+ docAndScoreAccBuffer.copyFrom(docAndScoreBuffer);
+ scoreNonEssentialClauses(collector, docAndScoreAccBuffer, firstEssentialScorer);
+ }
+
+ top.doc = top.iterator.docID();
+ essentialQueue.updateTop();
+ }
+
+ private void scoreInnerWindowMultipleEssentialClauses(
+ LeafCollector collector, Bits acceptDocs, int max) throws IOException {
+ DisiWrapper top = essentialQueue.top();
+
+ int innerWindowMin = top.doc;
+ int innerWindowMax = MathUtil.unsignedMin(max, innerWindowMin + INNER_WINDOW_SIZE);
+ int innerWindowSize = innerWindowMax - innerWindowMin;
+
+ // Collect matches of essential clauses into a bitset
+ collectEssentialScoresIntoWindow(top, innerWindowMax, innerWindowMin, acceptDocs, null);
+ flushWindowToDocAndScoreAccBuffer(innerWindowMin, innerWindowSize);
scoreNonEssentialClauses(collector, docAndScoreAccBuffer, firstEssentialScorer);
}
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestMaxScoreBulkScorer.java b/lucene/core/src/test/org/apache/lucene/search/TestMaxScoreBulkScorer.java
index c49dd6d7da6..e4696267de2 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestMaxScoreBulkScorer.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestMaxScoreBulkScorer.java
@@ -638,6 +638,209 @@ public class TestMaxScoreBulkScorer extends LuceneTestCase {
dir.close();
}
+ /**
+ * Test that the bitset-based filter path in MaxScoreBulkScorer is exercised when the filter is
+ * dense enough. This creates a filter whose cost exceeds maxDoc/32 (the density threshold) so
+ * that filterMatches is non-null and fillScoreBufferViaBitSet is used.
+ */
+ public void testFilteredDisjunctionWithDenseFilter() throws Exception {
+ try (Directory dir = newDirectory()) {
+ // We need maxDoc >= INNER_WINDOW_SIZE (4096) and filter.cost >= maxDoc/32.
+ // Create INNER_WINDOW_SIZE + 1 docs. Add the filter term "F" to all of them,
+ // and scoring terms "A" and "C" to specific docs.
+ int numDocs = MaxScoreBulkScorer.INNER_WINDOW_SIZE + 1;
+ try (IndexWriter w =
+ new IndexWriter(dir, newIndexWriterConfig().setMergePolicy(newLogMergePolicy()))) {
+ for (int i = 0; i < numDocs; i++) {
+ Document doc = new Document();
+ // Dense filter: every doc matches
+ doc.add(new StringField("foo", "F", Field.Store.NO));
+ if (i == 0 || i == 1) {
+ doc.add(new StringField("foo", "A", Field.Store.NO));
+ }
+ if (i == 1 || i == 2) {
+ doc.add(new StringField("foo", "C", Field.Store.NO));
+ }
+ w.addDocument(doc);
+ }
+ w.forceMerge(1);
+ }
+
+ try (IndexReader reader = DirectoryReader.open(dir)) {
+ IndexSearcher searcher = newSearcher(reader);
+
+ Query clause1 =
+ new BoostQuery(new ConstantScoreQuery(new TermQuery(new Term("foo", "A"))), 2);
+ Query clause2 = new ConstantScoreQuery(new TermQuery(new Term("foo", "C")));
+ Query filter = new TermQuery(new Term("foo", "F"));
+ LeafReaderContext context = searcher.getIndexReader().leaves().get(0);
+
+ // Verify the filter is dense enough to trigger the bitset path
+ Scorer filterCheck =
+ searcher
+ .createWeight(searcher.rewrite(filter), ScoreMode.TOP_SCORES, 1f)
+ .scorer(context);
+ assertTrue(
+ "filter should be dense enough for bitset path",
+ filterCheck.iterator().cost()
+ >= context.reader().maxDoc()
+ / DenseConjunctionBulkScorer.DENSITY_THRESHOLD_INVERSE);
+
+ Scorer scorer1 =
+ searcher
+ .createWeight(searcher.rewrite(clause1), ScoreMode.TOP_SCORES, 1f)
+ .scorer(context);
+ Scorer scorer2 =
+ searcher
+ .createWeight(searcher.rewrite(clause2), ScoreMode.TOP_SCORES, 1f)
+ .scorer(context);
+ Scorer filterScorer =
+ searcher
+ .createWeight(searcher.rewrite(filter), ScoreMode.TOP_SCORES, 1f)
+ .scorer(context);
+
+ BulkScorer scorer =
+ new MaxScoreBulkScorer(
+ context.reader().maxDoc(), Arrays.asList(scorer1, scorer2), filterScorer);
+
+ // Doc 0: has A (score=2), filter matches
+ // Doc 1: has A+C (score=2+1=3), filter matches
+ // Doc 2: has C (score=1), filter matches
+ scorer.score(
+ new LeafCollector() {
+
+ private int i;
+ private Scorable scorer;
+
+ @Override
+ public void setScorer(Scorable scorer) throws IOException {
+ this.scorer = scorer;
+ }
+
+ @Override
+ public void collect(int doc) throws IOException {
+ switch (i++) {
+ case 0:
+ assertEquals(0, doc);
+ assertEquals(2, scorer.score(), 0);
+ break;
+ case 1:
+ assertEquals(1, doc);
+ assertEquals(2 + 1, scorer.score(), 0);
+ break;
+ case 2:
+ assertEquals(2, doc);
+ assertEquals(1, scorer.score(), 0);
+ break;
+ default:
+ fail();
+ break;
+ }
+ }
+ },
+ null,
+ 0,
+ DocIdSetIterator.NO_MORE_DOCS);
+ }
+ }
+ }
+
+ /**
+ * Test the bitset-based filter path with acceptDocs (live docs) to verify that deleted docs are
+ * correctly excluded when the filter is loaded into a bitset.
+ */
+ public void testFilteredDisjunctionWithDenseFilterAndAcceptDocs() throws Exception {
+ try (Directory dir = newDirectory()) {
+ int numDocs = MaxScoreBulkScorer.INNER_WINDOW_SIZE + 1;
+ try (IndexWriter w =
+ new IndexWriter(dir, newIndexWriterConfig().setMergePolicy(newLogMergePolicy()))) {
+ for (int i = 0; i < numDocs; i++) {
+ Document doc = new Document();
+ doc.add(new StringField("foo", "F", Field.Store.NO));
+ if (i == 0 || i == 1 || i == 2) {
+ doc.add(new StringField("foo", "A", Field.Store.NO));
+ }
+ w.addDocument(doc);
+ }
+ w.forceMerge(1);
+ }
+
+ try (IndexReader reader = DirectoryReader.open(dir)) {
+ IndexSearcher searcher = newSearcher(reader);
+
+ Query clause1 =
+ new BoostQuery(new ConstantScoreQuery(new TermQuery(new Term("foo", "A"))), 2);
+ Query filter = new TermQuery(new Term("foo", "F"));
+ LeafReaderContext context = searcher.getIndexReader().leaves().get(0);
+ Scorer scorer1 =
+ searcher
+ .createWeight(searcher.rewrite(clause1), ScoreMode.TOP_SCORES, 1f)
+ .scorer(context);
+ // Need at least 2 scoring clauses for MaxScoreBulkScorer
+ Query clause2 = new ConstantScoreQuery(new TermQuery(new Term("foo", "A")));
+ Scorer scorer2 =
+ searcher
+ .createWeight(searcher.rewrite(clause2), ScoreMode.TOP_SCORES, 1f)
+ .scorer(context);
+ Scorer filterScorer =
+ searcher
+ .createWeight(searcher.rewrite(filter), ScoreMode.TOP_SCORES, 1f)
+ .scorer(context);
+
+ BulkScorer bulkScorer =
+ new MaxScoreBulkScorer(
+ context.reader().maxDoc(), Arrays.asList(scorer1, scorer2), filterScorer);
+
+ // acceptDocs that excludes doc 1
+ Bits acceptDocs =
+ new Bits() {
+ @Override
+ public boolean get(int index) {
+ return index != 1;
+ }
+
+ @Override
+ public int length() {
+ return numDocs;
+ }
+ };
+
+ bulkScorer.score(
+ new LeafCollector() {
+
+ private int i;
+ private Scorable scorer;
+
+ @Override
+ public void setScorer(Scorable scorer) throws IOException {
+ this.scorer = scorer;
+ }
+
+ @Override
+ public void collect(int doc) throws IOException {
+ switch (i++) {
+ case 0:
+ assertEquals(0, doc);
+ assertEquals(2 + 1, scorer.score(), 0);
+ break;
+ case 1:
+ // doc 1 skipped by acceptDocs
+ assertEquals(2, doc);
+ assertEquals(2 + 1, scorer.score(), 0);
+ break;
+ default:
+ fail();
+ break;
+ }
+ }
+ },
+ acceptDocs,
+ 0,
+ DocIdSetIterator.NO_MORE_DOCS);
+ }
+ }
+ }
+
// This test simulates what happens over time for the query `the quick fox` as collection
// progresses and the minimum competitive score increases.
public void testPartition() throws IOException {