public final class SearcherManager extends ReferenceManager<IndexSearcher>
IndexSearcher
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, and ReferenceManager.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 call ReferenceManager.close()
once you are done.
SearcherFactory
ReferenceManager.RefreshListener
Constructor and Description |
---|
SearcherManager(Directory dir,
SearcherFactory searcherFactory)
Creates and returns a new SearcherManager from the given
Directory . |
SearcherManager(IndexWriter writer,
boolean applyAllDeletes,
SearcherFactory searcherFactory)
Creates and returns a new SearcherManager from the given
IndexWriter . |
Modifier and Type | Method and Description |
---|---|
static IndexSearcher |
getSearcher(SearcherFactory searcherFactory,
IndexReader reader)
Expert: creates a searcher from the provided
IndexReader using the provided SearcherFactory . |
boolean |
isSearcherCurrent()
Returns
true if no changes have occured since this searcher
ie. |
acquire, addListener, close, maybeRefresh, maybeRefreshBlocking, release, removeListener
public SearcherManager(IndexWriter writer, boolean applyAllDeletes, SearcherFactory searcherFactory) throws IOException
IndexWriter
.writer
- the IndexWriter to open the IndexReader from.applyAllDeletes
- If true
, all buffered deletes will be applied (made
visible) in the IndexSearcher
/ DirectoryReader
.
If false
, 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 passing false
. See
DirectoryReader.openIfChanged(DirectoryReader, IndexWriter, boolean)
.searcherFactory
- An optional SearcherFactory
. Pass null
if you
don't require the searcher to be warmed before going live or other
custom behavior.IOException
- if there is a low-level I/O errorpublic SearcherManager(Directory dir, SearcherFactory searcherFactory) throws IOException
Directory
.dir
- the directory to open the DirectoryReader on.searcherFactory
- An optional SearcherFactory
. Pass
null
if you don't require the searcher to be warmed
before going live or other custom behavior.IOException
- if there is a low-level I/O errorpublic boolean isSearcherCurrent() throws IOException
true
if no changes have occured since this searcher
ie. reader was opened, otherwise false
.IOException
DirectoryReader.isCurrent()
public static IndexSearcher getSearcher(SearcherFactory searcherFactory, IndexReader reader) throws IOException
IndexReader
using the provided SearcherFactory
. NOTE: this decRefs incoming reader
on throwing an exception.IOException
Copyright © 2010 - 2020 Adobe. All Rights Reserved