-
openIfChanged
public static DirectoryReader openIfChanged(DirectoryReader oldReader) throws IOException
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 an 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 providedDirectoryReader
, when possible.The provided reader is not closed (you are responsible for doing so); if a new reader is returned you also must eventually close it. Be sure to never close a reader while other threads are still using it; see
SearcherManager
to simplify managing this.- Returns:
- null if there are no changes; else, a new DirectoryReader instance which you must eventually close
- Throws:
CorruptIndexException
- if the index is corruptIOException
- if there is a low-level IO error
如果旧reader是一个NRT reader, 使用此方法重新打开reader可以发现writer的修改,而不需要提交。
-
openIfChanged
public static DirectoryReader openIfChanged(DirectoryReader oldReader, IndexWriter writer) throws IOException
Expert: If there changes (committed or not) in theIndexWriter
VS 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 callingIndexWriter.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
IndexWriterConfig.setMergedSegmentWarmer(org.apache.lucene.index.IndexWriter.IndexReaderWarmer)
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 closed, any outstanding readers may continue to be used. However, if you attempt to reopen any of those readers, you'll hit an
AlreadyClosedException
.- Parameters:
writer
- The IndexWriter to open from- Returns:
- DirectoryReader that covers entire index plus all changes made so far by this IndexWriter instance, or null if there are no new changes
- Throws:
IOException
- if there is a low-level IO error- WARNING: This API is experimental and might change in incompatible ways in the next release.
-
openIfChanged
public static DirectoryReader openIfChanged(DirectoryReader oldReader, IndexWriter writer, boolean applyAllDeletes) throws IOException
Expert: Opens a new reader, if there are any changes, controlling whether past deletions should be applied.- Parameters:
writer
- The IndexWriter to open fromapplyAllDeletes
- If true, all buffered deletes will be applied (made visible) in the returned reader. If false, the deletes are not 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.- Throws:
IOException
- if there is a low-level IO error- See Also:
openIfChanged(DirectoryReader,IndexWriter)
- WARNING: This API is experimental and might change in incompatible ways in the next release.