lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sol myr <>
Subject RE: Newbie: "Life span" of IndexWriter / IndexSearcher?
Date Sun, 16 Jan 2011 10:06:25 GMT

Thank you kindly for replying. 
Unfortunately, reopen() doesn't help me see the changes.
Here's my test:
First I write & commit a document, and run a search - which correctly finds this document.
Then I write & commit another document, re-open the reader and run another search - this
should find 2 documents, but it only finds 1 document (the first one).
BTW if instead of 'reader.reopen()' I instantiate a brand-new searcher (and reader), it correctly
finds 2 documents...

// Shared objects:
Directory directory = File("c:/myDir"));
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30); 
IndexWriter writer = new IndexWriter(directory, analyzer, 
Query query =  new TermQuery(new Term("title", "hello"));

// Write document #1:
writer.addDocument(makeDoc("hello world 1")); // Field title="hello world 1"

// First search (yields document #1 as expected):
IndexReader, true);
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs results1 =, 10000);
printResults(searcher, results1);

// Write document #2:
writer.addDocument(makeDoc("hello world 2")); // Field title="hello world 2"

// Reopen reader, and search (should yield 2 documents, but I only see 1):
TopDocs results2 =, 10000);
printResults(searcher, results2);

--- On Thu, 1/13/11, Uwe Schindler <> wrote:

From: Uwe Schindler <>
Subject: RE: Newbie: "Life span" of IndexWriter / IndexSearcher?
Date: Thursday, January 13, 2011, 7:40 AM

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). 

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message