Context:
A helper facade class is providing search methods for my application.
As performance is not an issue, a new IndexSearcher
is created for each query.
对于每个询问,都设立了一个新的搜索器:
File indexFile = new File(String absolutePathToIndex);
IndexSearcher searcher = new IndexSearcher(indexFile.getAbsolutePath(), true);
有时,我拿到<条码>已关闭的Exception,我不理解,因为没有分享搜索器物体。
Any ideas? Any best practice of how to open the index? Known issues? Thanks.
Stacktrace:
org.apache.lucene.store.AlreadyClosedException: this Directory is closed
at org.apache.lucene.store.Directory.ensureOpen(Directory.java:251)
at org.apache.lucene.store.FSDirectory.listAll(FSDirectory.java:530)
at org.apache.lucene.index.SegmentInfos$FindSegmentsFile.run(SegmentInfos.java:585)
at org.apache.lucene.index.DirectoryReader.open(DirectoryReader.java:69)
at org.apache.lucene.index.IndexReader.open(IndexReader.java:476)
at org.apache.lucene.index.IndexReader.open(IndexReader.java:243)
at org.apache.lucene.search.IndexSearcher.<init>(IndexSearcher.java:78)
Proposed Solution #1: the way to go?
Directory directory = FSDirectory.open(File indexFile);
IndexSearcher searcher = new IndexSearcher(directory, true);
...do the query...
searcher.close();
Question: the above code from solution #1 is created for EACH query. Is it necessary to close the directory too? Having checked the source code of Lucene 2.9.2, searcher.close()
does not close the directory
associated with the internal reader
object.