slow-J commented on code in PR #16271:
URL: https://github.com/apache/lucene/pull/16271#discussion_r3444948182


##########
lucene/core/src/java/org/apache/lucene/search/AbstractKnnVectorQuery.java:
##########
@@ -149,9 +149,64 @@ public Query rewrite(IndexSearcher indexSearcher) throws 
IOException {
       topK = runSearchTasks(tasks, taskExecutor, perLeafResults, 
leafReaderContexts);
     }
     if (topK.scoreDocs.length == 0) {
-      return MatchNoDocsQuery.INSTANCE;
+      return new MatchNoDocsQuery("No documents matched the nearest-neighbor 
search");
     }
-    return DocAndScoreQuery.createDocAndScoreQuery(reader, topK, reentryCount);
+    return DocAndScoreQuery.createDocAndScoreQuery(
+        reader, topK, reentryCount, noMatchExplainer(topK, filterWeight));
+  }
+
+  /** Builds the explainer for documents this query did not collect, capturing 
minTopKScore. */
+  private DocAndScoreQuery.NoMatchExplainer noMatchExplainer(TopDocs topK, 
Weight filterWeight) {
+    // topK is score-descending, so the lowest collected score is the last 
entry.
+    final float minTopKScore = topK.scoreDocs[topK.scoreDocs.length - 1].score;
+    return (context, doc, topN) ->
+        explainNotCollected(context, doc, topN, filterWeight, minTopKScore);

Review Comment:
   Thanks, good catch!
   
   Looking into this a bit, I don't think it would always throw an exception 
when called with a different reader, but it would evaluate the filter against 
the other reader's segment, giving the wrong answer.
   
   Instead of the try/catch, I think we should gate the explain in 
DocAndScoreQuery#createWeight on contextIdentity, like so:
   ```
    if (noMatchExplainer != null
                && ReaderUtil.getTopLevelContext(context).id() == 
contextIdentity)
   ```
   This is similar to the existing check in createWeight.
   
   What do you think?
   



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