Skip to content

Commit 83816c5

Browse files
committed
expose a static for full precision scores
1 parent 8e87b52 commit 83816c5

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

lucene/core/src/java/org/apache/lucene/search/DoubleValuesSource.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,6 @@ public static DoubleValues similarityToQueryVector(
273273
*/
274274
public static DoubleValues similarityToQueryVector(
275275
LeafReaderContext ctx, float[] queryVector, String vectorField) throws IOException {
276-
if (ctx.reader().getFieldInfos().fieldInfo(vectorField).getVectorEncoding()
277-
!= VectorEncoding.FLOAT32) {
278-
throw new IllegalArgumentException(
279-
"Field "
280-
+ vectorField
281-
+ " does not have the expected vector encoding: "
282-
+ VectorEncoding.FLOAT32);
283-
}
284276
return new FloatVectorSimilarityValuesSource(queryVector, vectorField).getValues(ctx, null);
285277
}
286278

lucene/core/src/java/org/apache/lucene/search/FloatVectorSimilarityValuesSource.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.lucene.index.FloatVectorValues;
2525
import org.apache.lucene.index.KnnVectorValues;
2626
import org.apache.lucene.index.LeafReaderContext;
27+
import org.apache.lucene.index.VectorEncoding;
2728
import org.apache.lucene.index.VectorSimilarityFunction;
2829

2930
/**
@@ -32,6 +33,11 @@
3233
*/
3334
class FloatVectorSimilarityValuesSource extends VectorSimilarityValuesSource {
3435

36+
public static DoubleValues fullPrecisionScores(
37+
LeafReaderContext ctx, float[] queryVector, String vectorField) throws IOException {
38+
return new FloatVectorSimilarityValuesSource(queryVector, vectorField, true).getValues(ctx, null);
39+
}
40+
3541
private final float[] queryVector;
3642
private final boolean useFullPrecision;
3743

@@ -69,8 +75,15 @@ public VectorScorer getScorer(LeafReaderContext ctx) throws IOException {
6975
FloatVectorValues.checkField(ctx.reader(), fieldName);
7076
return null;
7177
}
78+
7279
final FieldInfo fi = ctx.reader().getFieldInfos().fieldInfo(fieldName);
73-
final VectorSimilarityFunction vectorSimilarityFunction = fi.getVectorSimilarityFunction();
80+
if (fi.getVectorEncoding() != VectorEncoding.FLOAT32) {
81+
throw new IllegalArgumentException(
82+
"Field "
83+
+ fieldName
84+
+ " does not have the expected vector encoding: "
85+
+ VectorEncoding.FLOAT32);
86+
}
7487
if (fi.getVectorDimension() != queryVector.length) {
7588
throw new IllegalArgumentException(
7689
"Query vector dimension does not match field dimension: "
@@ -79,12 +92,12 @@ public VectorScorer getScorer(LeafReaderContext ctx) throws IOException {
7992
+ fi.getVectorDimension());
8093
}
8194

95+
// default vector scorer
8296
if (useFullPrecision == false) {
83-
// use default VectorScorer for configured reader
8497
return vectorValues.scorer(queryVector);
8598
}
8699

87-
// return a full precision vector scorer
100+
final VectorSimilarityFunction vectorSimilarityFunction = fi.getVectorSimilarityFunction();
88101
return new VectorScorer() {
89102
final KnnVectorValues.DocIndexIterator iterator = vectorValues.iterator();
90103

lucene/core/src/test/org/apache/lucene/search/TestQuantizedVectorSimilarityValueSource.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,11 @@ public void testFullPrecisionVectorSimilarityDVS() throws Exception {
145145
}
146146

147147
float[] queryVector = TestVectorUtil.randomVector(VECTOR_DIMENSION);
148-
FloatVectorSimilarityValuesSource fpSimValueSource =
149-
new FloatVectorSimilarityValuesSource(queryVector, KNN_FIELD, true);
150-
FloatVectorSimilarityValuesSource quantizedSimValueSource =
151-
new FloatVectorSimilarityValuesSource(queryVector, KNN_FIELD);
152-
153148
try (IndexReader reader = DirectoryReader.open(dir)) {
154149
FieldExistsQuery query = new FieldExistsQuery(KNN_FIELD);
155150
for (LeafReaderContext ctx : reader.leaves()) {
156-
DoubleValues fpSimValues = fpSimValueSource.getValues(ctx, null);
157-
DoubleValues quantizedSimValues = quantizedSimValueSource.getValues(ctx, null);
151+
DoubleValues fpSimValues = FloatVectorSimilarityValuesSource.fullPrecisionScores(ctx, queryVector, KNN_FIELD);
152+
DoubleValues quantizedSimValues = DoubleValuesSource.similarityToQueryVector(ctx, queryVector, KNN_FIELD);
158153
// validate when segment has no vectors
159154
if (fpSimValues == DoubleValues.EMPTY || quantizedSimValues == DoubleValues.EMPTY) {
160155
assertEquals(fpSimValues, quantizedSimValues);

0 commit comments

Comments
 (0)