jpountz commented on a change in pull request #413:
URL: https://github.com/apache/lucene/pull/413#discussion_r735928316



##########
File path: lucene/core/src/java/org/apache/lucene/search/KnnVectorQuery.java
##########
@@ -60,7 +60,8 @@ public KnnVectorQuery(String field, float[] target, int k) {
   public Query rewrite(IndexReader reader) throws IOException {
     TopDocs[] perLeafResults = new TopDocs[reader.leaves().size()];
     for (LeafReaderContext ctx : reader.leaves()) {
-      perLeafResults[ctx.ord] = searchLeaf(ctx, Math.min(k, reader.numDocs()));
+      int numDocs = ctx.reader().numDocs();
+      perLeafResults[ctx.ord] = numDocs > 0 ? searchLeaf(ctx, Math.min(k, 
numDocs)) : NO_RESULTS;

Review comment:
       This makes me wonder why we pass `min(k, numDocs)` rather than just `k`. 
Is it to avoid oversizing the heap that collect nearest neighbors?

##########
File path: lucene/core/src/java/org/apache/lucene/search/KnnVectorQuery.java
##########
@@ -60,7 +60,8 @@ public KnnVectorQuery(String field, float[] target, int k) {
   public Query rewrite(IndexReader reader) throws IOException {
     TopDocs[] perLeafResults = new TopDocs[reader.leaves().size()];
     for (LeafReaderContext ctx : reader.leaves()) {
-      perLeafResults[ctx.ord] = searchLeaf(ctx, Math.min(k, reader.numDocs()));
+      int numDocs = ctx.reader().numDocs();
+      perLeafResults[ctx.ord] = numDocs > 0 ? searchLeaf(ctx, Math.min(k, 
numDocs)) : NO_RESULTS;

Review comment:
       One reason why I'm wondering this is because in the past we tried to 
avoid calling `numDocs()`unless it was strictly necessary in the past because 
it's expensive on reader views that hide subsets of documents 
([LUCENE-9003](https://issues.apache.org/jira/browse/LUCENE-9003)).




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to