LeafCollector
Collector decouples the score from the collected doc: the score computation is skipped entirely if it's not needed. Collectors that do need the score should implement the {@link #setScorer} method, to hold onto the passed {@link Scorer} instance, and call {@link Scorer#score()} within the collect method to compute the current hit's score. If your collector may request the score for a single hit multiple times, you should use {@link ScoreCachingWrappingScorer}.
NOTE: The doc that is passed to the collect method is relative to the current reader. If your collector needs to resolve this to the docID space of the Multi*Reader, you must re-base it by recording the docBase from the most recent setNextReader call. Here's a simple example showing how to collect docIDs into a BitSet:
IndexSearcher searcher = new IndexSearcher(indexReader);
final BitSet bits = new BitSet(indexReader.maxDoc());
searcher.search(query, new Collector() {
public LeafCollector getLeafCollector(LeafReaderContext context)
throws IOException {
final int docBase = context.docBase;
return new LeafCollector() {
// ignore scorer
public void setScorer(Scorer scorer) throws IOException {
}
public void collect(int doc) throws IOException {
bits.set(docBase + doc);
}
};
}
});
Not all collectors will need to rebase the docID. For example, a collector that simply counts the total number of hits would skip it.
Inheritors
Properties
Functions
Called once for every document matching a query, with the unbased document number.
Bulk-collect doc IDs.
Optionally returns an iterator over competitive documents.
Hook that gets called once the leaf that is associated with this collector has finished collecting successfully, including when a CollectionTerminatedException is thrown. This is typically useful to compile data that has been collected on this leaf, e.g. to convert facet counts on leaf ordinals to facet counts on global ordinals. The default implementation does nothing.