Package org.apache.lucene.search
Class CachingCollectorManager<C extends Collector,R>
java.lang.Object
org.apache.lucene.search.CachingCollectorManager<C,R>
- All Implemented Interfaces:
CollectorManager<CachingCollector,R>
public class CachingCollectorManager<C extends Collector,R>
extends Object
implements CollectorManager<CachingCollector,R>
A
CollectorManager that wraps a delegate CollectorManager and caches all
collected documents (and optionally scores) per slice, so they can be replayed to a second-pass
CollectorManager without re-running the query.
One CachingCollector is created per slice. During replay(org.apache.lucene.search.CollectorManager<C2, R2>), each cached slice
is replayed into a fresh second-pass collector, and all second-pass collectors are reduced
together. This works correctly with both sequential and concurrent search.
Example usage:
CachingCollectorManager<C1, R1> caching = new CachingCollectorManager<>(
firstPassManager, cacheScores, maxRAMMB, null);
R1 firstResult = searcher.search(query, caching);
if (caching.isCached()) {
R2 secondResult = caching.replay(secondPassManager);
} else {
// cache overflowed — re-run the query
R2 secondResult = searcher.search(query, secondPassManager);
}
- WARNING: This API is experimental and might change in incompatible ways in the next release.
-
Constructor Summary
ConstructorsConstructorDescriptionCachingCollectorManager(CollectorManager<C, R> delegate, boolean cacheScores, Double maxRAMMB, Integer maxDocsToCache) -
Method Summary
Modifier and TypeMethodDescriptionbooleanisCached()Returnstrueif the search has been run and all per-slice caches are intact (none overflowed their RAM/doc budget).Return a newCollector.reduce(Collection<CachingCollector> collectors) Reduce the results of individual collectors into a meaningful result.<C2 extends Collector,R2>
R2replay(CollectorManager<C2, R2> secondPassManager) Replays each per-slice cache into a fresh second-pass collector, then reduces all results.
-
Constructor Details
-
CachingCollectorManager
public CachingCollectorManager(CollectorManager<C, R> delegate, boolean cacheScores, Double maxRAMMB, Integer maxDocsToCache) - Parameters:
delegate- the first-passCollectorManagercacheScores- whether to cache scores in addition to document IDsmaxRAMMB- the maximum RAM in MB to use per slice cache, or null if using maxDocsToCachemaxDocsToCache- the maximum number of documents to cache per slice, or null if using maxRAMMB
-
-
Method Details
-
newCollector
Description copied from interface:CollectorManagerReturn a newCollector. This must return a different instance on each call.- Specified by:
newCollectorin interfaceCollectorManager<C extends Collector,R> - Throws:
IOException
-
reduce
Description copied from interface:CollectorManagerReduce the results of individual collectors into a meaningful result. For instance aTopDocsCollectorwould compute thetop docsof each collector and then merge them usingTopDocs.merge(int, TopDocs[]). This method must be called after collection is finished on all provided collectors.- Specified by:
reducein interfaceCollectorManager<C extends Collector,R> - Throws:
IOException
-
isCached
public boolean isCached()Returnstrueif the search has been run and all per-slice caches are intact (none overflowed their RAM/doc budget). Returnsfalseif the search has not yet been run or any cache overflowed. -
replay
public <C2 extends Collector,R2> R2 replay(CollectorManager<C2, R2> secondPassManager) throws IOExceptionReplays each per-slice cache into a fresh second-pass collector, then reduces all results.- Throws:
IllegalStateException- ifisCached()returnsfalseIOException
-