Guest User

Untitled

a guest
Sep 2nd, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. Option 1: freqs of top docs
  2.  
  3.     IndexSearcher searcher = ...;
  4.     Query query = ...;
  5.     Term term = ...; // term that we want to count freqs of
  6.     TopDocs topDocs = searcher.search(query, 10);
  7.     for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
  8.       int leafIndex = ReaderUtil.subIndex(scoreDoc.doc, searcher.getIndexReader().leaves());
  9.       LeafReaderContext leaf = searcher.getIndexReader().leaves().get(leafIndex);
  10.       PostingsEnum postings = leaf.reader().postings(term);
  11.       int numOccurrences;
  12.       if (postings.advance(scoreDoc.doc - leaf.docBase) == scoreDoc.doc - leaf.docBase) {
  13.         numOccurrences = postings.freq();
  14.       } else {
  15.         numOccurrences = 0;
  16.       }
  17.     }
  18.  
  19. Option 2: freqs from a collector
  20.     IndexSearcher searcher = ...;
  21.     Query query = ...;
  22.     searcher.search(query, new Collector() {
  23.      
  24.       @Override
  25.       public boolean needsScores() {
  26.         return true;
  27.       }
  28.      
  29.       @Override
  30.       public LeafCollector getLeafCollector(LeafReaderContext context) throws IOException {
  31.         return new LeafCollector() {
  32.          
  33.           Scorer scorer;
  34.          
  35.           @Override
  36.           public void setScorer(Scorer scorer) throws IOException {
  37.             this.scorer = scorer;
  38.           }
  39.          
  40.           @Override
  41.           public void collect(int doc) throws IOException {
  42.             int numOccurrencess = scorer.freq();
  43.           }
  44.         };
  45.       }
  46.     });
Advertisement
Add Comment
Please, Sign In to add comment