public class UpdateIndex {
private IndexWriter indexWriter;
@Before
public void init() throws Exception {
indexWriter = new IndexWriter(FSDirectory.open(
new File("F:\\学习文档\\框架10 lucene\\index").toPath()),
new IndexWriterConfig(new IKAnalyzer()));
}
@After
public void end() throws Exception {
if (indexWriter!=null){
indexWriter.close();// 关闭索引库
}
}
//添加索引(添加文档代码实现)
@Test
public void addDocument() throws Exception {
Document document = new Document();
document.add(new TextField("name", "香港", Field.Store.YES));
document.add(new TextField("content","香港爱国的好青年啊!", Field.Store.NO));
document.add(new StoredField("path", "F:/index"));
indexWriter.addDocument(document);
}
//删除所有索引
@Test
public void deleteAllDocument() throws Exception{
indexWriter.deleteAll();
}
//删除指定域指定的索引
@Test
public void deleteDocument() throws Exception {
indexWriter.deleteDocuments(new Term("name", "香港"));
}
// 修改的原理是先删除后添加
// 通俗点说就是:只会保留一个
@Test
public void updateDocument() throws Exception {
Document document = new Document();
document.add(new TextField("name", "香港爱国主义好青年", Field.Store.YES));
document.add(new TextField("content", "香港?", Field.Store.YES));
document.add(new TextField("names", "香港万岁!", Field.Store.YES));
indexWriter.updateDocument(new Term("name", "香港"), document);
}
}
Lucene8.2 索引增删改
最新推荐文章于 2021-07-30 19:54:25 发布