lucene索引的更新和删除
索引的删除:
IndexReader和IndexWriter都由删除索引的功能,但这两者是有区别的,
使用IndexReader删除索引时,索引会马上被删除,其有两种方法,可以删除索引deleteDocument(int docNum)和deleteDocument(Term term)前者会删除编号为docNum的document,后者会删除带有term的document。但是用这个类删除有局限性,当用此类打开的索引后来修改后会报StaleReaderException异常,并且当其他writer已经打开此索引时也会报异常。
IndexWriter可以根据多种情况进行删除deleteAll()删除所有的document、deleteDocuments(Query… queries)删除多个查询出来的document,deleteDocuments(Query query)删除query查询出来的document等等,但用Indexwriter执行删除的话一定要进行关闭,否则删除不会立马生效。
- Directory directory = FSDirectory.open(new File("d:\\lucene"));
- IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_34, new StandardAnalyzer(Version.LUCENE_34));
- //最大缓存文档数,控制写入一个新的segment前内存中保存的document的数目
- config.setMaxBufferedDocs(100);
- //控制一个segment中可以保存的最大document数目,值较大有利于追加索引的速度,默认Integer.MAX_VALUE,无需修改。
- config.setMaxBufferedDocs(Integer.MAX_VALUE);
- IndexWriter indexWriter = new IndexWriter(directory, config);
- indexWriter.deleteDocuments(new Term("id",index));
- //这两句一定要执行
- indexWriter.optimize();
- indexWriter.close();
索引的更新:
IndexWriter可以更新索引。
更新索引实际上是把要更新的document删除,然后把修改过的document加入到索引中,其方法是document(Term term,Document doc),第一个参数是删除含有term的document,第二个参数是更新后的document。
- Directory directory = FSDirectory.open(new File("d:\\lucene"));
- IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_34, new StandardAnalyzer(Version.LUCENE_34));
- //最大缓存文档数,控制写入一个新的segment前内存中保存的document的数目
- config.setMaxBufferedDocs(100);
- //控制一个segment中可以保存的最大document数目,值较大有利于追加索引的速度,默认Integer.MAX_VALUE,无需修改。
- config.setMaxBufferedDocs(Integer.MAX_VALUE);
- IndexWriter indexWriter = new IndexWriter(directory, config);
- Document doc = new Document();
- //设置Field的name是为了搜索时更精确,比如只想在title中搜索。
- Field idField = new Field("id", blog2.getId() + "", Field.Store.YES, Field.Index.NOT_ANALYZED);
- Field titleField = new Field("title", blog2.getTitle(), Field.Store.YES, Field.Index.ANALYZED);
- Field contentField = new Field("content", blog2.getContent(), Field.Store.NO, Field.Index.ANALYZED);
- Field dateField = new Field("date", blog2.getDate(), Field.Store.YES, Field.Index.NO);
- doc.add(titleField);
- doc.add(contentField);
- doc.add(idField);
- doc.add(dateField);
- indexWriter.updateDocument(new Term("id", blog2.getId() + ""), doc);
- //这两句一定要执行,不然的话,更新不会马上执行。
- indexWriter.optimize();
- indexWriter.close();
lucene索引的更新和删除的更多相关文章
- ELK学习总结(2-4)bulk 批量操作-实现多个文档的创建、索引、更新和删除
bulk 批量操作-实现多个文档的创建.索引.更新和删除 ----------------------------------------------------------------------- ...
- pymongo创建索引、更新、删除
pymongo创建索引.更新.删除 索引创建 ## collection 为数据集合collection.create_Index({'需创建索引字段': 1})collection.ensu ...
- Lucene教程(四) 索引的更新和删除
这篇文章是基于上一篇文章来写的,使用的是IndexUtil类,下面的例子不在贴出整个类的内容,只贴出具体的方法内容. 3.5版本: 先写了一个check()方法来查看索引文件的变化: /** ...
- Elasticsearch 索引、更新、删除文档
一.Elasticsearch 索引(新建)一个文档的命令: curl XPUT ' http://localhost:9200/test_es_order_index/test_es_order_t ...
- Lucene——索引的创建、删除、修改
package cn.tz.lucene; import java.io.File; import java.util.ArrayList; import java.util.List; import ...
- Lucene系列五:Lucene索引详解(IndexWriter详解、Document详解、索引更新)
一.IndexWriter详解 问题1:索引创建过程完成什么事? 分词.存储到反向索引中 1. 回顾Lucene架构图: 介绍我们编写的应用程序要完成数据的收集,再将数据以document的形式用lu ...
- Lucene 更新、删除、分页操作以及IndexWriter优化
更新操作如下: 注意:通过lukeall-1.0.0.jar 查看软件,我们可以看到,更新其实是先删除在插入, 前面我们知道索引库中有两部分的内容组成,一个是索引文件,另一个是目录文件, 目前我们更新 ...
- SQL Server索引进阶:第十三级,插入,更新,删除
在第十级到十二级中,我们看了索引的内部结构,以及改变结构造成的影响.在本文中,继续查看Insert,update,delete和merge造成的影响.首先,我们单独看一下这四个命令. 插入INSERT ...
- MongoDB的第二天(更新,删除,查询,索引)
Mongodb的更新方式有三种 update函数,操作符更新,save函数 update: 语法格式:db.COLLECTION_NAME.update({查询条件},{更新内容},{更新参数(可选) ...
随机推荐
- consul service
{ "name": "consul-agent (host:{{ .MONITOR_CONSUL }})", "command&q ...
- python's try&except&else
[python's try&except&else] python的try&catch有个好用的东西,else,即try&except&else可以共用,els ...
- IIS上架设https网站证书处理备忘
1. 免费SSL证书申请 https://www.startssl.com 教程:http://hxs.fd.fj.cn/?action=show&id=13 2. 证书转换 申请到的证书有两 ...
- 第七章 二叉搜索树 (a)概述
- UISearchDisplayController 阴影遮盖frame调整
iOS7中UISearchDisplayController 与UISearchBar结合使用时,有时候会出现搜索框获得焦点时,阴影遮盖部分挡住了搜索框,影响用户使用,如下图 API中没有阴影图层的接 ...
- [leetcode]121. Best Time to Buy and Sell Stock 最佳炒股时机
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- Orientation of phone Image
相机拍摄的图像方向问题 Description 很多时候,我们习惯把手机相机拍摄的图像在电脑上面查看.有的时候在手机上面看图像是正的,可是电脑端查看是反的:有的时候手机和电脑都是反的:有的时候都是正的 ...
- C语言日志处理
一.简介 zlog是一个高可靠性.高性能.线程安全.灵活.概念清晰的纯C日志函数库,在效率.功能.安全性上大大超过了log4c,并且是用c写成的,具有比较好的通用性. 二.安装 下载 https:// ...
- 安装python-empy
sudo python setup.py install
- nslookup域名查询命令查询ip
一.nslookup(name server lookup ) nslookup可以指定查询的类型,可以查到DNS记录的生存时间还可以指定使用哪个DNS服务器进行解释.在已安装TCP/IP协议的电脑上 ...