To delete a document on Lucene, there is the function indexWriter.deleteDocuments(…) with Query and/or Terms.
That works fine.
However, I’d need to browse a collection of documents, and delete some of them based on a condition.
I could add a unique id field in the Document, and call indexWriter.deleteDocuments(…) based on this unique id.
However, I’d like to avoid this option, and do something like this instead:
TopDocs hits = indexSearcher.search(...);
for (ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = indexSearcher.doc(scoreDoc.doc);
if (...) {
indexWriter.delete(doc); // or similar
}
}
Is it possible to do that?
ps: Again, I am aware that I could add a unique id field in the document,
and delete the document inside the loop by calling indexWriter.deleteDocuments(…) based on this unique id.
That would work fine. However, I am asking if it is possible to do it without this option.
Please do not answer about whether this is or is not the right approach. It is an interesting discussion, but it is not the objective of this post.
Please answer only about if it is possible (and how) or not to do without adding a unique id.
pss: I know I am repeating myself, but I’ll tell it again, because I fear that I will get answers not related to the objective of this post.
really, please focus only about if it is possible (and how) or not to do without adding a unique id.
You can come at it from the
IndexReaderinstead.It won’t work if you have an
IndexWriteralready open on theDirectory, though, so perhaps it’s not applicable to your case.