Apache Lucene open-source search software
Adds new simplified rescorer API to [Float|Byte]VectorValues (#15176)
Benjamin Trent8beb3c65c4f8
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene102/Lucene102BinaryQuantizedVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene102/Lucene102BinaryQuantizedVectorsReader.javaindex 80e532df0f9..67a8fb97a55 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene102/Lucene102BinaryQuantizedVectorsReader.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene102/Lucene102BinaryQuantizedVectorsReader.java@@ -431,6 +431,11 @@ class Lucene102BinaryQuantizedVectorsReader extends FlatVectorsReader {return quantizedVectorValues.scorer(query);}+ @Override+ public VectorScorer rescorer(float[] query) throws IOException {+ return rawVectorValues.rescorer(query);+ }+BinarizedByteVectorValues getQuantizedVectorValues() throws IOException {return quantizedVectorValues;}diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99ScalarQuantizedVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99ScalarQuantizedVectorsReader.javaindex 4d6526f43bd..c9dbc1edc6e 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99ScalarQuantizedVectorsReader.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99ScalarQuantizedVectorsReader.java@@ -459,6 +459,11 @@ public final class Lucene99ScalarQuantizedVectorsReader extends FlatVectorsReadereturn quantizedVectorValues.scorer(query);}+ @Override+ public VectorScorer rescorer(float[] query) throws IOException {+ return rawVectorValues.rescorer(query);+ }+@Overridepublic DocIndexIterator iterator() {return rawVectorValues.iterator();diff --git a/lucene/core/src/java/org/apache/lucene/index/ByteVectorValues.java b/lucene/core/src/java/org/apache/lucene/index/ByteVectorValues.javaindex e9be3423c18..e531d444f42 100644--- a/lucene/core/src/java/org/apache/lucene/index/ByteVectorValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/ByteVectorValues.java@@ -65,7 +65,8 @@ public abstract class ByteVectorValues extends KnnVectorValues {}/**- * Return a {@link VectorScorer} for the given query vector.+ * Return a {@link VectorScorer} for the given query vector. When the underlying format quantizes+ * the vectors, this will return a {@link VectorScorer} that scores against the quantized vectors.** @param query the query vector* @return a {@link VectorScorer} instance or null@@ -74,6 +75,23 @@ public abstract class ByteVectorValues extends KnnVectorValues {throw new UnsupportedOperationException();}+ /**+ * Rescore using the given query vector and the current {@link ByteVectorValues}. This is unique+ * from scorer() in that it is explicitly for rescoring an existing set of hits and thus will+ * often utilize the highest fidelity scoring algorithm available. This is useful when the initial+ * search used a quantized index or an approximate scoring algorithm, and now we want to rescore+ * the hits using the full fidelity vectors. The default implementation is to call {@link+ * #scorer(byte[])} assuming that the scorer is already the highest fidelity implementation+ * available.+ *+ * @param target the query vector+ * @return a {@link VectorScorer} instance or null+ * @throws IOException if an I/O error occurs+ */+ public VectorScorer rescorer(byte[] target) throws IOException {+ return scorer(target);+ }+@Overridepublic VectorEncoding getEncoding() {return VectorEncoding.BYTE;diff --git a/lucene/core/src/java/org/apache/lucene/index/FloatVectorValues.java b/lucene/core/src/java/org/apache/lucene/index/FloatVectorValues.javaindex aa840fc3931..b318f7c162b 100644--- a/lucene/core/src/java/org/apache/lucene/index/FloatVectorValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/FloatVectorValues.java@@ -66,7 +66,8 @@ public abstract class FloatVectorValues extends KnnVectorValues {/*** Return a {@link VectorScorer} for the given query vector and the current {@link- * FloatVectorValues}.+ * FloatVectorValues}. When the underlying format quantizes the vectors, this will return a {@link+ * VectorScorer} that scores against the quantized vectors.** @param target the query vector* @return a {@link VectorScorer} instance or null@@ -75,6 +76,23 @@ public abstract class FloatVectorValues extends KnnVectorValues {throw new UnsupportedOperationException();}+ /**+ * Rescore using the given query vector and the current {@link FloatVectorValues}. This is unique+ * from scorer() in that it is explicitly for rescoring an existing set of hits and thus will+ * often utilize the highest fidelity scoring algorithm available. This is useful when the initial+ * search used a quantized index or an approximate search algorithm, and now we want to rescore+ * the hits using the full fidelity vectors. The default implementation is to call {@link+ * #scorer(float[])} assuming that the scorer is already the highest fidelity implementation+ * available.+ *+ * @param target the query vector+ * @return a {@link VectorScorer} instance or null+ * @throws IOException if an I/O error occurs+ */+ public VectorScorer rescorer(float[] target) throws IOException {+ return scorer(target);+ }+@Overridepublic VectorEncoding getEncoding() {return VectorEncoding.FLOAT32;diff --git a/lucene/core/src/java/org/apache/lucene/search/FullPrecisionFloatVectorSimilarityValuesSource.java b/lucene/core/src/java/org/apache/lucene/search/FullPrecisionFloatVectorSimilarityValuesSource.javaindex b299fd97fdd..f7097e9d261 100644--- a/lucene/core/src/java/org/apache/lucene/search/FullPrecisionFloatVectorSimilarityValuesSource.java+++ b/lucene/core/src/java/org/apache/lucene/search/FullPrecisionFloatVectorSimilarityValuesSource.java@@ -86,7 +86,23 @@ public class FullPrecisionFloatVectorSimilarityValuesSource extends DoubleValues}if (vectorSimilarityFunction == null) {- this.vectorSimilarityFunction = fi.getVectorSimilarityFunction();+ VectorScorer scorer = vectorValues.rescorer(queryVector);+ if (scorer == null) {+ return DoubleValues.EMPTY;+ }+ DocIdSetIterator iterator = scorer.iterator();+ return new DoubleValues() {+ @Override+ public double doubleValue() throws IOException {+ return scorer.score();+ }++ @Override+ public boolean advanceExact(int doc) throws IOException {+ return doc >= iterator.docID()+ && (iterator.docID() == doc || iterator.advance(doc) == doc);+ }+ };}final KnnVectorValues.DocIndexIterator iterator = vectorValues.iterator();return new DoubleValues() {