Lucene.Net Searcher does not find new documents

161 views Asked by At

I have an application where users insert data into Lucene.NET (v3.0.3.0) and should be able to query for new data after a few seconds.

Currently, I am creating a new instance of IndexSearcher for every query that is executed. This is said to be slow and it is recommended to use a cached instance of IndexSeacher for performance reasons.

As soon as I keep the IndexSearcher around in a cache, the queries seem to refer to old data. Freshly added documents are not found.

I tried to call

searcher.IndexReader.Reopen();

but that did not help. Only the following code will make sure that the search returns the latest documents:

Lucene.Net.Store.Directory dir = Lucene.Net.Store.FSDirectory.Open(@"c:\lucene\test");
searcher = new IndexSearcher(dir);

How can I make sure I read the latest data without having to recreate a new IndexSearcher for every query?

1

There are 1 answers

0
user1691896 On

OK I figured it out.

My mistake was that I thought that IndexReader.Reopen() would cause the IndexReader to Reopen. Actually, this is not the case. Instead, the method returns a new IndexReader, which can be used to create a new IndexSearcher Instance.

With multiple Threads, this can become a bit tricky to get right. Lucene 3.5 has a SearchManager class which helps with this, but I am using Lucene.NET 3.0 which is on version 3.0.3 at the moment.

The following project provides a port of the searchManager class which can be used from lucene 3.0: https://github.com/NielsKuhnel/NrtManager/tree/master/source/Lucene.Net.Contrib.Management