Apache Lucene open-source search software
Tighten constant-score bulk routing (#16143)
Costin Leau75bb7781cc19
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 57ddfd2dc8a..2d76a85ed28 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -456,7 +456,10 @@ Optimizations* GITHUB#16126: Implement #intoBitSet and #docIDRunEnd in DocValuesWriter implementations. (Ignacio Vera)-* GITHUB#16141: Optimize constant-score bulk scoring via intoBitSet batching. (Costin Leau)+* GITHUB#16141, GITHUB#16143: Optimize constant-score bulk scoring via intoBitSet+ batching, route more constant-score iterators through ConstantScoreScorerSupplier,+ and preserve two-phase iterator semantics when falling back to DefaultBulkScorer.+ (Costin Leau)* GITHUB#16147: Optimize column batch parent field with bulk fill. (Tim Brooks)diff --git a/lucene/core/src/java/org/apache/lucene/search/AbstractMultiTermQueryConstantScoreWrapper.java b/lucene/core/src/java/org/apache/lucene/search/AbstractMultiTermQueryConstantScoreWrapper.javaindex a78a32aea80..b9affb5e340 100644--- a/lucene/core/src/java/org/apache/lucene/search/AbstractMultiTermQueryConstantScoreWrapper.java+++ b/lucene/core/src/java/org/apache/lucene/search/AbstractMultiTermQueryConstantScoreWrapper.java@@ -291,8 +291,9 @@ abstract class AbstractMultiTermQueryConstantScoreWrapper<Q extends MultiTermQuebulkScorer = weightOrIterator.weight.bulkScorer(context);} else {bulkScorer =- new DefaultBulkScorer(- new ConstantScoreScorer(score(), scoreMode, weightOrIterator.iterator));+ ConstantScoreScorerSupplier.fromIterator(+ weightOrIterator.iterator, score(), scoreMode, context.reader().maxDoc())+ .bulkScorer();}// It's against the API contract to return a null scorer from a non-null ScoreSupplier.diff --git a/lucene/core/src/java/org/apache/lucene/search/ConstantScoreScorerSupplier.java b/lucene/core/src/java/org/apache/lucene/search/ConstantScoreScorerSupplier.javaindex 6df6d8871c9..48239201443 100644--- a/lucene/core/src/java/org/apache/lucene/search/ConstantScoreScorerSupplier.java+++ b/lucene/core/src/java/org/apache/lucene/search/ConstantScoreScorerSupplier.java@@ -93,7 +93,11 @@ public abstract class ConstantScoreScorerSupplier extends ScorerSupplier {} else if (scoreMode.needsScores() == false && twoPhase == null) {return new ConstantScoreBulkScorer(score, scoreMode, iterator);} else {- return new Weight.DefaultBulkScorer(new ConstantScoreScorer(score, scoreMode, iterator));+ Scorer scorer =+ twoPhase == null+ ? new ConstantScoreScorer(score, scoreMode, iterator)+ : new ConstantScoreScorer(score, scoreMode, twoPhase);+ return new Weight.DefaultBulkScorer(scorer);}}}diff --git a/lucene/core/src/java/org/apache/lucene/search/FieldExistsQuery.java b/lucene/core/src/java/org/apache/lucene/search/FieldExistsQuery.javaindex f6bac7b52bc..d4232efef08 100644--- a/lucene/core/src/java/org/apache/lucene/search/FieldExistsQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/FieldExistsQuery.java@@ -212,8 +212,8 @@ public class FieldExistsQuery extends Query {if (iterator == null) {return null;}- final var scorer = new ConstantScoreScorer(score(), scoreMode, iterator);- return new DefaultScorerSupplier(scorer);+ return ConstantScoreScorerSupplier.fromIterator(+ iterator, score(), scoreMode, context.reader().maxDoc());}@Overridediff --git a/lucene/core/src/java/org/apache/lucene/search/IndexSortSortedNumericDocValuesRangeQuery.java b/lucene/core/src/java/org/apache/lucene/search/IndexSortSortedNumericDocValuesRangeQuery.javaindex ff607f3b5b8..fb4afd4c795 100644--- a/lucene/core/src/java/org/apache/lucene/search/IndexSortSortedNumericDocValuesRangeQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/IndexSortSortedNumericDocValuesRangeQuery.java@@ -156,17 +156,8 @@ public class IndexSortSortedNumericDocValuesRangeQuery extends NumericDocValuesRIteratorAndCount itAndCount = getDocIdSetIteratorOrNull(context);if (itAndCount != null) {DocIdSetIterator disi = itAndCount.it;- return new ScorerSupplier() {- @Override- public Scorer get(long leadCost) throws IOException {- return new ConstantScoreScorer(score(), scoreMode, disi);- }-- @Override- public long cost() {- return disi.cost();- }- };+ return ConstantScoreScorerSupplier.fromIterator(+ disi, score(), scoreMode, context.reader().maxDoc());}return fallbackWeight.scorerSupplier(context);}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestBooleanScorer.java b/lucene/core/src/test/org/apache/lucene/search/TestBooleanScorer.javaindex 0ce7b21d3e0..53d0e7961de 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestBooleanScorer.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestBooleanScorer.java@@ -260,6 +260,50 @@ public class TestBooleanScorer extends LuceneTestCase {1f, ScoreMode.COMPLETE, new CountingDocIdSetIterator(new int[] {1})));}+ public void testConstantScoreScorerSupplierPreservesTwoPhaseIterator() throws IOException {+ int[] docs = {1, 2, 3};+ CountingDocIdSetIterator approximation = new CountingDocIdSetIterator(docs);+ DocIdSetIterator iterator =+ TwoPhaseIterator.asDocIdSetIterator(+ new TwoPhaseIterator(approximation) {+ @Override+ public boolean matches() {+ return approximation.docID() != 2;+ }++ @Override+ public float matchCost() {+ return 1f;+ }+ });++ int[] collected = new int[docs.length];+ int[] count = new int[1];+ LeafCollector collector =+ new LeafCollector() {+ @Override+ public void setScorer(Scorable scorer) {}++ @Override+ public void collect(int doc) {+ collected[count[0]++] = doc;+ }++ @Override+ public void collect(DocIdStream stream) {+ fail("two-phase iterators must preserve per-doc confirmation");+ }+ };++ BulkScorer bulkScorer =+ ConstantScoreScorerSupplier.fromIterator(iterator, 1f, ScoreMode.COMPLETE_NO_SCORES, 9000)+ .bulkScorer();+ assertThat(bulkScorer, instanceOf(DefaultBulkScorer.class));+ assertEquals(DocIdSetIterator.NO_MORE_DOCS, bulkScorer.score(collector, null, 0, 9000));+ assertArrayEquals(new int[] {1, 3}, Arrays.copyOf(collected, count[0]));+ assertEquals(0, approximation.intoBitSetCalls);+ }+public void testDefaultBulkScorerDoesNotUseDocIdStreamForTopScores() throws IOException {assertDefaultBulkScorerDoesNotUseDocIdStreamForScores(ScoreMode.TOP_SCORES);}