3
votes

I am trying to understand how Lucene should be used.

From what I have read, creating an IndexReader is costly, so using a Search Manager shoulg be the right choice. However, a SearchManager should be produced by a NRTManager(which, by the way, should replace the IndexWriter for every add or delete operation performed). But in order to have a NRTManager, I should first have an IndexWriter, and here comes my problem.

The documentation says:

  • an IndexWriter is thread-safe
  • the constructor of this class takes a Directory object, so it seems creating an instace should be costly(as in the case of an IndexReader)
  • all changes are buffered and flushed periodically(so they seem to encourage using a single instance)

    but:

  • the changes, although flushed will only be visible after commit or close

  • after finished making updates(add/delete), the instance should be closed
  • I also found this: Forgot to close the Lucene IndexWriter after adding Documents to the index where it is said that not closing a writer might ruin everything

So what am I really supposed to do? Is having a single IndexWriter instance a good idea(make only commit and never close it)?

What is more, if I use NRTManager, how can I make a commit? Is it even possible?

1

1 Answers

3
votes

This blog article will probably help you understand how to use NRTManager and SearcherManager. It explains how to handle commits and reopens in a near-realtime context.

Regarding your IndexWriter questions, yes, using a single IndexWriter is encouraged, but you always need to close your IndexWriter at some point. Closing an IndexWriter will perform several things:

  • wait for the current merge operations to finish (optional),
  • commit pending data,
  • remove the directory lock file.

This is why documents might not be visible if the IndexWriter was not closed whereas there were still pending changes to the index.

In a long-running application, you should close your IndexWriter when the application exits (in a desktop application, this can be done when the users closes the application, and in a webapp, this can be done in the destroy method of a servlet).