I had a following problem:
I filled index like this:
iw = new IndexWriter(index, new SimpleAnalyzer(), false);
iw.addDocument(assetToDoc(asset)); // assetToDoc return Document
instance
iw.close();
and deleted one document as follows:
ir = IndexReader.open(index);
Term uidTerm = new Term("uid", assetUid); // uid is primary
key
int count = ir.delete(uidTerm);
ir.close();
When I next wanted to print index's content:
IndexReader ir = IndexReader.open(indexPath);
for (int i=0; i<ir.maxDoc(); i++) {
System.out.println(i);
Document doc = ir.document(i);
Enumeration fields = doc.fields();
while (fields.hasMoreElements()) {
System.out.println(fields.nextElement());
}
System.out.println();
}
the ir.maxDoc() went beyond true number of docs in index and I got stucked
with Exception telling me that I'm trying to access deleted document.
Now I always, after deleting a document, open an IndexWriter and call
optimize() and close it and it works okay then, the index doesn't get
currupted anymore. But I haven't noticed anywhere that this is a standard
procedure after deleting a document, so am I doing something wrong?
Has anyone experienced something similiar? If true, please let me know how
did you solve it.
Happy y2k+2