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/08/24 15:26:00 UTC

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

jpountz commented on pull request #242:
URL: https://github.com/apache/lucene/pull/242#issuecomment-904741873


   > Note: This method is probably slower than the pre-existing one because it is not slice-level multithreaded like the previous one
   
   FWIW I was thinking that we could benefit from this new shortcut and from multithreaded search at the same time by doing something like this in the end:
   
   ```java
     private static class ShortcutHitCountCollector implements Collector {
       private final Weight weight;
       private final TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
       private int weightCount;
   
       ShortcutHitCountCollector(Weight weight) {
         this.weight = weight;
       }
       
       @Override
       public LeafCollector getLeafCollector(LeafReaderContext context) throws IOException {
         int count = weight.count(context);
         if (count == -1) {
           return totalHitCountCollector.getLeafCollector(context);
         } else {
           weightCount += count;
           throw new CollectionTerminatedException();
         }
       }
     }
   
     /** Count how many documents match the given query. */
     public int count(Query query) throws IOException {
       query = rewrite(query);
       Weight weight = createWeight(query, ScoreMode.COMPLETE_NO_SCORES, 1f);
       return search(weight, new CollectorManager<ShortcutHitCountCollector, Integer>() {
   
         @Override
         public ShortcutHitCountCollector newCollector() throws IOException {
           return new ShortcutHitCountCollector(weight);
         }
   
         @Override
         public Integer reduce(Collection<ShortcutHitCountCollector> collectors) throws IOException {
           int totalHitCount;
           for (ShortcutHitCountCollector c : collectors) {
             totalHitCount += c.weightCount + c.totalHitCountCollector.getTotalHits();
           }
         }
       });
     }
   ```


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