我注意到当我的一个虚拟机正在写入我的lucene索引时,我的其他虚拟机上的查询会停止,直到删除了write.lock文件。
当IndexTrant对象正忙于写入时,IndexSearcher对象应该能够从索引中读取。
我使用IndexSearcher如下:
using (IndexSearcher searcher = new IndexSearcher(directory(indexName)))
{
int endIndex = skip + take;
endIndex = endIndex > 0 ? endIndex : 1000;
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_30, searchFields, analyzer);
Query query = parseQuery(searchQuery, parser);
TopDocs results = searcher.Search(query, null, endIndex);
ScoreDoc[] scoreDocs = results.ScoreDocs;
//code to return the data
}
我使用IndexWriter如下:
var analyzer = new StandardAnalyzer(Version.LUCENE_30);
using (var writer = new IndexWriter(directory(_indexName), analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
{
// add data to lucene search index (replaces older entry if any)
foreach (var item in articles) _addArticleToLuceneIndex(item, writer);
// close handles
analyzer.Close();
writer.Dispose();
}
_addArticleToLuceneIndex
方法删除相应的文档并重新添加:
private static void _addArticleToLuceneIndex(NewsRoomArticle article, IndexWriter writer)
{
var searchQuery = new TermQuery(new Term("ArticleId", article.ArticleId.ToString()));
writer.DeleteDocuments(searchQuery);
doc.Add(new Field("ArticleId", article.ArticleId.IsNotEmpty() ? article.ArticleId.ToString() : "", Field.Store.YES, Field.Index.ANALYZED));
//other fields
// other fields
writer.AddDocument(doc);
}
因此,当一个进程写入一批记录而另一个进程同时执行搜索过程时,问题就会出现。执行写操作时暂停搜索,写完成后继续搜索。
我的搜索过程是否可以在不被作者中断的情况下读取数据?