Package org.apache.lucene.search
Class SearcherManager
- java.lang.Object
-
- org.apache.lucene.search.ReferenceManager<IndexSearcher>
-
- org.apache.lucene.search.SearcherManager
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public final class SearcherManager extends ReferenceManager<IndexSearcher>
Utility class to safely shareIndexSearcherinstances across multiple threads, while periodically reopening. This class ensures each searcher is closed only once all threads have finished using it.Use
ReferenceManager.acquire()to obtain the current searcher, andReferenceManager.release(G)to release it, like this:IndexSearcher s = manager.acquire(); try { // Do searching, doc retrieval, etc. with s } finally { manager.release(s); } // Do not use s after this! s = null;In addition you should periodically call
ReferenceManager.maybeRefresh(). While it's possible to call this just before running each query, this is discouraged since it penalizes the unlucky queries that do the reopen. It's better to use a separate background thread, that periodically calls maybeReopen. Finally, be sure to callReferenceManager.close()once you are done.- See Also:
SearcherFactory- WARNING: This API is experimental and might change in incompatible ways in the next release.
-
-
Field Summary
-
Fields inherited from class org.apache.lucene.search.ReferenceManager
current
-
-
Constructor Summary
Constructors Constructor Description SearcherManager(IndexWriter writer, boolean applyAllDeletes, SearcherFactory searcherFactory)Creates and returns a new SearcherManager from the givenIndexWriter.SearcherManager(Directory dir, SearcherFactory searcherFactory)Creates and returns a new SearcherManager from the givenDirectory.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected voiddecRef(IndexSearcher reference)Decrement reference counting on the given reference.booleanisSearcherCurrent()Returnstrueif no changes have occured since this searcher ie.booleanmaybeReopen()Deprecated.protected IndexSearcherrefreshIfNeeded(IndexSearcher referenceToRefresh)Refresh the given reference if needed.protected booleantryIncRef(IndexSearcher reference)Try to increment reference counting on the given reference.-
Methods inherited from class org.apache.lucene.search.ReferenceManager
acquire, afterClose, afterRefresh, close, maybeRefresh, release
-
-
-
-
Constructor Detail
-
SearcherManager
public SearcherManager(IndexWriter writer, boolean applyAllDeletes, SearcherFactory searcherFactory) throws IOException
Creates and returns a new SearcherManager from the givenIndexWriter.- Parameters:
writer- the IndexWriter to open the IndexReader from.applyAllDeletes- Iftrue, all buffered deletes will be applied (made visible) in theIndexSearcher/IndexReader. Iffalse, the deletes may or may not be applied, but remain buffered (in IndexWriter) so that they will be applied in the future. Applying deletes can be costly, so if your app can tolerate deleted documents being returned you might gain some performance by passingfalse. SeeIndexReader.openIfChanged(IndexReader, IndexWriter, boolean).searcherFactory- An optionalSearcherFactory. Passnullif you don't require the searcher to be warmed before going live or other custom behavior.- Throws:
IOException
-
SearcherManager
public SearcherManager(Directory dir, SearcherFactory searcherFactory) throws IOException
Creates and returns a new SearcherManager from the givenDirectory.- Parameters:
dir- the directory to open the DirectoryReader on.searcherFactory- An optionalSearcherFactory. Passnullif you don't require the searcher to be warmed before going live or other custom behavior.- 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
-
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
-
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>
-
maybeReopen
@Deprecated public boolean maybeReopen() throws IOException
Deprecated.- 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()
-
-