Package org.apache.lucene.search
Class CachingCollector
java.lang.Object
org.apache.lucene.search.Collector
org.apache.lucene.search.CachingCollector
Caches all docs, and optionally also scores, coming from
a search, and is then able to replay them to another
collector. You specify the max RAM this class may use.
Once the collection is done, call
isCached()
. If
this returns true, you can use replay(Collector)
against a new collector. If it returns false, this means
too much RAM was required and you must instead re-run the
original search.
NOTE: this class consumes 4 (or 8 bytes, if scoring is cached) per collected document. If the result set is large this can easily be a very substantial amount of RAM!
NOTE: this class caches at least 128 documents before checking RAM limits.
See the Lucene modules/grouping module for more details including a full code example.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returntrue
if this collector does not require the matching docIDs to be delivered in int sort order (smallest to largest) toCollector.collect(int)
.static CachingCollector
create
(boolean acceptDocsOutOfOrder, boolean cacheScores, double maxRAMMB) Creates aCachingCollector
which does not wrap another collector.static CachingCollector
Create a newCachingCollector
that wraps the given collector and caches documents and scores up to the specified RAM threshold.static CachingCollector
Create a newCachingCollector
that wraps the given collector and caches documents and scores up to the specified max docs threshold.boolean
isCached()
abstract void
Replays the cached doc IDs (and scores) to the given Collector.void
setNextReader
(AtomicReaderContext context) Called before collecting from eachAtomicReaderContext
.
-
Field Details
-
other
-
maxDocsToCache
protected final int maxDocsToCache -
cachedSegs
-
cachedDocs
-
curDocs
protected int[] curDocs -
upto
protected int upto -
base
protected int base -
lastDocBase
protected int lastDocBase
-
-
Method Details
-
create
public static CachingCollector create(boolean acceptDocsOutOfOrder, boolean cacheScores, double maxRAMMB) Creates aCachingCollector
which does not wrap another collector. The cached documents and scores can later bereplayed
.- Parameters:
acceptDocsOutOfOrder
- whether documents are allowed to be collected out-of-order
-
create
Create a newCachingCollector
that wraps the given collector and caches documents and scores up to the specified RAM threshold.- Parameters:
other
- the Collector to wrap and delegate calls to.cacheScores
- whether to cache scores in addition to document IDs. Note that this increases the RAM consumed per docmaxRAMMB
- the maximum RAM in MB to consume for caching the documents and scores. If the collector exceeds the threshold, no documents and scores are cached.
-
create
Create a newCachingCollector
that wraps the given collector and caches documents and scores up to the specified max docs threshold.- Parameters:
other
- the Collector to wrap and delegate calls to.cacheScores
- whether to cache scores in addition to document IDs. Note that this increases the RAM consumed per docmaxDocsToCache
- the maximum number of documents for caching the documents and possible the scores. If the collector exceeds the threshold, no documents and scores are cached.
-
acceptsDocsOutOfOrder
public boolean acceptsDocsOutOfOrder()Description copied from class:Collector
Returntrue
if this collector does not require the matching docIDs to be delivered in int sort order (smallest to largest) toCollector.collect(int)
.Most Lucene Query implementations will visit matching docIDs in order. However, some queries (currently limited to certain cases of
BooleanQuery
) can achieve faster searching if theCollector
allows them to deliver the docIDs out of order.Many collectors don't mind getting docIDs out of order, so it's important to return
true
here.- Specified by:
acceptsDocsOutOfOrder
in classCollector
-
isCached
public boolean isCached() -
setNextReader
Description copied from class:Collector
Called before collecting from eachAtomicReaderContext
. All doc ids inCollector.collect(int)
will correspond toIndexReaderContext.reader()
. AddAtomicReaderContext.docBase
to the currentIndexReaderContext.reader()
's internal document id to re-base ids inCollector.collect(int)
.- Specified by:
setNextReader
in classCollector
- Parameters:
context
- next atomic reader context- Throws:
IOException
-
replay
Replays the cached doc IDs (and scores) to the given Collector. If this instance does not cache scores, then Scorer is not set onother.setScorer
as well as scores are not replayed.- Throws:
IllegalStateException
- if this collector is not cached (i.e., if the RAM limits were too low for the number of documents + scores to cache).IllegalArgumentException
- if the given Collect's does not support out-of-order collection, while the collector passed to the ctor does.IOException
-