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({查询条件},{更新内容},{更新参数(可选) ...
随机推荐
- Redis搭建(七):Redis的Cluster集群动态增删节点
一.引言 上一篇文章我们一步一步的教大家搭建了Redis的Cluster集群环境,形成了3个主节点和3个从节点的Cluster的环境.当然,大家可以使用 Cluster info 命令查看Cluste ...
- js练习 closure
window.onload = function() { for (var i = 1; i < 4; i++) { var id = doc ...
- set 续4
---------siwuxie095 批处理简易计算器 @echo off ::设置窗口标题 title DOS 简易计算器 ::设置窗口大小 ::设置窗口颜色 co ...
- Golang之匿名函数和闭包
Go语言支持匿名函数,即函数可以像普通变量一样被传递或使用. 使用方法如下: main.go package main import ( "fmt" ) func main() { ...
- Oracle GoldenGate 一、介绍和安装
1 说明 1.1 下载和文档说明 下载地址:http://www.oracle.com/technetwork/middleware/goldengate/downloads/index.html 我 ...
- [.NET] WeakReference的使用
声明:本篇博客翻译自:http://tipsandtricks.runicsoft.com/CSharp/WeakReferences.html 由于水平(技术水平+英语理解能力)有限/不足,肯定会有 ...
- cucumber安装步骤
#Start Guide##Environment###1. Install Ruby Verify your installation by running ruby -v in a termina ...
- JavaScript 语法总结3
1. 数组初始化可以跳着来 var s = [1,2,,,,6]; // 中间省略的元素为undefined 2. 函数定义表达式: var f = function(args){ return ...
- .NET平台机器学习资源汇总,有你想要的么?(转)
出处:http://www.cnblogs.com/asxinyu/p/4422050.html 阅读目录 1.开源综合类 2.开源.NET平台非综合类 3.其他资源与技术博客 4.我的100篇博客之 ...
- mvc api odata查询选项之 $inlinecount $format 选项(转)
出处:http://www.it165.net/pro/html/201505/40236.html 网上百度“odata 语法”会出来很多结果,其中有一项是比较一致的,那就是odata支持一下几种语 ...