Lucene IndexSearcher在重建时锁定导致IOException的索引

时间:2022-07-16 03:07:50

I've learned from reading the available documentation that an IndexSearcher instance should be shared across searches, for optimal performance, and that a new instance must be created in order to load any changes made to the index. This implies that the index is writable (using IndexWriter) after having created an instance of IndexSearcher that points to the same directory. However, this is not the behaviour I see in my implementation of Lucene.Net. I'm using FSDirectory. RAMDirectory is not a viable option. The IndexSearcher locks one of the index files (in my implementation it's the _1.cfs file) making the index non-updatable during the lifetime of the IndexSearcher instance.

我从阅读可用文档中了解到,应该跨搜索共享IndexSearcher实例,以获得最佳性能,并且必须创建新实例才能加载对索引所做的任何更改。这意味着在创建指向同一目录的IndexSearcher实例后,索引是可写的(使用IndexWriter)。但是,这不是我在Lucene.Net实现中看到的行为。我正在使用FSDirectory。 RAMDirectory不是一个可行的选择。 IndexSearcher锁定其中一个索引文件(在我的实现中它是_1.cfs文件),使索引在IndexSearcher实例的生命周期内不可更新。

Is this a known behaviour? Can't I rebuild the index from scratch while using an IndexSearcher instance created prior to rebuilding? Is it only possible to to modifications to the index, but not to rebuild it?

这是一种已知行为吗?使用在重建之前创建的IndexSearcher实例时,无法从头开始重建索引吗?是否只能修改索引,而不是重建它?

Here is how I create the IndexSearcher instance:

以下是我创建IndexSearcher实例的方法:

// Create FSDirectory
var directory = FSDirectory.GetDirectory(storagePath, false);

// Create IndexReader
var reader = IndexReader.Open(directory);

// I get the same behaviour regardless of whether I close the directory or not.
directory.Close();

// Create IndexSearcher
var searcher = new IndexSearcher(reader);

// Closing the reader will cause "object reference not set..." when searching.
//reader.Close();

Here is how I create the IndexWriter:

以下是我创建IndexWriter的方法:

var directory = FSDirectory.GetDirectory(storagePath, true);
var indexWriter = new IndexWriter(directory, new StandardAnalyzer(), true);

I'm using Lucene.Net version 2.0.

我正在使用Lucene.Net 2.0版。

Edit:

Upgrading to Lucene.Net 2.1 (thx KenE) and slightly modifying the way I create my IndexWriter fixed the problem:

升级到Lucene.Net 2.1(thx KenE)并稍微修改我创建IndexWriter的方式修复了问题:

var directory = FSDirectory.GetDirectory(storagePath, false);
var indexWriter = new IndexWriter(directory, new StandardAnalyzer(), true);

1 个解决方案

#1


The latest version of Lucene.Net (2.1) appears to support opening an IndexWriter with create=true even when there are open readers:

最新版本的Lucene.Net(2.1)似乎支持使用create = true打开IndexWriter,即使有开放阅读器:

http://incubator.apache.org/lucene.net/docs/2.1/Lucene.Net.Index.IndexWriter.html

Earlier versions are not clear as to whether they support this or not. I would try using 2.1.

早期版本不清楚它们是否支持这一点。我会尝试使用2.1。

#1


The latest version of Lucene.Net (2.1) appears to support opening an IndexWriter with create=true even when there are open readers:

最新版本的Lucene.Net(2.1)似乎支持使用create = true打开IndexWriter,即使有开放阅读器:

http://incubator.apache.org/lucene.net/docs/2.1/Lucene.Net.Index.IndexWriter.html

Earlier versions are not clear as to whether they support this or not. I would try using 2.1.

早期版本不清楚它们是否支持这一点。我会尝试使用2.1。