简体   繁体   English

使用Lucene Near实时索引功能时是否需要关闭DirectoryReader

[英]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. 我正在使用Lucence 4.7.2并且是它的新手。 I tried looking into the lucene source code, but was not able to find the information. 我尝试查看lucene源代码,但无法找到信息。 The reason for using near real-time is, while searching the index should be visible withing 1 minute of creation. 使用接近实时的原因是,搜索索引应该在创建1分钟后可见。

I have created the following trying to implement/use lucene NRT (near real-time) feature. 我创建了以下尝试实现/使用Lucene NRT(近实时)功能的示例。

//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) 根据DirectoryReader openIfChanged(DirectoryReader oldReader,IndexWriter writer,boolean applyAllDeletes)的文档

Everytime before performing a search, I create IndexSearcher as below 每次执行搜索之前,我都会如下创建IndexSearcher

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? 上面的代码中带有注释“ NOTE 1”的原始DirectoryReader“ directoryReader”是否会被Lucene自身关闭? I mean the DirectoryReader implementation or the class that used in it? 我的意思是DirectoryReader实现或在其中使用的类?

If not, how do I track if the IndexSearcher that was created from the DirectoryReader is still being referenced before closing DirectoryReader. 如果不是,在关闭DirectoryReader之前,如何跟踪是否仍在引用从DirectoryReader创建的IndexSearcher。

Note: I will not be able to use Solr, Please excuse me. 注意:我将无法使用Solr,请原谅。

Each IndexReader has to be closed after being used, otherwise you will end up with "Too many open files" exception. 每个IndexReader使用后都必须关闭,否则最终将出现“ Too many open files”异常。

If you are using openIfChanged , you can check reference equality to see if the reader is different or not: 如果使用的是openIfChanged ,则可以检查引用是否相等,以查看读者是否不同:

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 . 解决方法是NRTManager和/或SearcherManager

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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