Class NRTManager
- java.lang.Object
-
- org.apache.lucene.search.ReferenceManager<IndexSearcher>
-
- org.apache.lucene.search.NRTManager
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class NRTManager extends ReferenceManager<IndexSearcher>
Utility class to manage sharing near-real-time searchers across multiple searching thread. The difference vs SearcherManager is that this class enables individual requests to wait until specific indexing changes are visible.You must create an IndexWriter, then create a
NRTManager.TrackingIndexWriterfrom it, and pass that to the NRTManager. You may want to create two NRTManagers, once that always applies deletes on refresh and one that does not. In this case you should use a singleNRTManager.TrackingIndexWriterinstance for both.Then, use
ReferenceManager.acquire()to obtain theIndexSearcher, andReferenceManager.release(G)(ideally, from within afinallyclause) to release it.NOTE: to use this class, you must call
ReferenceManager.maybeRefresh()periodically. TheNRTManagerReopenThreadis a simple class to do this on a periodic basis, and reopens more quickly if a request is waiting. If you implement your own reopener, be sure to calladdWaitingListener(org.apache.lucene.search.NRTManager.WaitingListener)so your reopener is notified when a caller is waiting for a specific generation searcher.- See Also:
SearcherFactory- WARNING: This API is experimental and might change in incompatible ways in the next release.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classNRTManager.TrackingIndexWriterClass that tracks changes to a delegated IndexWriter.static interfaceNRTManager.WaitingListenerNRTManager invokes this interface to notify it when a caller is waiting for a specific generation searcher to be visible.
-
Field Summary
-
Fields inherited from class org.apache.lucene.search.ReferenceManager
current
-
-
Constructor Summary
Constructors Constructor Description NRTManager(NRTManager.TrackingIndexWriter writer, SearcherFactory searcherFactory)Create new NRTManager.NRTManager(NRTManager.TrackingIndexWriter writer, SearcherFactory searcherFactory, boolean applyAllDeletes)Expert: just likeNRTManager(TrackingIndexWriter,SearcherFactory), but you can also specify whether each reopened searcher must apply deletes.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddWaitingListener(NRTManager.WaitingListener l)Adds a listener, to be notified when a caller is waiting for a specific generation searcher to be visible.protected voidafterClose()Called after close(), so subclass can free any resources.protected voidafterRefresh()Called after swapReference has installed a new instance.protected voiddecRef(IndexSearcher reference)Decrement reference counting on the given reference.longgetCurrentSearchingGen()Returns generation of current searcher.booleanisSearcherCurrent()Returnstrueif no changes have occured since this searcher ie.protected IndexSearcherrefreshIfNeeded(IndexSearcher referenceToRefresh)Refresh the given reference if needed.voidremoveWaitingListener(NRTManager.WaitingListener l)Remove a listener added withaddWaitingListener(org.apache.lucene.search.NRTManager.WaitingListener).protected booleantryIncRef(IndexSearcher reference)Try to increment reference counting on the given reference.voidwaitForGeneration(long targetGen)Waits for the target generation to become visible in the searcher.voidwaitForGeneration(long targetGen, long time, TimeUnit unit)Waits for the target generation to become visible in the searcher.-
Methods inherited from class org.apache.lucene.search.ReferenceManager
acquire, close, maybeRefresh, release
-
-
-
-
Constructor Detail
-
NRTManager
public NRTManager(NRTManager.TrackingIndexWriter writer, SearcherFactory searcherFactory) throws IOException
Create new NRTManager.- Parameters:
writer- TrackingIndexWriter to open near-real-time readerssearcherFactory- An optionalSearcherFactory. Passnullif you don't require the searcher to be warmed before going live or other custom behavior.- Throws:
IOException
-
NRTManager
public NRTManager(NRTManager.TrackingIndexWriter writer, SearcherFactory searcherFactory, boolean applyAllDeletes) throws IOException
Expert: just likeNRTManager(TrackingIndexWriter,SearcherFactory), but you can also specify whether each reopened searcher must apply deletes. This is useful for cases where certain uses can tolerate seeing some deleted docs, since reopen time is faster if deletes need not be applied.- Throws:
IOException
-
-
Method Detail
-
decRef
protected void decRef(IndexSearcher reference) throws IOException
Description copied from class:ReferenceManagerDecrement reference counting on the given reference.- Specified by:
decRefin classReferenceManager<IndexSearcher>- Throws:
IOException
-
tryIncRef
protected boolean tryIncRef(IndexSearcher reference)
Description copied from class:ReferenceManagerTry to increment reference counting on the given reference. Return true if the operation was successful.- Specified by:
tryIncRefin classReferenceManager<IndexSearcher>
-
addWaitingListener
public void addWaitingListener(NRTManager.WaitingListener l)
Adds a listener, to be notified when a caller is waiting for a specific generation searcher to be visible.
-
removeWaitingListener
public void removeWaitingListener(NRTManager.WaitingListener l)
Remove a listener added withaddWaitingListener(org.apache.lucene.search.NRTManager.WaitingListener).
-
waitForGeneration
public void waitForGeneration(long targetGen)
Waits for the target generation to become visible in the searcher. If the current searcher is older than the target generation, this method will block until the searcher is reopened, by another viaReferenceManager.maybeRefresh()or until theNRTManageris closed.- Parameters:
targetGen- the generation to wait for
-
waitForGeneration
public void waitForGeneration(long targetGen, long time, TimeUnit unit)Waits for the target generation to become visible in the searcher. If the current searcher is older than the target generation, this method will block until the searcher has been reopened by another thread viaReferenceManager.maybeRefresh(), the given waiting time has elapsed, or until the NRTManager is closed.NOTE: if the waiting time elapses before the requested target generation is available the current
SearcherManageris returned instead.- Parameters:
targetGen- the generation to wait fortime- the time to wait for the target generationunit- the waiting time's time unit
-
getCurrentSearchingGen
public long getCurrentSearchingGen()
Returns generation of current searcher.
-
refreshIfNeeded
protected IndexSearcher refreshIfNeeded(IndexSearcher referenceToRefresh) throws IOException
Description copied from class:ReferenceManagerRefresh the given reference if needed. Returnsnullif no refresh was needed, otherwise a new refreshed reference.- Specified by:
refreshIfNeededin classReferenceManager<IndexSearcher>- Throws:
IOException
-
afterRefresh
protected void afterRefresh()
Description copied from class:ReferenceManagerCalled after swapReference has installed a new instance.- Overrides:
afterRefreshin classReferenceManager<IndexSearcher>
-
afterClose
protected void afterClose() throws IOExceptionDescription copied from class:ReferenceManagerCalled after close(), so subclass can free any resources.- Overrides:
afterClosein classReferenceManager<IndexSearcher>- Throws:
IOException
-
isSearcherCurrent
public boolean isSearcherCurrent() throws IOExceptionReturnstrueif no changes have occured since this searcher ie. reader was opened, otherwisefalse.- Throws:
IOException- See Also:
IndexReader.isCurrent()
-
-