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


##########
lucene/core/src/java/org/apache/lucene/search/RescoreTopNQuery.java:
##########
@@ -0,0 +1,128 @@
+/*
+ * 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 java.io.IOException;
+import java.util.Objects;
+import org.apache.lucene.index.IndexReader;
+
+/**
+ * A Query that re-scores another Query with a DoubleValueSource function and 
cut-off the results at
+ * top N.
+ *
+ * @lucene.experimental
+ */
+public class RescoreTopNQuery extends Query {
+
+  private final int n;
+  private final Query query;
+  private final DoubleValuesSource valuesSource;
+
+  /**
+   * Execute the inner Query, re-score using a customizable DoubleValueSource 
and trim down the
+   * result to k
+   *
+   * @param query the query to execute as initial phase
+   * @param valuesSource the double value source to re-score
+   * @param n the number of documents to find
+   * @throws IllegalArgumentException if <code>n</code> is less than 1
+   */
+  public RescoreTopNQuery(Query query, DoubleValuesSource valuesSource, int n) 
{
+    if (n < 1) {
+      throw new IllegalArgumentException("n must be >= 1");
+    }
+    this.query = query;
+    this.valuesSource = valuesSource;
+    this.n = n;
+  }
+
+  @Override
+  public Query rewrite(IndexSearcher indexSearcher) throws IOException {
+    DoubleValuesSource rewrittenValueSource = 
valuesSource.rewrite(indexSearcher);
+    IndexReader reader = indexSearcher.getIndexReader();
+    Query rewritten = indexSearcher.rewrite(query);
+    Weight weight = indexSearcher.createWeight(rewritten, 
ScoreMode.COMPLETE_NO_SCORES, 1.0f);
+    HitQueue queue = new HitQueue(n, false);
+    for (var leaf : reader.leaves()) {
+      Scorer innerScorer = weight.scorer(leaf);
+      if (innerScorer == null) {
+        continue;
+      }
+      DoubleValues rescores = rewrittenValueSource.getValues(leaf, 
getDoubleValues(innerScorer));
+      DocIdSetIterator iterator = innerScorer.iterator();
+      while (iterator.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
+        int docId = iterator.docID();
+        if (rescores.advanceExact(docId)) {
+          double v = rescores.doubleValue();
+          queue.insertWithOverflow(new ScoreDoc(leaf.docBase + docId, (float) 
v));
+        } else {
+          queue.insertWithOverflow(new ScoreDoc(leaf.docBase + docId, 0f));
+        }
+      }
+    }
+    int i = 0;
+    ScoreDoc[] scoreDocs = new ScoreDoc[queue.size()];
+    for (ScoreDoc topDoc : queue) {
+      scoreDocs[i++] = topDoc;
+    }
+    TopDocs topDocs =
+        new TopDocs(new TotalHits(queue.size(), TotalHits.Relation.EQUAL_TO), 
scoreDocs);

Review Comment:
   I used the original result count as totalHits, but keep the relation as 
EQUAL_TO, as it doesn't seem it was exposed anywhere. Usually this would only 
be visible on `search(query, n)` with TopDocsCollector.
   
   I also realized that this `totalHitsRelation` value would not be used 
anywhere so it would not matter.



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