【删除】

【恢复删除】

【强制删除】

【优化和合并】

【更新索引】

附:

代码:

IndexUtil.java:

 package cn.hk.index;

 import java.io.File;
import java.io.IOException; import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.StaleReaderException;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version; public class IndexUtil {
private String[] ids = {"1","2","3","4","5","6"};
private String[] emails = {"aa@hk.arg","bb@hk.org","cc@hk.arg",
"dd@hk.org","ee@hk.org","ff@hk.org"};
private String[] content = {
"welcome to visited the space","hello boy","my name is aa","i like football",
"I like football and I like Basketball too","I like movie and swim"
};
private int[] attachs = {2,3,1,4,5,5};
private String[] names = {"zhangsan","lisi","john","mike","jetty","jake"}; private Directory directory = null; public IndexUtil(){
try {
directory = FSDirectory.open(new File("d://lucene/index02"));
} catch (IOException e) {
e.printStackTrace();
}
} public void update(){
IndexWriter writer =null;
try {
writer = new IndexWriter(directory,
new IndexWriterConfig(Version.LUCENE_35,new StandardAnalyzer(Version.LUCENE_35)));
/*
* lucene并没有提供更新的方法,这里的更新其实是提供如下两个操作:
* 先删除之后再添加
*/
Document doc = new Document();
doc.add(new Field("id","11",Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("email",emails[0],Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("content",content[0],Field.Store.NO,Field.Index.ANALYZED));
doc.add(new Field("name",names[0],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
writer.updateDocument(new Term("id","1"),doc);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(writer != null)
try {
writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} } public void merge(){
IndexWriter writer = null;
try {
writer = new IndexWriter(directory,
new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
//会将索引合并为2段,这两段中的被删除的数据会被清空
//特别注意:此处在lucene3.5后不建议使用,因为会消耗大量的开销,
//lucene会根据情况自动处理的
writer.forceMerge(2);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(writer != null)
try {
writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} public void forceDelete(){
IndexWriter writer = null;
try {
writer = new IndexWriter(directory,
new IndexWriterConfig(Version.LUCENE_35,new StandardAnalyzer(Version.LUCENE_35)));
writer.forceMergeDeletes();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(writer != null)
try {
writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} public void undelete(){
//使用IndexReader进行恢复
try {
IndexReader reader = IndexReader.open(directory,false);
//回复时,必须把IndexReader的只读(readyonly)设置为FALSE
reader.undeleteAll();
reader.close();
} catch (StaleReaderException e) { e.printStackTrace();
} catch (CorruptIndexException e) { e.printStackTrace();
} catch (LockObtainFailedException e) { e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void delete(){
IndexWriter writer = null;
try {
writer = new IndexWriter(directory,
new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
//删除ID为1的文档
//参数可以是一个选项,可以是一个Query,也可以是一个Term,Term是一个精确查找的值
//此时删除的文档并不会被完全删除,而是存储在回收站中的,可以恢复
writer.deleteDocuments(new Term("id","1"));
} catch (CorruptIndexException e) { e.printStackTrace();
} catch (LockObtainFailedException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
}finally{
if(writer != null)
try {
writer.close();
} catch (CorruptIndexException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
}
}
} public void query(){
try {
IndexReader reader = IndexReader.open(directory);
//通过reader可以获取文档的数量
System.out.println("numDocs:" + reader.numDocs());
System.out.println("maxDocs" + reader.maxDoc());
System.out.println("deleteDocs:" + reader.numDeletedDocs());
reader.close();
} catch (CorruptIndexException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
}
} public void index(){
IndexWriter writer = null;
try {
writer = new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
Document doc = null;
for(int i=0;i<ids.length;i++){
doc = new Document();
doc.add(new Field("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("email",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("content",content[i],Field.Store.NO,Field.Index.ANALYZED));
doc.add(new Field("name",names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
writer.addDocument(doc);
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(writer != null)
try {
writer.close();
} catch (CorruptIndexException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
} }
} }

TestIndex.java:

 package cn.hk.test;

 import org.junit.Test;

 import cn.hk.index.IndexUtil;

 public class TestIndex {

     @Test
public void testIndex(){
IndexUtil iu = new IndexUtil();
iu.index();
} @Test
public void testQuery(){
IndexUtil iu = new IndexUtil();
iu.query();
} @Test
public void testDelete(){
IndexUtil iu = new IndexUtil();
iu.delete();
} @Test
public void testUnDelete(){
IndexUtil iu = new IndexUtil();
iu.undelete();
} @Test
public void testForceDelete(){
IndexUtil iu = new IndexUtil();
iu.forceDelete();
} public void testMerge(){
IndexUtil iu = new IndexUtil();
iu.merge();
} @Test
public void testUpdate(){
IndexUtil iu = new IndexUtil();
iu.update();
}
}

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

  1. *lucene索引_创建_域选项

    [索引建立步骤] [创建Directory] [创建writer] [创建文档并添加索引] 文档和域的概念很重要 文档相当于表中的每一条记录,域相当于表中的每一个字段. [查询索引的基本信息] 使用I ...

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

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

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

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

  4. Lucene索引维护(添加、修改、删除)

    1. Field域属性分类 添加文档的时候,我们文档当中包含多个域,那么域的类型是我们自定义的,上个案例使用的TextField域,那么这个域他会自动分词,然后存储            我们要根据数 ...

  5. Lucene——索引过程分析Index

    Lucene索引过程分为3个主要操作步骤:将原始文档转换成文本.分析文本.将分析好的文本保存至索引中 一.提取文本和创建文档 从 pdf.word等非纯文本格式文件中,提取文本格式信息.建立起对应的, ...

  6. 【手把手教你全文检索】Lucene索引的【增、删、改、查】

    前言 搞检索的,应该多少都会了解Lucene一些,它开源而且简单上手,官方API足够编写些小DEMO.并且根据倒排索引,实现快速检索.本文就简单的实现增量添加索引,删除索引,通过关键字查询,以及更新索 ...

  7. 理解Lucene索引与搜索过程中的核心类

    理解索引过程中的核心类 执行简单索引的时候需要用的类有: IndexWriter.ƒDirectory.ƒAnalyzer.ƒDocument.ƒField 1.IndexWriter IndexWr ...

  8. lucene索引

    一.lucene索引 1.文档层次结构 索引(Index):一个索引放在一个文件夹中: 段(Segment):一个索引中可以有很多段,段与段之间是独立的,添加新的文档可能产生新段,不同的段可以合并成一 ...

  9. Lucene 索引功能

    Lucene 数据建模 基本概念 文档(doc): 文档是 Lucene 索引和搜索的原子单元,文档是一个包含多个域的容器. 域(field): 域包含“真正的”被搜索的内容,每一个域都有一个标识名称 ...

随机推荐

  1. python实现计数排序

    计数排序有局限性,最小值和最大值决定着数组的长度,如果分配均匀的话可以用 # @File: count_sort import random def count_sort(li, max_num=10 ...

  2. sql 语句操作,修改字段中字符串的一部分

    update 表名 set 字段=replace(字段,‘替换的部分’,‘替换后的字符串’): update 表名 set A=replace( A, '海淀', '朝阳') where A like ...

  3. javascript简单的表单验证

    <html> <head> <title>用户登录</title> <script language="javascript" ...

  4. 数位dp知识

    转自http://blog.csdn.net/zhaoxinfan/article/details/8707605 下面先给出数位DP的背景: •在给定区间[A,B]内,找满足要求的数. •要求一般和 ...

  5. 【C#】基础之数组排序,对象大小比较(对比器)

    C#基础之数组排序,对象大小比较 原文链接:[OutOfMemory.CN] 从个小例子开始: 1 2 3 int[] intArray = new int[]{2,3,6,1,4,5}; Array ...

  6. 关于c#的结构体struct与class的区别

    C# 结构体 struct C#中结构类型和类类型在语法上非常相似,他们都是一种数据结构,都可以包括数据成员和方法成员. 结构和类的区别: 1.结构是值类型,它在栈中分配空间:而类是引用类型,它在堆中 ...

  7. 设计模式(3)-- 原型模式 (clone分析)

    原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型来创建对象. 在java中有语言级别的支持:clone 在java中使用原型模式是非常简单的事情,实现Cloneable接口,调用Objec ...

  8. CF779B(round 402 div.2 B) Weird Rounding

    题意: Polycarp is crazy about round numbers. He especially likes the numbers divisible by 10k. In the ...

  9. 使用windows的fsutil命令创建指定大小及类型的测试文件

    在软件测试中,对于上传.下载一类功能常常需要用不同大小的文件进行测试. 使用Windows命令fsutil可以生成任意大小.任意类型文件. C:\Users\axia\fsutil file crea ...

  10. 原生js的容易忽略的相似点(一)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...