You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by GitBox <gi...@apache.org> on 2021/09/01 09:31:26 UTC

[GitHub] [lucene] jpountz commented on a change in pull request #242: LUCENE-9620 Add Weight#count(LeafReaderContext)

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



##########
File path: lucene/core/src/java/org/apache/lucene/search/FilterWeight.java
##########
@@ -67,4 +67,9 @@ public Scorer scorer(LeafReaderContext context) throws IOException {
   public Matches matches(LeafReaderContext context, int doc) throws IOException {
     return in.matches(context, doc);
   }
+
+  @Override
+  public int count(LeafReaderContext context) throws IOException {
+    return in.count(context);
+  }

Review comment:
       In general we only forward calls to the wrapped instance in `FilterXXX` classes when the method is abstract. This is why `FilterWeight` doesn't implement `bulkScorer` for instance.
   
   The reason why we do this is because it can be a bit trappy otherwise, e.g. someone extends FilterWeight and overrides the `scorer()` method but forgets to override `count`.
   
   We should move this to the concrete instances where it's correct to forward to the wrapped instance like the Weight of `ConstantScoreQuery`.

##########
File path: lucene/core/src/java/org/apache/lucene/search/TermQuery.java
##########
@@ -179,6 +179,22 @@ public Explanation explain(LeafReaderContext context, int doc) throws IOExceptio
       }
       return Explanation.noMatch("no matching term");
     }
+
+    @Override
+    public int count(LeafReaderContext context) throws IOException {
+      if (context.reader().hasDeletions() == false) {
+        TermsEnum termsEnum = getTermsEnum(context);
+        // termsEnum is not null if term state is available
+        if (termsEnum != null) {
+          return termsEnum.docFreq();
+        } else {
+          // no term state found so rely on the default reader.docFreq call
+          return context.reader().docFreq(term);

Review comment:
       You could return `0` directly here, if `getTermsEnum` returns `null` , it means that the term cannot be found in the dictionary, so the count for the query is 0.




-- 
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-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org