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({查询条件},{更新内容},{更新参数(可选) ...
随机推荐
- 理解C# 4 dynamic(4) – 让人惊艳的Clay(转)
作者:Justrun名字来自<阿甘正传>,是希望自己能够更更傻一点. link: http://www.cnblogs.com/JustRun1983/p/3529157.html 理 ...
- artZoom 图片可放大旋转
资源下载:http://www.sucaijiayuan.com/Js/TuPianDaiMa/774.html 第一步:引入文件 <link rel="stylesheet" ...
- php7源码编译安装
以下以CentOS 7.2为例,安装php的运行环境,首先打开php官网http://php.net/点击导航栏的Downloads进入下载页面:http://php.net/downloads.ph ...
- 电话号码的字母组合 · Letter Combinations of a Phone Number
[抄题]: Given a digit string excluded 01, return all possible letter combinations that the number coul ...
- Vue.js基础知识
<!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml&q ...
- Solr分词搜索结果不准确
Solr的schema.xml默认配置分词后条件取 OR 例如:大众1.6T 系统会自动分词为 [大众] [1.6T](ps:不同分词器分词效果不同) 会搜索出包含 [大众 OR 1.6T] ...
- python进行数据清理之pandas中的drop用法
好久好久没有更新博客了,之前自学的估计也都忘记差不多了.由于毕业选择从事的行业与自己的兴趣爱好完全两条路,心情也难过了很久,既然入职了就要好好干,仍要保持自己的兴趣,利用业余时间重拾之前的乐趣. 从基 ...
- JScript 对象 <|> JSON
<script type="text/javascript"> function test(){ var array = [{"id":1},{&q ...
- iOS9 视频播放
self.videoFileURL = [NSURL URLWithString:[NSString stringWithFormat:@"file:///%@", self ...
- 设置手机iphone5s邮件
由于更新系统后,手机自带的邮件服务器老是报错,后来查一下,需要设置qq邮箱独立密码,http://jingyan.baidu.com/article/c146541354cefb0bfdfc4c5d. ...