Class TimeLimitingCollector
- java.lang.Object
-
- org.apache.lucene.search.Collector
-
- org.apache.lucene.search.TimeLimitingCollector
-
public class TimeLimitingCollector extends Collector
TheTimeLimitingCollectoris used to timeout search requests that take longer than the maximum allowed search time limit. After this time is exceeded, the search thread is stopped by throwing aTimeLimitingCollector.TimeExceededException.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTimeLimitingCollector.TimeExceededExceptionThrown when elapsed search time exceeds allowed search time.static classTimeLimitingCollector.TimerThreadThread used to timeout search requests.
-
Constructor Summary
Constructors Constructor Description TimeLimitingCollector(Collector collector, Counter clock, long ticksAllowed)Create a TimeLimitedCollector wrapper over anotherCollectorwith a specified timeout.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanacceptsDocsOutOfOrder()Returntrueif this collector does not require the matching docIDs to be delivered in int sort order (smallest to largest) toCollector.collect(int).voidcollect(int doc)CallsCollector.collect(int)on the decoratedCollectorunless the allowed time has passed, in which case it throws an exception.static CountergetGlobalCounter()Returns the global TimerThreadsCounterstatic TimeLimitingCollector.TimerThreadgetGlobalTimerThread()Returns the globalTimeLimitingCollector.TimerThread.booleanisGreedy()Checks if this time limited collector is greedy in collecting the last hit.voidsetBaseline()Syntactic sugar forsetBaseline(long)usingCounter.get()on the clock passed to the constructor.voidsetBaseline(long clockTime)Sets the baseline for this collector.voidsetCollector(Collector collector)This is so the same timer can be used with a multi-phase search process such as grouping.voidsetGreedy(boolean greedy)Sets whether this time limited collector is greedy.voidsetNextReader(AtomicReaderContext context)Called before collecting from eachAtomicReaderContext.voidsetScorer(Scorer scorer)Called before successive calls toCollector.collect(int).
-
-
-
Constructor Detail
-
TimeLimitingCollector
public TimeLimitingCollector(Collector collector, Counter clock, long ticksAllowed)
Create a TimeLimitedCollector wrapper over anotherCollectorwith a specified timeout.- Parameters:
collector- the wrappedCollectorclock- the timer clockticksAllowed- max time allowed for collecting hits after whichTimeLimitingCollector.TimeExceededExceptionis thrown
-
-
Method Detail
-
setBaseline
public void setBaseline(long clockTime)
Sets the baseline for this collector. By default the collectors baseline is initialized once the first reader is passed to the collector. To include operations executed in prior to the actual document collection set the baseline through this method in your prelude.Example usage:
Counter clock = ...; long baseline = clock.get(); // ... prepare search TimeLimitingCollector collector = new TimeLimitingCollector(c, clock, numTicks); collector.setBaseline(baseline); indexSearcher.search(query, collector);
- See Also:
setBaseline()
-
setBaseline
public void setBaseline()
Syntactic sugar forsetBaseline(long)usingCounter.get()on the clock passed to the constructor.
-
isGreedy
public boolean isGreedy()
Checks if this time limited collector is greedy in collecting the last hit. A non greedy collector, upon a timeout, would throw aTimeLimitingCollector.TimeExceededExceptionwithout allowing the wrapped collector to collect current doc. A greedy one would first allow the wrapped hit collector to collect current doc and only then throw aTimeLimitingCollector.TimeExceededException.- See Also:
setGreedy(boolean)
-
setGreedy
public void setGreedy(boolean greedy)
Sets whether this time limited collector is greedy.- Parameters:
greedy- true to make this time limited greedy- See Also:
isGreedy()
-
collect
public void collect(int doc) throws java.io.IOExceptionCallsCollector.collect(int)on the decoratedCollectorunless the allowed time has passed, in which case it throws an exception.- Specified by:
collectin classCollector- Throws:
TimeLimitingCollector.TimeExceededException- if the time allowed has exceeded.java.io.IOException
-
setNextReader
public void setNextReader(AtomicReaderContext context) throws java.io.IOException
Description copied from class:CollectorCalled before collecting from eachAtomicReaderContext. All doc ids inCollector.collect(int)will correspond toIndexReaderContext.reader(). AddAtomicReaderContext.docBaseto the currentIndexReaderContext.reader()'s internal document id to re-base ids inCollector.collect(int).- Specified by:
setNextReaderin classCollector- Parameters:
context- next atomic reader context- Throws:
java.io.IOException
-
setScorer
public void setScorer(Scorer scorer) throws java.io.IOException
Description copied from class:CollectorCalled before successive calls toCollector.collect(int). Implementations that need the score of the current document (passed-in toCollector.collect(int)), should save the passed-in Scorer and call scorer.score() when needed.
-
acceptsDocsOutOfOrder
public boolean acceptsDocsOutOfOrder()
Description copied from class:CollectorReturntrueif 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 theCollectorallows them to deliver the docIDs out of order.Many collectors don't mind getting docIDs out of order, so it's important to return
truehere.- Specified by:
acceptsDocsOutOfOrderin classCollector
-
setCollector
public void setCollector(Collector collector)
This is so the same timer can be used with a multi-phase search process such as grouping. We don't want to create a new TimeLimitingCollector for each phase because that would reset the timer for each phase. Once time is up subsequent phases need to timeout quickly.- Parameters:
collector- The actual collector performing search functionality
-
getGlobalCounter
public static Counter getGlobalCounter()
Returns the global TimerThreadsCounterInvoking this creates may create a new instance of
TimeLimitingCollector.TimerThreadiff the globalTimeLimitingCollector.TimerThreadhas never been accessed before. The thread returned from this method is started on creation and will be alive unless you stop theTimeLimitingCollector.TimerThreadviaTimeLimitingCollector.TimerThread.stopTimer().- Returns:
- the global TimerThreads
Counter
-
getGlobalTimerThread
public static TimeLimitingCollector.TimerThread getGlobalTimerThread()
Returns the globalTimeLimitingCollector.TimerThread.Invoking this creates may create a new instance of
TimeLimitingCollector.TimerThreadiff the globalTimeLimitingCollector.TimerThreadhas never been accessed before. The thread returned from this method is started on creation and will be alive unless you stop theTimeLimitingCollector.TimerThreadviaTimeLimitingCollector.TimerThread.stopTimer().- Returns:
- the global
TimeLimitingCollector.TimerThread
-
-