我的问题很简单,当使用IndexReader.openIfChanged (reader)替换以前的reader时,如何安全地关闭oldReader

下面是代码:(使用Lucene 3.5)

IndexReader newReader=IndexReader.openIfChanged(reader);
if(newReader!=null){
    IndexReader oldReader=reader;
    IndexSearcher oldSearcher=searcher;

    reader=newReader;
    searcher=new IndexSearcher(newReader);

    oldSearcher.close();
    oldReader.close();//or oldReader.decRef(),result is the same
}

该代码在守护线程中,每5秒运行一次
IndexReader实例(reader对象)是全局唯一的

由于此更改,我得到一个例外:
org.apache.lucene.store.AlreadyClosedException: this IndexReader is closed
    at org.apache.lucene.index.IndexReader.ensureOpen(IndexReader.java:297)
    at org.apache.lucene.index.IndexReader.getSequentialSubReaders(IndexReader.java:1622)
    at org.apache.lucene.search.TermQuery$TermWeight.scorer(TermQuery.java:98)
    at org.apache.lucene.search.BooleanQuery$BooleanWeight.scorer(BooleanQuery.java:298)
    at org.apache.lucene.search.BooleanQuery$BooleanWeight.scorer(BooleanQuery.java:298)
    at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:577)
    at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:517)
    at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:487)
    at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:400)
    at org.zenofo.index.IndexManager.query(IndexManager.java:392)
    ...

使用IndexManager.java:392对象的reader(IndexReader实例,全局唯一)
IndexManager.query方法具有大量并发请求,所有请求都使用一个全局唯一的IndexReader实例(reader对象)

我需要关闭oldReader只是因为:
  • Too many open files in Lucene Indexing when number of usersincrease
  • Lucene Wiki:Too many open files

  • 参考:
  • API-IndexReader
  • API-IndexSearcher

  • 我该如何解决这个问题?

    最佳答案

    看一下NRTManager和SearcherManager。您实际上不必自己处理。

    09-27 23:27