--- Otis Gospodnetic [EMAIL PROTECTED] wrote:
No, you can't add documents to an index once you close the IndexWriter.
You can re-open the IndexWriter and add more documents, of course.
Otis
That's what I expected at first, but:
1- It's a disappointment, because such a 'feature' would have made IndexeWriter
management much easier. You would open an IndexWriter at startup and reuse it
during all the life of the application, just flushing on a regular base using
the close() method and without worrying if other objects are currently using
the writer.
2- When you say you can't add, do you mean it's impossible or that you
shouldn't because for example it could corrupt the index?
Maybe I'm wrong, but I think it's possible. Let's look at the follwoing code:
public static void main(String[] args) throws IOException
{
final IndexWriter writer1 = new IndexWriter(/tmp/test-reuse, new
StandardAnalyzer(), true);
// First write with the writer
Document doc = new Document();
doc.add(new Field(name, John, Field.Store.YES,
Field.Index.UN_TOKENIZED));
writer1.addDocument(doc);
System.out.println(1 After first write, before closing the writer
---);
Searcher searcher = new IndexSearcher(/tmp/test-reuse);
Query query = new TermQuery(new Term(name, John));
Hits hits = searcher.search(query);
System.out.println(=== hits: + hits.length());
System.out.println();
// CLOSING THE WRITER ONCE
writer1.close();
System.out.println(2 After first write, after closing the writer
---);
searcher = new IndexSearcher(/tmp/test-reuse);
hits = searcher.search(query);
System.out.println(=== hits: + hits.length());
System.out.println();
// Second write, THE WRITER HAS ALREADY BEEN CLOSED ONCE
writer1.addDocument(doc);
System.out.println(3 After second write, the writer has been
closed once
---);
hits = searcher.search(query);
System.out.println(=== hits: + hits.length());
System.out.println();
// Closing the writer again
writer1.close();
System.out.println(4 After second write, the writer has been
closed
twice ---);
searcher = new IndexSearcher(/tmp/test-reuse);
hits = searcher.search(query);
System.out.println(=== hits: + hits.length());
}
== Results ==
1 After first write, before closing the writer ---
=== hits: 0
2 After first write, after closing the writer ---
=== hits: 1
3 After second write, the writer has been closed once ---
=== hits: 1
4 After second write, the writer has been closed twice ---
=== hits: 2
As your can see, not only does the code above execute without complain but it
also gives the right results.
Thanks for your comments.
__
Do you Yahoo!?
Yahoo! Mail - Easier than ever with enhanced search. Learn more.
http://info.mail.yahoo.com/mail_250
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]