lucene索引的删除和更新

发布时间:2024-11-24 15:37

定期回顾和更新清单,删除已完成事项 #生活常识# #时间管理建议# #任务清单制定#

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(); } } }

1234567891011121314151617181920212223242526272829303132

除此之外IndexWriter还提供了以下方法:

DeleteDocuments(Query query):根据Query条件来删除单个或多个DocumentDeleteDocuments(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(); } } }

123456789101112131415161718192021222324252627282930313233343536

上面的代码中我们新建了一个IndexWriter对象和Document对象,通过updateDocument()方法完成更新操作。Term对象用于定位文档,查找title中含有“北大”的文档,然后用新的文档替换原文档,这样就完成了索引的更新操作。

网址:lucene索引的删除和更新 https://www.yuejiaxmz.com/news/view/237943

相关内容

win7更新的补丁文件怎么删除
SQL Server大表如何快速删除数据
Windows下快速删除上万个文件和子目录(快速删除文件) 命令行cmd快速删除文件夹
Oracle 大表数据删除/清理方法小结
WPS表格高效操作:快速删除多余行和列的技巧
电脑怎么快速删除重复文件?系统自带工具和第三方软件,汇总了!
小红书新增关注记录怎么删除 新增关注记录列表删除教程
PyMySQL的使用:事务、索引、如何防止SQL注入
WPS Word文档空白页删除指南:快速清除技巧
【anaconda】conda创建、查看、删除虚拟环境(anaconda命令集)

随便看看