我是Lucene的新手,請耐心等待。Lucene - AlreadyClosedException:此IndexReader已關閉
我有一個LuceneUtility類,我的應用程序使用它來調用RequestIndexSearcher()來獲取indexSearcher對象並執行所有搜索。 每當我返回indexSearcher對象時,我正在更新索引(如果需要更新任何東西)並重新創建IndexSearcher對象以反映新更新(如果有任何新更新),但有時會出現AlreadyClosedException:此IndexReader已關閉。
public class LuceneUtility
{
private static IndexSearcher _searcher;
private static Directory _directory;
private static Lazy<IndexWriter> _writer = new Lazy<IndexWriter>(() => new IndexWriter(_directory, new KeywordLowerCaseAnalyser(), IndexWriter.MaxFieldLength.UNLIMITED));
private static Object lock_Lucene = new object();
//this private constructor makes it a singleton now.
private LuceneUtility() { }
//Static constructor, opening the directory once for all.
static LuceneUtility()
{
string s ="Path of lucene Index";
_directory = FSDirectory.Open(s);
}
public static IndexSearcher IndexSearcher
{
get
{
if (_searcher == null)
{
InitializeSearcher();
}
else if (!_searcher.IndexReader.IsCurrent())
{
_searcher.Dispose();
InitializeSearcher();
}
return _searcher;
}
}
public static IndexWriter IndexWriter
{
get
{
return _writer.Value;
}
}
private static void InitializeSearcher()
{
_searcher = new IndexSearcher(_directory, false);
}
public static IndexSearcher RequestIndexSearcher()
{
lock (lock_Lucene)
{
PerformIndexUpdation();
}
return IndexSearcher;
}
/// <summary>
/// Performs Lucene Index Updation
/// </summary>
private static void PerformIndexUpdation()
{
// Performs Index Updation
}
堆棧跟蹤:
AlreadyClosedException: this IndexReader is closed
Lucene.Net.Index.IndexReader.EnsureOpen()
at Lucene.Net.Index.DirectoryReader.IsCurrent()
at LuceneOperation.LuceneUtility.get_IndexSearcher()
at LuceneOperation.LuceneUtility.RequestIndexSearcher()
所以...什麼這裏的交易...?我究竟做錯了什麼 ?
非常感謝提前! :)
爲了這個目的,Lucene.net 4.8的新版本有一個'SearcherManager' – AndyPook