SpringBoot+Lucene第六篇 — 索引的增删改查
为了减少代码量,写一个公用的增删改公用的获取IndexWriter
对象的方法
/**
* 增删改公用的获取IndexWriter对象
* @return
* @throws Exception
*/
public IndexWriter getIndexWriter() throws Exception{
//获得索引存放的位置
Directory directory = FSDirectory.open(new File("./index"));
//获得分词器
Analyzer analyzer = new IKAnalyzer();
//获得IndexWriterConfig对象
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_2, analyzer);
//获得indexWeiter对象
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
return indexWriter;
}
删除,修改索引
/**
* 删除索引——全部删除
* @throws Exception
*/
@Test
public void deleteAll() throws Exception{
//获得indexWeiter对象
IndexWriter indexWriter = this.getIndexWriter();
//删除所有的索引
indexWriter.deleteAll();
indexWriter.close();
}
/**
* 删除索引-按条件删除
*/
@Test
public void delete() throws Exception{
//获得indexWeiter对象
IndexWriter indexWriter = this.getIndexWriter();
Term t = new Term("fileName", "全文");
TermQuery query = new TermQuery(t);
//删除指定条件
indexWriter.deleteDocuments(query);
indexWriter.close();
}
/**
* 更新索引
* @throws Exception
*/
@Test
public void update() throws Exception{
//获得indexWeiter对象
IndexWriter indexWriter = this.getIndexWriter();
Document document = new Document();
document.add(new TextField("fileN", "测试文件名", Field.Store.YES));
document.add(new TextField("fileC", "测试文件内容", Field.Store.YES));
indexWriter.updateDocument(new Term("fileName", "全文"), document, new IKAnalyzer());
indexWriter.close();
}
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/05/04/springboot-lucene-chapter-6-adding-deleting-checking-indexes/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
THE END
0
二维码
打赏
海报
SpringBoot+Lucene第六篇 — 索引的增删改查
为了减少代码量,写一个公用的增删改公用的获取IndexWriter对象的方法
/**
* 增删改公用的获取IndexWriter对象
* @return
* @throws Exception
*/
public ……
文章目录
关闭
共有 0 条评论