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({查询条件},{更新内容},{更新参数(可选) ...
随机推荐
- mysql5.7 初始化启动
root@0f6852dfee81:/# mysql --versionmysql Ver 14.14 Distrib 5.7.18-16, for debian-linux-gnu (x86_64 ...
- How To Use Google Flags
[How To Use Google Flags] 1.Commandline flags are flags that users specify on the command line when ...
- java web作用域page request session application
转载自:http://blog.csdn.net/wyd458549392147/article/details/6944481 1.page指当前页面.只在一个jsp页面里有效 . 2.reques ...
- Template7学习记录
来源:http://idangero.us/template7/#.V2iXqJGF6Ul 测试用json数据: var jsonData = { people: [ { firstName: 'Jo ...
- React Native开源项目案例
(六).React Native开源项目: 1.Pober Wong_17童鞋为gank.io做的纯React Native项目,开源地址:https://github.com/Bob1993/Rea ...
- Centos 7.2编译安装MariaDB-10.0.xx
系统: centos7.2 x64数据库:MariaDB-10.0.30 使用jemalloc对MySQL内存进行优化. 软件包下载地址:http://pan.baidu.com/s/1eS44OKU ...
- redis集群种类(转)
原文:http://blog.csdn.net/c295477887/article/details/52487621 关于redis主从.哨兵.集群的介绍网上很多,这里就不赘述了. 一.主从 通过持 ...
- 《Forward团队-爬虫豆瓣top250项目-开发文档》
码云地址:https://github.com/xyhcq/top250 模块功能:获取豆瓣top250网页的源代码,并分析. def getHTMLText(url,k): # 获取网页源代码 tr ...
- Hdu1429 胜利大逃亡(续) 2017-01-20 18:33 53人阅读 评论(0) 收藏
胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Subm ...
- spring获取webapplicationcontext,applicationcontext几种方法详解(转载)
转载自 http://www.blogjava.net/Todd/archive/2010/04/22/295112.html 方法一:在初始化时保存ApplicationContext对象 代码: ...