Package org.apache.lucene.search
Class SearcherManager
- java.lang.Object
-
- org.apache.lucene.search.ReferenceManager<IndexSearcher>
-
- org.apache.lucene.search.SearcherManager
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public final class SearcherManager extends ReferenceManager<IndexSearcher>
Utility class to safely shareIndexSearcher
instances 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
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.lucene.search.ReferenceManager
ReferenceManager.RefreshListener
-
-
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 Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static IndexSearcher
getSearcher(SearcherFactory searcherFactory, IndexReader reader)
Expert: creates a searcher from the providedIndexReader
using the providedSearcherFactory
.boolean
isSearcherCurrent()
Returnstrue
if no changes have occured since this searcher ie.-
Methods inherited from class org.apache.lucene.search.ReferenceManager
acquire, addListener, close, maybeRefresh, maybeRefreshBlocking, release, removeListener
-
-
-
-
Constructor Detail
-
SearcherManager
public SearcherManager(IndexWriter writer, boolean applyAllDeletes, SearcherFactory searcherFactory) throws java.io.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
/DirectoryReader
. 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
. SeeDirectoryReader.openIfChanged(DirectoryReader, IndexWriter, boolean)
.searcherFactory
- An optionalSearcherFactory
. Passnull
if you don't require the searcher to be warmed before going live or other custom behavior.- Throws:
java.io.IOException
- if there is a low-level I/O error
-
SearcherManager
public SearcherManager(Directory dir, SearcherFactory searcherFactory) throws java.io.IOException
Creates and returns a new SearcherManager from the givenDirectory
.- Parameters:
dir
- the directory to open the DirectoryReader on.searcherFactory
- An optionalSearcherFactory
. Passnull
if you don't require the searcher to be warmed before going live or other custom behavior.- Throws:
java.io.IOException
- if there is a low-level I/O error
-
-
Method Detail
-
isSearcherCurrent
public boolean isSearcherCurrent() throws java.io.IOException
Returnstrue
if no changes have occured since this searcher ie. reader was opened, otherwisefalse
.- Throws:
java.io.IOException
- See Also:
DirectoryReader.isCurrent()
-
getSearcher
public static IndexSearcher getSearcher(SearcherFactory searcherFactory, IndexReader reader) throws java.io.IOException
Expert: creates a searcher from the providedIndexReader
using the providedSearcherFactory
. NOTE: this decRefs incoming reader on throwing an exception.- Throws:
java.io.IOException
-
-