|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.lucene.search; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Objects; |
| 23 | +import org.apache.lucene.index.FieldInfo; |
| 24 | +import org.apache.lucene.index.FloatVectorValues; |
| 25 | +import org.apache.lucene.index.KnnVectorValues; |
| 26 | +import org.apache.lucene.index.LeafReaderContext; |
| 27 | +import org.apache.lucene.index.VectorSimilarityFunction; |
| 28 | + |
| 29 | +/** |
| 30 | + * A {@link DoubleValuesSource} that computes vector similarity between a query vector and raw full |
| 31 | + * precision vectors indexed in provided {@link org.apache.lucene.document.KnnFloatVectorField} in |
| 32 | + * documents. |
| 33 | + */ |
| 34 | +public class FullPrecisionFloatVectorSimilarityValuesSource extends DoubleValuesSource { |
| 35 | + |
| 36 | + private final float[] queryVector; |
| 37 | + private final String fieldName; |
| 38 | + private VectorSimilarityFunction vectorSimilarityFunction; |
| 39 | + |
| 40 | + /** |
| 41 | + * Creates a {@link DoubleValuesSource} that returns vector similarity score between provided |
| 42 | + * query vector and field for documents. |
| 43 | + * |
| 44 | + * @param vector the query vector |
| 45 | + * @param fieldName the field name of the {@link org.apache.lucene.document.KnnFloatVectorField} |
| 46 | + * @param vectorSimilarityFunction the vector similarity function to use |
| 47 | + */ |
| 48 | + public FullPrecisionFloatVectorSimilarityValuesSource( |
| 49 | + float[] vector, String fieldName, VectorSimilarityFunction vectorSimilarityFunction) { |
| 50 | + this.queryVector = vector; |
| 51 | + this.fieldName = fieldName; |
| 52 | + this.vectorSimilarityFunction = vectorSimilarityFunction; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates a {@link DoubleValuesSource} that returns vector similarity score between provided |
| 57 | + * query vector and field for documents. Uses the configured vector similarity function for the |
| 58 | + * field. |
| 59 | + * |
| 60 | + * @param vector the query vector |
| 61 | + * @param fieldName the field name of the {@link org.apache.lucene.document.KnnFloatVectorField} |
| 62 | + */ |
| 63 | + public FullPrecisionFloatVectorSimilarityValuesSource(float[] vector, String fieldName) { |
| 64 | + this(vector, fieldName, null); |
| 65 | + } |
| 66 | + |
| 67 | + /** Sugar to fetch full precision similarity score values */ |
| 68 | + public DoubleValues getSimilarityScores(LeafReaderContext ctx) throws IOException { |
| 69 | + return getValues(ctx, null); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public DoubleValues getValues(LeafReaderContext ctx, DoubleValues scores) throws IOException { |
| 74 | + final FloatVectorValues vectorValues = ctx.reader().getFloatVectorValues(fieldName); |
| 75 | + if (vectorValues == null) { |
| 76 | + FloatVectorValues.checkField(ctx.reader(), fieldName); |
| 77 | + return DoubleValues.EMPTY; |
| 78 | + } |
| 79 | + final FieldInfo fi = ctx.reader().getFieldInfos().fieldInfo(fieldName); |
| 80 | + if (fi.getVectorDimension() != queryVector.length) { |
| 81 | + throw new IllegalArgumentException( |
| 82 | + "Query vector dimension does not match field dimension: " |
| 83 | + + queryVector.length |
| 84 | + + " != " |
| 85 | + + fi.getVectorDimension()); |
| 86 | + } |
| 87 | + |
| 88 | + if (vectorSimilarityFunction == null) { |
| 89 | + this.vectorSimilarityFunction = fi.getVectorSimilarityFunction(); |
| 90 | + } |
| 91 | + final KnnVectorValues.DocIndexIterator iterator = vectorValues.iterator(); |
| 92 | + return new DoubleValues() { |
| 93 | + @Override |
| 94 | + public double doubleValue() throws IOException { |
| 95 | + return vectorSimilarityFunction.compare( |
| 96 | + queryVector, vectorValues.vectorValue(iterator.index())); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public boolean advanceExact(int doc) throws IOException { |
| 101 | + return doc >= iterator.docID() && (iterator.docID() == doc || iterator.advance(doc) == doc); |
| 102 | + } |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public boolean needsScores() { |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public DoubleValuesSource rewrite(IndexSearcher reader) throws IOException { |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public int hashCode() { |
| 118 | + return Objects.hash(fieldName, Arrays.hashCode(queryVector), vectorSimilarityFunction); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public boolean equals(Object obj) { |
| 123 | + if (this == obj) return true; |
| 124 | + if (obj == null || getClass() != obj.getClass()) return false; |
| 125 | + FullPrecisionFloatVectorSimilarityValuesSource other = |
| 126 | + (FullPrecisionFloatVectorSimilarityValuesSource) obj; |
| 127 | + return Objects.equals(fieldName, other.fieldName) |
| 128 | + && Objects.equals(vectorSimilarityFunction, other.vectorSimilarityFunction) |
| 129 | + && Arrays.equals(queryVector, other.queryVector); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public String toString() { |
| 134 | + return "FullPrecisionFloatVectorSimilarityValuesSource(fieldName=" |
| 135 | + + fieldName |
| 136 | + + " vectorSimilarityFunction=" |
| 137 | + + vectorSimilarityFunction.name() |
| 138 | + + " queryVector=" |
| 139 | + + Arrays.toString(queryVector) |
| 140 | + + ")"; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public boolean isCacheable(LeafReaderContext ctx) { |
| 145 | + return true; |
| 146 | + } |
| 147 | +} |
0 commit comments