shubhamvishu commented on code in PR #14009: URL: https://github.com/apache/lucene/pull/14009#discussion_r1862616243
########## lucene/core/src/java/org/apache/lucene/search/RerankKnnFloatVectorQuery.java: ########## @@ -0,0 +1,117 @@ +/* + * 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.search; + +import static org.apache.lucene.search.AbstractKnnVectorQuery.createRewrittenQuery; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Objects; +import org.apache.lucene.index.FieldInfo; +import org.apache.lucene.index.FloatVectorValues; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.VectorSimilarityFunction; + +/** + * A wrapper of KnnFloatVectorQuery which does full-precision reranking. + * + * @lucene.experimental + */ +public class RerankKnnFloatVectorQuery extends Query { + + private final int k; + private final float[] target; + private final KnnFloatVectorQuery query; + + /** + * Execute the KnnFloatVectorQuery and re-rank using full-precision vectors + * + * @param query the KNN query to execute as initial phase + * @param target the target of the search + * @param k the number of documents to find + * @throws IllegalArgumentException if <code>k</code> is less than 1 + */ + public RerankKnnFloatVectorQuery(KnnFloatVectorQuery query, float[] target, int k) { + this.query = query; + this.target = target; + this.k = k; + } + + @Override + public Query rewrite(IndexSearcher indexSearcher) throws IOException { + IndexReader reader = indexSearcher.getIndexReader(); + Query rewritten = indexSearcher.rewrite(query); + // short-circuit: don't re-rank if we already got all possible results + if (query.getK() <= k) { + return rewritten; + } + Weight weight = indexSearcher.createWeight(rewritten, ScoreMode.COMPLETE_NO_SCORES, 1.0f); + HitQueue queue = new HitQueue(k, false); + for (var leaf : reader.leaves()) { Review Comment: Here we have the access to [IndexSearcher#getTaskExecutor](https://github.com/apache/lucene/blob/main/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java#L1150-L1156) and could use it to parallelize the work across segments(like we did earlier with some other query rewrites). But the `HitQueue` here isn't thread-safe. I don't know if using concurrency after making `insertWithOverflow` thread-safe would be really helpful since it looks like the added cost is cheap? or Maybe it will be helpful? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org