txwei commented on code in PR #16801:
URL: https://github.com/apache/lucene/pull/16801#discussion_r4300672434


##########
lucene/core/src/java/org/apache/lucene/search/TermInSetQuery.java:
##########
@@ -164,6 +164,27 @@ public long getTermsCount() {
     return termData.size();
   }
 
+  @Override
+  public long estimateCost(Terms terms) throws IOException {
+    // Estimate the cost. If the MTQ can provide its term count, we can do a 
better job
+    // estimating.
+    // Cost estimation reasoning is:
+    // Since we know how many query terms there are...
+    //   a. Assume every query term matches at least one document 
(queryTermsCount).
+    //   b. Determine the total number of docs beyond the first one for each 
term.
+    //      That count provides a ceiling on the number of extra docs that 
could match beyond
+    //      that first one. (We omit the first since it's already been counted 
in 2a).
+    // For id fields (i.e. docFreq = 1 for every term), the estimated cost 
will be equal
+    // to the number of query terms.
+    // See: LUCENE-10207
+    long potentialExtraCost = terms.getSumDocFreq();
+    final long indexedTermCount = terms.size();
+    if (indexedTermCount != -1) {
+      potentialExtraCost -= indexedTermCount;
+    }
+    return termData.size() + potentialExtraCost;

Review Comment:
   can we cap by `terms.getrDocCount()`? If `TermInSetQuery` has more terms 
than the segment's dictionary, or if this is a multi valued field with 
`sumDocFreq` > `docCount`, we could provide a tighter estimation here



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