Method OpenIfChanged
OpenIfChanged(DirectoryReader)
If the index has changed since the provided reader was
opened, open and return a new reader; else, return
null
. The new reader, if not null
, will be the same
type of reader as the previous one, ie a near-real-time (NRT) reader
will open a new NRT reader, a MultiReader will open a
new MultiReader, etc.
This method is typically far less costly than opening a fully new DirectoryReader as it shares resources (for example sub-readers) with the provided DirectoryReader, when possible.
The provided reader is not disposed (you are responsible for doing so); if a new reader is returned you also must eventually dispose it. Be sure to never dispose a reader while other threads are still using it; see SearcherManager to simplify managing this.
Declaration
public static DirectoryReader OpenIfChanged(DirectoryReader oldReader)
Parameters
Type | Name | Description |
---|---|---|
DirectoryReader | oldReader |
Returns
Type | Description |
---|---|
DirectoryReader |
|
Exceptions
Type | Condition |
---|---|
CorruptIndexException | if the index is corrupt |
OpenIfChanged(DirectoryReader, IndexCommit)
If the IndexCommit differs from what the
provided reader is searching, open and return a new
reader; else, return null
.
Declaration
public static DirectoryReader OpenIfChanged(DirectoryReader oldReader, IndexCommit commit)
Parameters
Type | Name | Description |
---|---|---|
DirectoryReader | oldReader | |
IndexCommit | commit |
Returns
Type | Description |
---|---|
DirectoryReader |
See Also
OpenIfChanged(DirectoryReader, IndexWriter, Boolean)
Expert: If there changes (committed or not) in the
IndexWriter versus what the provided reader is
searching, then open and return a new
IndexReader searching both committed and uncommitted
changes from the writer; else, return null
(though, the
current implementation never returns null
).
This provides "near real-time" searching, in that changes made during an IndexWriter session can be quickly made available for searching without closing the writer nor calling Commit().
It's near real-time because there is no hard guarantee on how quickly you can get a new reader after making changes with IndexWriter. You'll have to experiment in your situation to determine if it's fast enough. As this is a new and experimental feature, please report back on your findings so we can learn, improve and iterate.
The very first time this method is called, this writer instance will make every effort to pool the readers that it opens for doing merges, applying deletes, etc. This means additional resources (RAM, file descriptors, CPU time) will be consumed.
For lower latency on reopening a reader, you should call MergedSegmentWarmer (on IndexWriterConfig) to pre-warm a newly merged segment before it's committed to the index. This is important for minimizing index-to-search delay after a large merge.
If an AddIndexes* call is running in another thread, then this reader will only search those segments from the foreign index that have been successfully copied over, so far.
NOTE: Once the writer is disposed, any
outstanding readers may continue to be used. However,
if you attempt to reopen any of those readers, you'll
hit an
@lucene.experimental
Declaration
public static DirectoryReader OpenIfChanged(DirectoryReader oldReader, IndexWriter writer, bool applyAllDeletes)
Parameters
Type | Name | Description |
---|---|---|
DirectoryReader | oldReader | |
IndexWriter | writer | The IndexWriter to open from |
System.Boolean | applyAllDeletes | If |
Returns
Type | Description |
---|---|
DirectoryReader | DirectoryReader that covers entire index plus all
changes made so far by this IndexWriter instance, or
|