Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to safely close an IndexReader?
    text
    copied!<p>My question is very simple, When use <code>IndexReader.openIfChanged (reader)</code> replace the previous <code>reader</code>, How to safely the close <code>oldReader</code>?</p> <p>Here is the code: (Use Lucene 3.5)</p> <pre><code>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 } </code></pre> <p>This code in a deamon thread,Every 5 seconds run time</p> <p><code>IndexReader</code> instance(<code>reader</code> object) is globally unique</p> <p>Since this change, I get an exception:</p> <pre><code>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) ... </code></pre> <p><code>IndexManager.java:392</code> using the <code>reader</code> object(<code>IndexReader</code> instance,globally unique)</p> <p><code>IndexManager.query</code> method has a large number of concurrent requests, all requests to use a globally unique <code>IndexReader</code> instance (<code>reader</code> object)</p> <p><img src="https://i.stack.imgur.com/AQiRT.gif" alt="enter image description here"></p> <p>I need to close <code>oldReader</code> just because:</p> <ul> <li><a href="https://stackoverflow.com/questions/8224413/too-many-open-files-in-lucene-indexing-when-number-of-users-increase">Too many open files in Lucene Indexing when number of users increase</a></li> <li><a href="http://wiki.apache.org/lucene-java/LuceneFAQ#Why_am_I_getting_an_IOException_that_says_.22Too_many_open_files.22.3F" rel="nofollow noreferrer">Lucene Wiki:Too many open files</a></li> </ul> <p>Reference:</p> <ul> <li><a href="http://lucene.apache.org/core/3_6_0/api/all/index.html?org/apache/lucene/index/IndexReader.html" rel="nofollow noreferrer">API-IndexReader</a></li> <li><a href="http://lucene.apache.org/core/3_6_0/api/all/index.html?org/apache/lucene/search/IndexSearcher.html" rel="nofollow noreferrer">API-IndexSearcher</a></li> </ul> <p>How do I solve this problem?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload