diff --git a/lucene/core/src/java/org/apache/lucene/search/RerankKnnFloatVectorQuery.java b/lucene/core/src/java/org/apache/lucene/search/RerankKnnFloatVectorQuery.java index 753ab22f95d..a7bf7ee5be9 100644 --- a/lucene/core/src/java/org/apache/lucene/search/RerankKnnFloatVectorQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/RerankKnnFloatVectorQuery.java @@ -55,6 +55,10 @@ public class RerankKnnFloatVectorQuery extends Query { 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()) { diff --git a/lucene/core/src/test/org/apache/lucene/search/TestRerankKnnFloatVectorQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestRerankKnnFloatVectorQuery.java index fb8c9cf7e91..9f9ee842e6d 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestRerankKnnFloatVectorQuery.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestRerankKnnFloatVectorQuery.java @@ -91,7 +91,7 @@ public class TestRerankKnnFloatVectorQuery extends LuceneTestCase { IndexSearcher searcher = new IndexSearcher(reader); float[] targetVector = randomFloatVector(VECTOR_DIMENSION, random); int k = 10; - double oversample = 1.0; + double oversample = random.nextFloat(1.5f, 3.0f); KnnFloatVectorQuery knnQuery = new KnnFloatVectorQuery(FIELD, targetVector, k + (int) (k * oversample));