Apache Lucene open-source search software
Introduce optional access to backing vector values from the scorer (#15501)
Chris Hegarty367551036dd4
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 3f9934735d3..1a2ec6a6a69 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -170,6 +170,8 @@ API Changes* GITHUB#15483: Deprecate SortField.setMissingValue() in preparation for making SortField immutablein Lucene 11.0. Missing values can be set as constructor parameters. (Alan Woodward)+* GITHUB#15501: Introduce optional access to backing vector values from the scorer (Chris Hegarty)+New Features---------------------* GITHUB#15328: VectorSimilarityFunction.getValues() now implements doubleVal allowing itsdiff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/bitvectors/FlatBitVectorsScorer.java b/lucene/codecs/src/java/org/apache/lucene/codecs/bitvectors/FlatBitVectorsScorer.javaindex b74e051573b..676dcba4e35 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/bitvectors/FlatBitVectorsScorer.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/bitvectors/FlatBitVectorsScorer.java@@ -24,6 +24,7 @@ import org.apache.lucene.index.KnnVectorValues;import org.apache.lucene.index.VectorSimilarityFunction;import org.apache.lucene.util.Bits;import org.apache.lucene.util.VectorUtil;+import org.apache.lucene.util.hnsw.HasKnnVectorValues;import org.apache.lucene.util.hnsw.RandomVectorScorer;import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier;import org.apache.lucene.util.hnsw.UpdateableRandomVectorScorer;@@ -57,7 +58,7 @@ public class FlatBitVectorsScorer implements FlatVectorsScorer {throw new IllegalArgumentException("vectorValues must be an instance of ByteVectorValues");}- static class BitRandomVectorScorer implements UpdateableRandomVectorScorer {+ static class BitRandomVectorScorer implements UpdateableRandomVectorScorer, HasKnnVectorValues {private final ByteVectorValues vectorValues;private final int bitDimensions;private final byte[] query;@@ -93,6 +94,11 @@ public class FlatBitVectorsScorer implements FlatVectorsScorer {public Bits getAcceptOrds(Bits acceptDocs) {return vectorValues.getAcceptOrds(acceptDocs);}++ @Override+ public KnnVectorValues values() {+ return vectorValues;+ }}static class BitRandomVectorScorerSupplier implements RandomVectorScorerSupplier {diff --git a/lucene/core/src/java/org/apache/lucene/util/hnsw/HasKnnVectorValues.java b/lucene/core/src/java/org/apache/lucene/util/hnsw/HasKnnVectorValues.javanew file mode 100644index 00000000000..c602a8e93b3--- /dev/null+++ b/lucene/core/src/java/org/apache/lucene/util/hnsw/HasKnnVectorValues.java@@ -0,0 +1,31 @@+/*+ * 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.util.hnsw;++import org.apache.lucene.index.KnnVectorValues;++/**+ * Implementors can return the KnnVectorValues from which their scorers read.+ *+ * @lucene.experimental+ */+public interface HasKnnVectorValues {++ /** Returns the backing vector values, or null. */+ KnnVectorValues values();+}diff --git a/lucene/core/src/java/org/apache/lucene/util/hnsw/RandomVectorScorer.java b/lucene/core/src/java/org/apache/lucene/util/hnsw/RandomVectorScorer.javaindex f8fb4593eb7..cc9869d78de 100644--- a/lucene/core/src/java/org/apache/lucene/util/hnsw/RandomVectorScorer.java+++ b/lucene/core/src/java/org/apache/lucene/util/hnsw/RandomVectorScorer.java@@ -79,7 +79,7 @@ public interface RandomVectorScorer {}/** Creates a default scorer for random access vectors. */- abstract class AbstractRandomVectorScorer implements RandomVectorScorer {+ abstract class AbstractRandomVectorScorer implements RandomVectorScorer, HasKnnVectorValues {private final KnnVectorValues values;/**@@ -105,5 +105,10 @@ public interface RandomVectorScorer {public Bits getAcceptOrds(Bits acceptDocs) {return values.getAcceptOrds(acceptDocs);}++ @Override+ public KnnVectorValues values() {+ return values;+ }}}diff --git a/lucene/core/src/java/org/apache/lucene/util/hnsw/UpdateableRandomVectorScorer.java b/lucene/core/src/java/org/apache/lucene/util/hnsw/UpdateableRandomVectorScorer.javaindex ac07cf0fd6e..a2e07a0b821 100644--- a/lucene/core/src/java/org/apache/lucene/util/hnsw/UpdateableRandomVectorScorer.java+++ b/lucene/core/src/java/org/apache/lucene/util/hnsw/UpdateableRandomVectorScorer.java@@ -37,7 +37,8 @@ public interface UpdateableRandomVectorScorer extends RandomVectorScorer {void setScoringOrdinal(int node) throws IOException;/** Creates a default scorer for random access vectors. */- abstract class AbstractUpdateableRandomVectorScorer implements UpdateableRandomVectorScorer {+ abstract class AbstractUpdateableRandomVectorScorer+ implements UpdateableRandomVectorScorer, HasKnnVectorValues {private final KnnVectorValues values;/**@@ -63,5 +64,10 @@ public interface UpdateableRandomVectorScorer extends RandomVectorScorer {public Bits getAcceptOrds(Bits acceptDocs) {return values.getAcceptOrds(acceptDocs);}++ @Override+ public KnnVectorValues values() {+ return values;+ }}}