If you need to remove one or a class of documents from the index, Indexreader provides two methods:
Reader. deletedocument (int docnum)
Reader. Deletedocuments (term).
The former is to delete the document according to the number of the document, DocNum is the number of Lucene when the document enters the index, is in sequence, and the latter is to delete multiple documents that meet a certain condition.
After the Deletedocument or Deletedocuments method is executed, the system generates a *.del file that records the deleted document, but does not physically delete the document. Indexwriter.optimize (); only the actual deletion.
String indexdirstr = "D:\\index";D irectory indexdir = fsdirectory.getdirectory (new File (INDEXDIRSTR)); Indexreader Reader = Indexreader.open (indexdir); Reader.deletedocuments (new term ("id", string.valueof (ID))); Reader.flush (); Reader.close (); |
Note that the ID in New term () must be Field.Index.TOKENIZED, or it cannot be deleted if it cannot be searched.
Also note that a problem is that my lucene2.2 version will be deleted after the Indexreader.open error!! Replace with the new version of the lucene2.9 problem solved!
"Go" Delete Lucene index