lucene提供了两种删除索引的方式,一种是通过documentId删除某document文档,第二种是根据Term删除文档document.
对于第一种方式,在实际开发中使用很少,因为对于已经创建好的索引来说,我们很难确定某文档document的文档编号documentId是什么。
对于第二种方式,具体实例列举如下。
//按term删除文档
对于第一种方式,在实际开发中使用很少,因为对于已经创建好的索引来说,我们很难确定某文档document的文档编号documentId是什么。
对于第二种方式,具体实例列举如下。
//按term删除文档
public class MyDeleteIndexes {
public static final String STORE_PATH = "E:/lucene_index";
public static void deleteIndexes(String field , String keyword) throws IOException{
long startTime = System.currentTimeMillis();
Directory dir = FSDirectory.open(new File(STORE_PATH));
IndexReader reader = IndexReader.open(dir,false);
Term term = new Term(field,keyword);
reader.deleteDocuments(term);
//可以按documentId删除文档
//reader.deleteDocument(1);
reader.flush();
reader.close();
//System.out.println(reader.lastModified(dir));
long endTime = System.currentTimeMillis();
System.out.println("total time: " + (endTime - startTime) + " ms");
}
}