Lucene——索引删改
来源:互联网 发布:服务器租赁 知乎 编辑:程序博客网 时间:2024/05/21 08:57
还是那两个java文件
IndexUtil.java
Reader就是一根输入的管道,Writer就是一个输出的管道
IndexWriter.deleteDocument()//可以删除Query也可以删除Term
IndexReader.undeleteAll()
IndexWriter.forceMergeDelere()
IdexWriter.updateDocument()
通过IndexReader.numDocs()、maxDoc()、numDeletedDocs()查看删除情况
package org.itat.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.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@aa.org","bb@bb.org","cc@cc.org","dd@@dd.org","ee@ee.org","ff@ff.org"};private String[] contents={"helloaa","helloboy","hellocc","hellodog","helloegg","hellofox"};private int[] attachs={1,2,3,4,5,6};private String[] names={"zhangsan","lisi","wangwu","zhaoliu","wuba","chenjiu"};private Directory directory=null;public IndexUtil(){try {directory=FSDirectory.open(new File("e:/lucene/index02"));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void query(){try {IndexReader reader=IndexReader.open(directory);System.out.println("numdocs:"+reader.numDocs());System.out.println("maxDocs:"+reader.maxDoc());System.out.println("detelemaxDocs:"+reader.numDeletedDocs());reader.close();} catch (CorruptIndexException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void index(){ IndexWriter writer=null; Document doc=null; try {// writer.deleteAll(); writer =new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));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("emails",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED)); doc.add(new Field("contents",contents[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) {// TODO Auto-generated catch blocke.printStackTrace();} catch (LockObtainFailedException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{ if(writer!=null) { try {writer.close();} catch (CorruptIndexException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} } }}//删除public void delete() { IndexWriter writer=null; try { writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35))); //参数是一个选项.可以是一个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) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } //恢复public void undelete() { try { //恢复时必须把reader的只读设为false IndexReader reader=IndexReader.open(directory,false); reader.undeleteAll(); reader.close(); } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block 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) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public void merge() { IndexWriter writer=null; try { writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35))); //将索引合并为两段,建议不实用,开销太大 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) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } //更新索引 public void update() { // lucene本身不支持更新 // 通过删除索引然后再建立索引来更新 IndexWriter writer=null; Document doc=null; try { writer =new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35))); doc=new Document(); doc.add(new Field("id","111",Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS)); doc.add(new Field("emails",emails[0],Field.Store.YES,Field.Index.NOT_ANALYZED)); doc.add(new Field("contents",contents[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) { // TODO Auto-generated catch block e.printStackTrace(); } catch (LockObtainFailedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ if(writer!=null) { try { writer.close(); } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
测试类
package org.itat.test;import org.itat.index.IndexUtil;import org.junit.Test;public class TestIndex {@Testpublic void testIndex(){IndexUtil index=new IndexUtil();index.index(); }@Testpublic void testQuery(){IndexUtil index=new IndexUtil();index.query();}@Testpublic void testDelete(){IndexUtil index=new IndexUtil();index.delete();}@Testpublic void testUnDelete(){IndexUtil index=new IndexUtil();index.undelete();}@Testpublic void testForceDelete(){IndexUtil index=new IndexUtil();index.forceDelete();}@Testpublic void testUpdate(){IndexUtil index=new IndexUtil();index.update();}}
- Lucene——索引删改
- Lucene对于索引的删改
- Lucene学习教程——Lucene索引创建
- Lucene学习教程——Lucene查找索引
- lucene学习——索引的建立
- Lucene——索引小练
- Lucene读书笔记——2. 构建索引
- 基于Java的全文索引/检索引擎——Lucene
- 基于Java的全文索引/检索引擎——Lucene
- Lucene.Net 2.3.1开发介绍 —— 阅读索引
- Lucene.Net 2.3.1开发介绍 —— 阅读索引
- 基于Java的全文索引/检索引擎——Lucene
- 基于Java的全文索引/检索引擎——Lucene
- Lucene.Net 2.3.1开发介绍 —— 阅读索引
- Lucene.Net 2.3.1开发介绍 —— 阅读索引
- Lucene小练三——索引删除,恢复,更新
- Lucene小练四——为数字和日期添加索引
- 一步一步学lucene——(第三步:索引篇)
- Struts2.3.7+Spring3.0.0+Hibernate4.0.0 整合(四)Spring 升级到 3.1.0
- Fedora17 开启ssh服务
- centos下subversion安装
- VC编译器设置
- Servlet 中文乱码问题及解决方案剖析
- Lucene——索引删改
- 粘一个在UVA和ZOJ都能AC但POJ WR的代码
- POJ1861 Network
- 高水位问题
- 4.安卓网页表单登陆初探
- Ubuntu下创建vim+Taglist+cscope+ctags组合编辑器
- ActionContext和ServletActionContext小结
- IOS 6 基础框架概览
- Vim中关于 cscope 的帮助