索引的删除: 
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执行删除的话一定要进行关闭,否则删除不会立马生效。

  1. Directory directory = FSDirectory.open(new File("d:\\lucene"));
  2. IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_34, new StandardAnalyzer(Version.LUCENE_34));
  3. //最大缓存文档数,控制写入一个新的segment前内存中保存的document的数目
  4. config.setMaxBufferedDocs(100);
  5. //控制一个segment中可以保存的最大document数目,值较大有利于追加索引的速度,默认Integer.MAX_VALUE,无需修改。
  6. config.setMaxBufferedDocs(Integer.MAX_VALUE);
  7. IndexWriter indexWriter = new IndexWriter(directory, config);
  8. indexWriter.deleteDocuments(new Term("id",index));
  9. //这两句一定要执行
  10. indexWriter.optimize();
  11. indexWriter.close();

索引的更新: 
IndexWriter可以更新索引。 
更新索引实际上是把要更新的document删除,然后把修改过的document加入到索引中,其方法是document(Term term,Document doc),第一个参数是删除含有term的document,第二个参数是更新后的document。

  1. Directory directory = FSDirectory.open(new File("d:\\lucene"));
  2. IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_34, new StandardAnalyzer(Version.LUCENE_34));
  3. //最大缓存文档数,控制写入一个新的segment前内存中保存的document的数目
  4. config.setMaxBufferedDocs(100);
  5. //控制一个segment中可以保存的最大document数目,值较大有利于追加索引的速度,默认Integer.MAX_VALUE,无需修改。
  6. config.setMaxBufferedDocs(Integer.MAX_VALUE);
  7. IndexWriter indexWriter = new IndexWriter(directory, config);
  8. Document doc = new Document();
  9. //设置Field的name是为了搜索时更精确,比如只想在title中搜索。
  10. Field idField = new Field("id", blog2.getId() + "", Field.Store.YES, Field.Index.NOT_ANALYZED);
  11. Field titleField = new Field("title", blog2.getTitle(), Field.Store.YES, Field.Index.ANALYZED);
  12. Field contentField = new Field("content", blog2.getContent(), Field.Store.NO, Field.Index.ANALYZED);
  13. Field dateField = new Field("date", blog2.getDate(), Field.Store.YES, Field.Index.NO);
  14. doc.add(titleField);
  15. doc.add(contentField);
  16. doc.add(idField);
  17. doc.add(dateField);
  18. indexWriter.updateDocument(new Term("id", blog2.getId() + ""), doc);
  19. //这两句一定要执行,不然的话,更新不会马上执行。
  20. indexWriter.optimize();
  21. indexWriter.close();
 

lucene索引的更新和删除的更多相关文章

  1. ELK学习总结(2-4)bulk 批量操作-实现多个文档的创建、索引、更新和删除

    bulk 批量操作-实现多个文档的创建.索引.更新和删除 ----------------------------------------------------------------------- ...

  2. pymongo创建索引、更新、删除

    pymongo创建索引.更新.删除     索引创建 ## collection 为数据集合collection.create_Index({'需创建索引字段': 1})collection.ensu ...

  3. Lucene教程(四) 索引的更新和删除

    这篇文章是基于上一篇文章来写的,使用的是IndexUtil类,下面的例子不在贴出整个类的内容,只贴出具体的方法内容. 3.5版本: 先写了一个check()方法来查看索引文件的变化:   /**   ...

  4. Elasticsearch 索引、更新、删除文档

    一.Elasticsearch 索引(新建)一个文档的命令: curl XPUT ' http://localhost:9200/test_es_order_index/test_es_order_t ...

  5. Lucene——索引的创建、删除、修改

    package cn.tz.lucene; import java.io.File; import java.util.ArrayList; import java.util.List; import ...

  6. Lucene系列五:Lucene索引详解(IndexWriter详解、Document详解、索引更新)

    一.IndexWriter详解 问题1:索引创建过程完成什么事? 分词.存储到反向索引中 1. 回顾Lucene架构图: 介绍我们编写的应用程序要完成数据的收集,再将数据以document的形式用lu ...

  7. Lucene 更新、删除、分页操作以及IndexWriter优化

    更新操作如下: 注意:通过lukeall-1.0.0.jar 查看软件,我们可以看到,更新其实是先删除在插入, 前面我们知道索引库中有两部分的内容组成,一个是索引文件,另一个是目录文件, 目前我们更新 ...

  8. SQL Server索引进阶:第十三级,插入,更新,删除

    在第十级到十二级中,我们看了索引的内部结构,以及改变结构造成的影响.在本文中,继续查看Insert,update,delete和merge造成的影响.首先,我们单独看一下这四个命令. 插入INSERT ...

  9. MongoDB的第二天(更新,删除,查询,索引)

    Mongodb的更新方式有三种 update函数,操作符更新,save函数 update: 语法格式:db.COLLECTION_NAME.update({查询条件},{更新内容},{更新参数(可选) ...

随机推荐

  1. [ML]熵、KL散度、信息增益、互信息-学习笔记

    [ML]熵.KL散度.信息增益.互信息-学习笔记 https://segmentfault.com/a/1190000000641079

  2. python 字符串报错问题

    http://jingyan.baidu.com/article/25648fc1a96dd49191fd00c0.html 解决'ascii' codec can't encode characte ...

  3. docker 网络配置路由转发

    建好flannel 网络之后 iptables -L -n 查看 要全是accept iptables -P FORWARD ACCEPT 开启路由转发 修改/etc/sysctl.conf文件,添加 ...

  4. Java常用的输出调试技巧

    --------siwuxie095                 Eclipse 开发中常用的输出调试技巧:     先在左侧的 Package Explorer,右键->New->J ...

  5. Jenkins使用FTP进行一键部署及回滚2(Windows)(项目实践)

     转载:http://www.cnblogs.com/EasonJim/p/6295372.html Jenkins使用FTP进行一键部署及回滚2(Windows)(项目实践) 前提: 这一篇是继上一 ...

  6. Luogu 4602 [CTSC2018]混合果汁

    BZOJ 5343 福利题. 对于每一个询问可以二分$d$,然后把满足条件的果汁按照$p$从小到大排序贪心地取$L$升看看满不满足价格的条件. 那么按照$p$建立权值主席树,$chk$的时候在主席树上 ...

  7. web端跨域调用webapi(转)

    在做Web开发中,常常会遇到跨域的问题,到目前为止,已经有非常多的跨域解决方案. 通过自己的研究以及在网上看了一些大神的博客,写了一个Demo 首先新建一个webapi的程序,如下图所示: 由于微软已 ...

  8. jquery.fn.extend() 与 $.jquery 作用及区别

    原文:http://www.cnblogs.com/liu-l/p/3928373.html jQuery.extend()这个方法,主要是用来拓展个全局函数啦,例如$.ajax()这种,要不就是拓展 ...

  9. 三大语言实例 (python,C/C++,Java)

    Python3.5语言实例: #coding = utf-8 import sys def Sub_string(a,b): c=[0]*len(b) for i in range(len(a)): ...

  10. 安装Android SDK Manager的“Failed to fetch refused”问题解决方法

    安装Android SDK Manager的"Failed to fetch refused"问题解决方法 一见 2014/11/11 问题现象: 步骤一:修改hosts文件(wi ...