|
| 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 | +package org.apache.lucene.search; |
| 18 | + |
| 19 | +import static org.apache.lucene.search.AbstractKnnVectorQuery.createRewrittenQuery; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Objects; |
| 24 | +import org.apache.lucene.index.FieldInfo; |
| 25 | +import org.apache.lucene.index.FloatVectorValues; |
| 26 | +import org.apache.lucene.index.IndexReader; |
| 27 | +import org.apache.lucene.index.VectorSimilarityFunction; |
| 28 | + |
| 29 | +/** |
| 30 | + * A wrapper of KnnFloatVectorQuery which does full-precision reranking. |
| 31 | + * |
| 32 | + * @lucene.experimental |
| 33 | + */ |
| 34 | +public class RerankKnnFloatVectorQuery extends Query { |
| 35 | + |
| 36 | + private final int k; |
| 37 | + private final float[] target; |
| 38 | + private final KnnFloatVectorQuery query; |
| 39 | + |
| 40 | + /** |
| 41 | + * Execute the KnnFloatVectorQuery and re-rank using full-precision vectors |
| 42 | + * |
| 43 | + * @param query the KNN query to execute as initial phase |
| 44 | + * @param target the target of the search |
| 45 | + * @param k the number of documents to find |
| 46 | + * @throws IllegalArgumentException if <code>k</code> is less than 1 |
| 47 | + */ |
| 48 | + public RerankKnnFloatVectorQuery(KnnFloatVectorQuery query, float[] target, int k) { |
| 49 | + this.query = query; |
| 50 | + this.target = target; |
| 51 | + this.k = k; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public Query rewrite(IndexSearcher indexSearcher) throws IOException { |
| 56 | + IndexReader reader = indexSearcher.getIndexReader(); |
| 57 | + Query rewritten = indexSearcher.rewrite(query); |
| 58 | + Weight weight = indexSearcher.createWeight(rewritten, ScoreMode.COMPLETE_NO_SCORES, 1.0f); |
| 59 | + HitQueue queue = new HitQueue(k, false); |
| 60 | + for (var leaf : reader.leaves()) { |
| 61 | + Scorer scorer = weight.scorer(leaf); |
| 62 | + if (scorer == null) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + FloatVectorValues floatVectorValues = leaf.reader().getFloatVectorValues(query.getField()); |
| 66 | + if (floatVectorValues == null) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + FieldInfo fi = leaf.reader().getFieldInfos().fieldInfo(query.getField()); |
| 70 | + VectorSimilarityFunction comparer = fi.getVectorSimilarityFunction(); |
| 71 | + DocIdSetIterator iterator = scorer.iterator(); |
| 72 | + while (iterator.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) { |
| 73 | + int docId = iterator.docID(); |
| 74 | + float[] vectorValue = floatVectorValues.vectorValue(docId); |
| 75 | + float score = comparer.compare(vectorValue, target); |
| 76 | + queue.insertWithOverflow(new ScoreDoc(docId, score)); |
| 77 | + } |
| 78 | + } |
| 79 | + int i = 0; |
| 80 | + ScoreDoc[] scoreDocs = new ScoreDoc[queue.size()]; |
| 81 | + for (ScoreDoc topDoc : queue) { |
| 82 | + scoreDocs[i++] = topDoc; |
| 83 | + } |
| 84 | + return createRewrittenQuery(reader, scoreDocs); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public int hashCode() { |
| 89 | + int result = Arrays.hashCode(target); |
| 90 | + result = 31 * result + Objects.hash(query, k); |
| 91 | + return result; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public boolean equals(Object o) { |
| 96 | + if (this == o) return true; |
| 97 | + RerankKnnFloatVectorQuery that = (RerankKnnFloatVectorQuery) o; |
| 98 | + return Objects.equals(query, that.query) && k == that.k; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void visit(QueryVisitor visitor) { |
| 103 | + query.visit(visitor); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public String toString(String field) { |
| 108 | + return getClass().getSimpleName() + ":" + query.toString(field) + "[" + k + "]"; |
| 109 | + } |
| 110 | +} |
0 commit comments