2
votes

In the newest version of lucene, 4.6, the IndexReader.deleteDocuments() no longer exists. Someone told me I should use the IndexWriter instead. However, the API document shows that:

void deleteDocuments(Query... queries)

deletes the document(s) matching any of the provided queries,

void deleteDocuments(Query query)

deletes the document(s) matching the provided query,

void deleteDocuments(Term... terms)

deletes the document(s) containing any of the terms, and

void deleteDocuments(Term term)

deletes the document(s) containing term.

There is no method for deleting the docs by document ID.

1
Rewrite your code so you're working with your own domain keys (database primary key, etc) instead of the internal Lucene document ids. - sisve

1 Answers

3
votes

This is because Lucene doc IDs are ephemeral in IndexWriter, and can change at any moment (once a merge completes).

However, IndexWriter does have a tryDeleteDocument(), which takes a near-real-time IndexReader and an int docID. If possible (no merges have completed since that IndexReader was opened), the delete will "work" and the method returns true. If the method returns false, you must delete via Term or Query, or, re-open a new NRT reader and try the delete again.