tags:

views:

295

answers:

3

In my project we use Lucene 2.4.1 for fulltext search. This is a J2EE project, IndexSearcher is created once. In the background, the index is refreshed every couple of minutes (when the content changes). Users can search the index through a search mechanism on the page.

The problem is, the results returned by Lucene seem to be cached somehow.

This is scenario I noticed:

  • I start the application and search for 'keyword' - 6 results are returned,
  • Index is refreshed, using Luke I see, that there are 8 results now to query 'keyword',
  • I search again using the application, again 6 results are returned.

I analyzed our configuration and haven't found any caching anywhere. I have debugged the search, and there is no caching in out code, searcher.search returnes 6 results.

Does Lucene cache results internally somehow? What properties etc. should I check?

A: 

That's all I found about Lucene and caching. If that doesn't help, I think, you have to double check your application again if it doesn't resent an old query result.

One of the issue you really don't need, good luck!

Andreas_D
+7  A: 

To see changes made by IndexWriters against an index for which you have an open IndexReader, be sure to call IndexReader.reopen() to see the latest changes.

Make sure also that your IndexWriter is committing the changes, either through an explicit commit(), a close(), or having autoCommit set to true.

Cowan
I solved it by recreating <code>IndexSearcher</code> every time index is updated. This solved the issue. I think under the covers it does pretty much what you said.
Ula Krukar
reopen() is more efficient, as recreating it causes all the segment files to be read, but reopen() knows to only read the segments that have been updated since the last open.
Cowan
+1  A: 

With versions prior to 2.9.0, Lucene cached automatically the results of queries. With later releases there's no caching unless you wrap your query in a QueryFilter and then wrap the result in a CachingWrapperFilter. You could consider switching to a release >= 2.9.0 if reopening the index becomes a problem

Silvio Donnini