简体   繁体   中英

Do I need to close DirectoryReader while using Lucene Near real-time indexing feature

Am using Lucence 4.7.2 and am new to it. I tried looking into the lucene source code, but was not able to find the information. The reason for using near real-time is, while searching the index should be visible withing 1 minute of creation.

I have created the following trying to implement/use lucene NRT (near real-time) feature.

//Code to initialize IndexWriter and Near real-time IndexReader. 
//(DirectoryReader is used as IndexReader(IndexWriter, boolean) contructor is deprecated.
directory = FSDirectory.open(new File("C:/Users/arun/lucene-home/"));
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47);
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_47, analyzer);
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);

//NOTE 1
DirectoryReader directoryReader = DirectoryReader.open(indexWriter, true); 

As per documentation of DirectoryReader openIfChanged(DirectoryReader oldReader, IndexWriter writer, boolean applyAllDeletes)

Everytime before performing a search, I create IndexSearcher as below

DirectoryReader newDirectoryReader = DirectoryReader.openIfChanged(directoryReader , indexWriter, true);
IndexSearcher nrtIndexSearcher = new IndexSearcher(newDirectoryreader);

Question:

Will the original DirectoryReader "directoryReader " stated in the above code with comment "NOTE 1" be closed by lucene itself? I mean the DirectoryReader implementation or the class that used in it?

If not, how do I track if the IndexSearcher that was created from the DirectoryReader is still being referenced before closing DirectoryReader.

Note: I will not be able to use Solr, Please excuse me.

Each IndexReader has to be closed after being used, otherwise you will end up with "Too many open files" exception.

If you are using openIfChanged , you can check reference equality to see if the reader is different or not:

DirectoryReader oldReader = directoryReader;
DirectoryReader newReader = DirectoryReader.openIfChanged(directoryReader);
if ((newReader != null) & (oldReader != newReader)) {
   directoryReader = newReader;
   oldReader.close();
   // need to close the old one
} else {
   // nothing to do
}

Note: if you are using this in a multi-threaded environment, there's a chance the old reader is still in use - so if you close it too early, searches using old reader (searcher, created using this reader) will fail. The cure for this is NRTManager and/or SearcherManager .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM