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


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

Review Comment:
   Can we implement `Weight` and `Scorer` classes for this query instead of 
running it in `rewrite` ? I know `AbstractKnnVectorQuery` uses rewrite and my 
guess is because it needs to decide b/w exact and approximate search. But we 
hopefully don't have such requirements here?



##########
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) {

Review Comment:
   I wonder if we could generalize it to rerank any query with full precision 
vector similarity, instead of only `KnnFloatVectorQuery`s.



##########
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()) {
+      Scorer scorer = weight.scorer(leaf);
+      if (scorer == null) {
+        continue;
+      }
+      FloatVectorValues floatVectorValues = 
leaf.reader().getFloatVectorValues(query.getField());
+      if (floatVectorValues == null) {
+        continue;
+      }
+      FieldInfo fi = leaf.reader().getFieldInfos().fieldInfo(query.getField());
+      if (fi == null) {
+        continue;
+      }
+      VectorSimilarityFunction comparer = fi.getVectorSimilarityFunction();
+      DocIdSetIterator iterator = scorer.iterator();
+      while (iterator.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
+        int docId = iterator.docID();
+        float[] vectorValue = floatVectorValues.vectorValue(docId);
+        float score = comparer.compare(vectorValue, target);
+        queue.insertWithOverflow(new ScoreDoc(leaf.docBase + docId, score));
+      }
+    }
+    int i = 0;
+    ScoreDoc[] scoreDocs = new ScoreDoc[queue.size()];
+    for (ScoreDoc topDoc : queue) {
+      scoreDocs[i++] = topDoc;
+    }
+    return createRewrittenQuery(reader, scoreDocs);

Review Comment:
   nit: should we use `AbstractKnnVectorQuery.createRewrittenQuery()` instead 
of a static import, for readability?



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