Marc
Marc

Reputation: 13194

Changes made by an IndexWriter are not reflected by an IndexReader

I have an IndexReader in read only mode, an IndexSearcher based on this reader and an IndexWriter working on the same Lucene index. I want to delete a document from the index. Afterwards I don't want the document to show up in results returned by the IndexSearcher (that's what deletion is about, I suppose). Here is the code:

_enIndexWriter.DeleteDocuments(query);
_enIndexWriter.Commit();
_enIndexReader.Reopen();
_enIndexSearcher = new IndexSearcher(_enIndexReader);

However, deleted documents are still returned as results, until everything is restarted, i.e. writer, reader and searcher are re-instantiated. Also, it doesn't work to sort out deleted document in the query like this:

if (_enIndexReader.IsDeleted(documentId)) continue;

The documents which were deleted still return false for IndexReader.IsDeleted(Document).

How can I reflect the changes made by the IndexWriter in the IndexSearcher/IndexReader at minimal computational cost? Reinstantiating everything after a deletion is not an option.

I use Lucene.NET v4.0.30319.

Upvotes: 1

Views: 234

Answers (1)

Marc
Marc

Reputation: 13194

Got it. IndexReader.Reopen() returns a reopened instance of the reader on which the method was called, while this stays as it is. Thus, the code needs to be modified like this:

_enIndexWriter.DeleteDocuments(query);
_enIndexWriter.Commit();
_enIndexReader = _enIndexReader.Reopen();
_enIndexSearcher = new IndexSearcher(_enIndexReader);

Upvotes: 1

Related Questions