I want to delete a document in lucene 2.4 with java. My code is
Directory directory = FSDirectory.getDirectory("c:/index");
IndexReader indexReader = IndexReader.open(directory);
System.out.println("num="+indexReader.maxDoc());
indexReader.deleteDocuments(new Term("name","1"));
System.out.println("num="+indexReader.maxDoc());
output
num=1
num=1
In my opinion it is best to use Indexwriter to delete the documents, since Indexreader buffers the deletions and does not write changes to the index until close() is called on.; unless you use the same reference for search.
The Lucene wiki states
I can see you want the maxdoc value for the document in memory so its a better approach to use Indexwriter
so the answer for your question is
you should close the Indexreader object or use Indexwriter for deletions