Apache Lucene open-source search software
Corrects the VectorScorer.Bulk interface to be iterator friendly (#15496)
Benjamin Trentf7c5140dc4d3
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 6ce3d131a26..6d8d157f009 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -176,7 +176,7 @@ New Features* GITHUB#14565: Add ParentsChildrenBlockJoinQuery that supports parent and child filter in the same queryalong with limiting number of child documents to retrieve per parent. (Jinny Wang)-* GITHUB#15171: Add `VectorScorer.Bulk` for bulk iteration and scoring of vectors. This new interface is now+* GITHUB#15171, GITHUB#15496: Add `VectorScorer.Bulk` for bulk iteration and scoring of vectors. This new interface is nowused in AbstractKnnVectorQuery to make exact matches faster. (Ben Trent)* GITHUB#15176: Add `[Float|Byte]VectorValues#rescorer(element[])` interface to allow optimized rescoring of vectors.diff --git a/lucene/core/src/java/org/apache/lucene/search/AbstractKnnVectorQuery.java b/lucene/core/src/java/org/apache/lucene/search/AbstractKnnVectorQuery.javaindex c2a097da55d..8f618c92ddf 100644--- a/lucene/core/src/java/org/apache/lucene/search/AbstractKnnVectorQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/AbstractKnnVectorQuery.java@@ -316,17 +316,16 @@ abstract class AbstractKnnVectorQuery extends Query {HitQueue queue = new HitQueue(queueSize, true);TotalHits.Relation relation = TotalHits.Relation.EQUAL_TO;ScoreDoc topDoc = queue.top();- DocIdSetIterator vectorIterator = vectorScorer.iterator();DocAndFloatFeatureBuffer buffer = new DocAndFloatFeatureBuffer();VectorScorer.Bulk bulkScorer = vectorScorer.bulk(acceptIterator);- while (vectorIterator.docID() != DocIdSetIterator.NO_MORE_DOCS) {+ for (float maxScore = bulkScorer.nextDocsAndScores(DocIdSetIterator.NO_MORE_DOCS, null, buffer);+ buffer.size > 0;+ maxScore = bulkScorer.nextDocsAndScores(DocIdSetIterator.NO_MORE_DOCS, null, buffer)) {// Mark results as partial if timeout is metif (queryTimeout != null && queryTimeout.shouldExit()) {relation = TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO;break;}- // iterator already takes live docs into account- float maxScore = bulkScorer.nextDocsAndScores(64, null, buffer);if (maxScore < topDoc.score) {// all the scores in this batch are too low, skipcontinue;diff --git a/lucene/core/src/java/org/apache/lucene/search/VectorScorer.java b/lucene/core/src/java/org/apache/lucene/search/VectorScorer.javaindex b86e20af618..f6a2c1f6d6b 100644--- a/lucene/core/src/java/org/apache/lucene/search/VectorScorer.java+++ b/lucene/core/src/java/org/apache/lucene/search/VectorScorer.java@@ -30,6 +30,7 @@ import org.apache.lucene.util.hnsw.RandomVectorScorer;* @lucene.experimental*/public interface VectorScorer {+ int DEFAULT_BULK_BATCH_SIZE = 64;/*** Compute the score for the current document ID.@@ -52,7 +53,7 @@ public interface VectorScorer {* Additionally, if the iterators are unpositioned (docID() == -1), this method should position* them to the first document.*- * @param matchingDocs the documents to score+ * @param matchingDocs Optional filter to iterate over the documents to score* @return a {@link Bulk} scorer* @throws IOException if an exception occurs during bulk scorer creation* @lucene.experimental@@ -65,12 +66,13 @@ public interface VectorScorer {if (iterator.docID() == -1) {iterator.nextDoc();}- return (nextCount, liveDocs, buffer) -> {- buffer.growNoCopy(nextCount);+ return (upTo, liveDocs, buffer) -> {+ assert upTo > 0;+ buffer.growNoCopy(DEFAULT_BULK_BATCH_SIZE);int size = 0;float maxScore = Float.NEGATIVE_INFINITY;for (int doc = iterator.docID();- doc != DocIdSetIterator.NO_MORE_DOCS && size < nextCount;+ doc < upTo && size < DEFAULT_BULK_BATCH_SIZE;doc = iterator.nextDoc()) {if (liveDocs == null || liveDocs.get(doc)) {buffer.docs[size] = doc;@@ -91,16 +93,16 @@ public interface VectorScorer {*/interface Bulk {/**- * Score up to nextCount documents, store the results in the provided buffer. Behaves similarly- * to {@link Scorer#nextDocsAndScores(int, Bits, DocAndFloatFeatureBuffer)}+ * Score docs ids iterating to upTo documents, store the results in the provided buffer. Behaves+ * similarly to {@link Scorer#nextDocsAndScores(int, Bits, DocAndFloatFeatureBuffer)}*- * @param nextCount the maximum number of documents to score+ * @param upTo the maximum doc ID to score* @param liveDocs the live docs, or null if all docs are live* @param buffer the buffer to store the results* @return the max score of the scored documents* @throws IOException if an exception occurs during scoring*/- float nextDocsAndScores(int nextCount, Bits liveDocs, DocAndFloatFeatureBuffer buffer)+ float nextDocsAndScores(int upTo, Bits liveDocs, DocAndFloatFeatureBuffer buffer)throws IOException;static Bulk fromRandomScorerDense(@@ -111,14 +113,15 @@ public interface VectorScorer {matchingDocs == null? iterator: ConjunctionUtils.createConjunction(List.of(matchingDocs, iterator), List.of());- return (nextCount, liveDocs, buffer) -> {+ return (upTo, liveDocs, buffer) -> {+ assert upTo > 0;if (matches.docID() == -1) {matches.nextDoc();}- buffer.growNoCopy(nextCount);+ buffer.growNoCopy(DEFAULT_BULK_BATCH_SIZE);int size = 0;for (int doc = matches.docID();- doc != DocIdSetIterator.NO_MORE_DOCS && size < nextCount;+ doc < upTo && size < DEFAULT_BULK_BATCH_SIZE;doc = matches.nextDoc()) {if (liveDocs == null || liveDocs.get(doc)) {buffer.docs[size++] = doc;@@ -141,16 +144,17 @@ public interface VectorScorer {int[] docIds = new int[0];@Override- public float nextDocsAndScores(- int nextCount, Bits liveDocs, DocAndFloatFeatureBuffer buffer) throws IOException {+ public float nextDocsAndScores(int upTo, Bits liveDocs, DocAndFloatFeatureBuffer buffer)+ throws IOException {+ assert upTo > 0;if (matches.docID() == -1) {matches.nextDoc();}- buffer.growNoCopy(nextCount);- docIds = ArrayUtil.growNoCopy(docIds, nextCount);+ buffer.growNoCopy(DEFAULT_BULK_BATCH_SIZE);+ docIds = ArrayUtil.growNoCopy(docIds, DEFAULT_BULK_BATCH_SIZE);int size = 0;for (int doc = matches.docID();- doc != DocIdSetIterator.NO_MORE_DOCS && size < nextCount;+ doc < upTo && size < DEFAULT_BULK_BATCH_SIZE;doc = matches.nextDoc()) {if (liveDocs == null || liveDocs.get(doc)) {buffer.docs[size] = iterator.index();