views:

563

answers:

1

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.

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?

Here is how I create the IndexSearcher instance:

// 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:

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

I'm using Lucene.Net version 2.0.

Edit:

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

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

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

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.

KenE
Thanks a bunch for answering. I will try it out and see if that version does what is promised.
Marcus