lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Uwe Schindler" <>
Subject RE: Newbie: "Life span" of IndexWriter / IndexSearcher?
Date Thu, 13 Jan 2011 15:40:49 GMT

> We're writing a web application, which naturally needs
> - "IndexSearcher" when users use our search screen
> - "IndexWriter" in a background process that periodically updates and
> optimizes our index.
> Note our writer is exclusive - no other applications/threads ever write to
> index files.
> What's the common practice in terms of resource creation and sharing?
> Specifically:
> 1) Should I have a single IndexSearcher to serve all (concurrent) users?
> I saw such a recommendation in a tutorial, but discovered that an open
> IndexSearcher prevents 'optimize' from merging my files... so should I
close it
> just before optimization? Or should I open an individual (short-lived)
> IndexSearcher for each search request?

You can leave the IndexWriter and IndexSearcher all the time. The only
important thing, changes made by IndexWriter's commit() method are only seen
by IndexSearcher, when the underlying IndexReader is reopened (e.g. by using
IndexReader.reopen()) - please note that this only works with direct access
to the IndexReaders, so I would recommend using the constructors of
IndexSearcher that take IndexReaders (the Directory ones are only for easy
beginner's use). See the Lucene In Action 2 for a good example of a Searcher

> 2) Our tests also imply that IndexWriter.optimize()  takes effect only
> you close() that writer - which is a shame, because I hoped to keep using
> same writer (I hear it's expensive to instantiate). I doing something

This is wrong, see above. As the IndexReader/Searcher keeps the used
segments from the time it was opened, they can't go away until the
"snapshot" view of IndexReader is closed.

In general, it's not recommeneded to optimize indexes since 2.9 unless you
are doing things like delete all documents.


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message