Lucene索引操作,删除,更新,优化
来源:互联网 发布:p2p摄像头监控软件 编辑:程序博客网 时间:2024/05/23 21:58
- IndexUtil.java
- 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 = {"soukenan@qq.com","li@soukenan.com","804632564@qq.com","admin@qq.com","soukenan@kenan.org","123@df.com"};
- private String[] content ={
- "Welcome to my home",
- "Hello Kenan",
- "Good morning",
- "Are you OK?",
- "Yeah hahahahahahaha",
- "I like foot ball"
- };
- private int[] attachs = {1,4,6,2,3,8};
- private String[] names = {"zhangsan","kenan","soukenan","Micheal","Liangchengpeng","Jah"};
- //词典
- private Directory directory = null;
- //写入笔
- private IndexWriter writer = null;
- //文档对象
- private Document doc = null;
- //读取对象
- private IndexReader reader = null;
- public IndexUtil(){
- try {
- this.directory = FSDirectory.open(new File("d:/lucene/index02"));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * 构建索引
- */
- public void buildIndex(){
- try {
- writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_36)));
- for(int i=0;i<6;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",this.content[i],Field.Store.NO,Field.Index.ANALYZED));
- doc.add(new Field("name",this.names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
- writer.addDocument(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();
- }
- }
- }
- }
- /**
- * 查询文档信息
- */
- public void query(){
- try {
- this.reader = IndexReader.open(directory);
- System.out.println("maxDoc:"+reader.maxDoc());
- System.out.println("numDocs:"+reader.numDocs());
- System.out.println("delDocs:"+reader.numDeletedDocs());
- } catch (CorruptIndexException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * 删除文档
- */
- public void deleteIndex(){
- try {
- writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
- // writer.deleteAll();//删除所有索引
- writer.deleteDocuments(new Term("id","1")); //删除到回收站
- //删除id索引为1的记录
- //也可以传一个query对象
- // writer.deleteDocuments(query);
- } 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();
- }
- }
- }
- }
- /**
- * 恢复删除文档
- */
- public void unDeleteIndex(){
- try {
- reader = IndexReader.open(directory,false);
- reader.undeleteAll();//回复所有被删除的文档
- } catch (CorruptIndexException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- if(reader!=null){
- try {
- reader.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- public void forceDelete(){
- try {
- writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
- writer.forceMergeDeletes();//删除回收站中的内容
- } 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();
- }
- }
- }
- }
- /**
- * 文档索引更新
- */
- public void merge(){
- try {
- writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
- //消耗资源,不建议使用
- writer.forceMerge(2);
- } 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();
- }
- }
- }
- }
- /**
- * 更新文档
- */
- public void update(){
- try {
- writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_36)));
- doc = new Document();
- doc.add(new Field ("id","10",Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
- doc.add(new Field("email","email",Field.Store.YES,Field.Index.NOT_ANALYZED));
- doc.add(new Field("content","content",Field.Store.NO,Field.Index.ANALYZED));
- doc.add(new Field("name","names",Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
- writer.updateDocument(new Term("id","3"), 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();
- }
- }
- }
- }
- }
- 测试类TestCase.java
- import static org.junit.Assert.*;
- import org.junit.BeforeClass;
- import org.junit.Test;
- public class TestCase {
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- }
- @Test
- public void testBuildIndex() {
- new IndexUtil().buildIndex();
- }
- @Test
- public void testQuery(){
- new IndexUtil().query();
- }
- @Test
- public void testDeleteIndex(){
- new IndexUtil().deleteIndex();
- }
- @Test
- public void testUnDeleteIndex(){
- new IndexUtil().unDeleteIndex();
- }
- @Test
- public void testForceDelete(){
- new IndexUtil().forceDelete();
- }
- @Test
- public void testMerge(){
- new IndexUtil().merge();
- }
- @Test
- public void testUpdate(){
- new IndexUtil().update();
- }
- }
本文出自 “Kenan_ITBlog” 博客,请务必保留此出处http://soukenan.blog.51cto.com/5130995/1120058
- Lucene索引操作,删除,更新,优化
- Lucene索引删除、更新、恢复和加权操作
- Lucene学习二:封装操作接口,实现删除、更新索引
- lucene索引删除,恢复,更新
- lucene索引删除,恢复,更新
- lucene 新增 更新 删除索引
- lucene索引的更新和删除
- lucene索引的删除和更新
- Lucene创建、查询、删除、更新 索引
- lucene-索引文档的删除、更新及增强加权
- Lucene小练三——索引删除,恢复,更新
- 第二章:lucene构建索引(新增、查询、更新、删除)
- Lucene教程(四) 索引的更新和删除
- lucene索引_创建_域选项和lucene索引_的删除、更新.
- 3、学习lucene之索引的更新和优化
- lucene更新索引失败
- lucene中的删除索引
- lucene 删除索引
- WebRTC实现很难?看Mozilla如何做
- ibatis学习笔记
- 使用JFreeChart生成柱状图和饼图
- Lucene介绍和创建索引和搜索初步
- Lucene索引创建之域选项介绍,Field.Store和Fiele.Index
- Lucene索引操作,删除,更新,优化
- 归并排序—自底向上和自顶向下
- lucene加权操作和luke的简单演示
- lucene对数字和日期类型索引的创建
- IndexWriter和IndexReader对象分析
- 如何更好的玩转移动广告平台
- lucene的搜索,TermQuery,TermRangeQuery,NumericRangeQuery
- lucene中搜索
- lucene,基于QueryParser的搜索