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.TrackingIndexWriter
from 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.TrackingIndexWriter
instance for both.Then, use
ReferenceManager.acquire()
to obtain theIndexSearcher
, andReferenceManager.release(G)
(ideally, from within afinally
clause) to release it.NOTE: to use this class, you must call
ReferenceManager.maybeRefresh()
periodically. TheNRTManagerReopenThread
is 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 class
NRTManager.TrackingIndexWriter
Class that tracks changes to a delegated IndexWriter.static interface
NRTManager.WaitingListener
NRTManager 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 void
addWaitingListener(NRTManager.WaitingListener l)
Adds a listener, to be notified when a caller is waiting for a specific generation searcher to be visible.protected void
afterClose()
Called after close(), so subclass can free any resources.protected void
afterRefresh()
Called after swapReference has installed a new instance.protected void
decRef(IndexSearcher reference)
Decrement reference counting on the given reference.long
getCurrentSearchingGen()
Returns generation of current searcher.boolean
isSearcherCurrent()
Returnstrue
if no changes have occured since this searcher ie.protected IndexSearcher
refreshIfNeeded(IndexSearcher referenceToRefresh)
Refresh the given reference if needed.void
removeWaitingListener(NRTManager.WaitingListener l)
Remove a listener added withaddWaitingListener(org.apache.lucene.search.NRTManager.WaitingListener)
.protected boolean
tryIncRef(IndexSearcher reference)
Try to increment reference counting on the given reference.void
waitForGeneration(long targetGen)
Waits for the target generation to become visible in the searcher.void
waitForGeneration(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
. Passnull
if 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:ReferenceManager
Decrement reference counting on the given reference.- Specified by:
decRef
in classReferenceManager<IndexSearcher>
- Throws:
IOException
-
tryIncRef
protected boolean tryIncRef(IndexSearcher reference)
Description copied from class:ReferenceManager
Try to increment reference counting on the given reference. Return true if the operation was successful.- Specified by:
tryIncRef
in 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 theNRTManager
is 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
SearcherManager
is 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:ReferenceManager
Refresh the given reference if needed. Returnsnull
if no refresh was needed, otherwise a new refreshed reference.- Specified by:
refreshIfNeeded
in classReferenceManager<IndexSearcher>
- Throws:
IOException
-
afterRefresh
protected void afterRefresh()
Description copied from class:ReferenceManager
Called after swapReference has installed a new instance.- Overrides:
afterRefresh
in classReferenceManager<IndexSearcher>
-
afterClose
protected void afterClose() throws IOException
Description copied from class:ReferenceManager
Called after close(), so subclass can free any resources.- Overrides:
afterClose
in classReferenceManager<IndexSearcher>
- Throws:
IOException
-
isSearcherCurrent
public boolean isSearcherCurrent() throws IOException
Returnstrue
if no changes have occured since this searcher ie. reader was opened, otherwisefalse
.- Throws:
IOException
- See Also:
IndexReader.isCurrent()
-
-