dungba88 commented on code in PR #14009:
URL: https://github.com/apache/lucene/pull/14009#discussion_r1862816139


##########
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:
   That's right. In order to apply parallelism we need to use a per-segment 
queue, then merge it like in 
[AbstractKnnVectorQuery.mergeLeafResults](https://code.amazon.com/packages/Lucene/blobs/c059819a59a99873081833c0f957452ae4be70f0/--/lucene/core/src/java/org/apache/lucene/search/AbstractKnnVectorQuery.java#L231).
 I think the added latency is already low, but still want to try if it helps.



-- 
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

Reply via email to