Lucene索引的删除和更新
删除和更新和新增一样,也是通过IndexWriter 对象来操作的,IndexWrite对象的deleteDocuments ()方法用于实现索引的删除,updateDocument()方法用于实现索引的更新。
删除Lucene索引
删除索引的代码如下,该示例实现了根据Term来删除单个或多个Document,删除title中包含关键词“美国”的文档:
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import tup.lucene.ik.IKAnalyzer6x;
public class DeleteIndex {
public static void main(String[] args) {
// 删除title中含有关键词“美国”的文档
deleteDoc("title", "美国");
}
public static void deleteDoc(String field, String key) {
Analyzer analyzer = new IKAnalyzer6x();
IndexWriterConfig icw = new IndexWriterConfig(analyzer);
Path indexPath = Paths.get("indexdir");
Directory directory;
try {
directory = FSDirectory.open(indexPath);
IndexWriter indexWriter = new IndexWriter(directory, icw);
indexWriter.deleteDocuments(new Term(field, key));
indexWriter.commit();
indexWriter.close();
System.out.println("删除完成!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
除此之外IndexWriter还提供了以下方法:
- DeleteDocuments(Query query):根据Query条件来删除单个或多个Document
- DeleteDocuments(Query[] queries):根据Query条件来删除单个或多个Document
- DeleteDocuments(Term term):根据Term来删除单个或多个Document
- DeleteDocuments(Term[] terms):根据Term来删除单个或多个Document
- DeleteAll():删除所有的Document
更新Lucene索引
使用IndexWriter进行Document删除操作时,文档并不会立即被删除,而是把这个删除动作缓存起来,当IndexWriter.Commit()或IndexWriter.Close()时,删除操作才会被真正执行。
索引更新操作实质上是先删除索引,再重新建立新的文档,示例代码如下:
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
mport org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import tup.lucene.ik.IKAnalyzer6x;
public class UpdateIndex {
public static void main(String[] args) {
Analyzer analyzer = new IKAnalyzer6x();
IndexWriterConfig icw = new IndexWriterConfig(analyzer);
Path indexPath = Paths.get("indexdir");
Directory directory;
try {
directory = FSDirectory.open(indexPath);
IndexWriter indexWriter = new IndexWriter(directory, icw);
Document doc = new Document();
doc.add(new TextField("id","2", Store.YES));
doc.add(new TextField("title", " 北京大学开学迎来4380名新生",
Store.YES));
doc.add(new TextField("content", " 昨天,北京大学迎来4380名来自全
国各地及数十个国家的本科新生。其中,农村学生共700余名,为近年最
多...", Store.YES));
indexWriter.updateDocument(new Term("title", "北大"), doc);
indexWriter.commit();
indexWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
上面的代码中我们新建了一个IndexWriter对象和Document对象,通过updateDocument()方法完成更新操作。Term对象用于定位文档,查找title中含有“北大”的文档,然后用新的文档替换原文档,这样就完成了索引的更新操作。