博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lucene对index操作
阅读量:4031 次
发布时间:2019-05-24

本文共 5346 字,大约阅读时间需要 17 分钟。

package org.lucene.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 {
     String[] ids = { "aa", "bb", "cc", "dd", "ee", "ff", "gg" };
     String[] emails = { "aaa@lucenes.com", "bbb@lucenes.com",
             "ccc@lucenes.com", "ddd@lucenes.com", "eee@lucenes.com",
             "fff@lucenes.com", "ggg@lucenes.com" };
     String[] contents = { "aaaaaaaaaaaaaa", "bbbbbbbbbbbbbb",
             "ccccccccccccccc", "dddddddddddddd", "eeeeeeeeeeeeee",
             "fffffffffffffff", "gggggggggggggg" };
     String[] names = { "zhangsan", "lisi", "wangwu", "zhaoliu", "tianqi",
             "zhaoba", "chenjiu" };
     Integer[] attachs = { 1, 2, 5, 4, 6, 2, 3 };
 
     Directory directory = null;
 
     public IndexUtil() {
         try {
             directory = FSDirectory.open(new File(
                     "E:/luceneworkspace/Lucene2/path"));
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
 
     /**
      * 创建索引
      */
     public void index() {
         IndexWriter indexWriter = null;
         try {
             indexWriter = new IndexWriter(directory, new IndexWriterConfig(
                     Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
             indexWriter.deleteAll();
             Document document = null;
             for (int i = 0; i < ids.length; i++) {
                 document = new Document();
                 document.add(new Field("id", ids[i], Field.Store.YES,
                         Field.Index.NOT_ANALYZED_NO_NORMS));
                 document.add(new Field("email", emails[i], Field.Store.YES,
                         Field.Index.NOT_ANALYZED));
                 document.add(new Field("content", contents[i], Field.Store.NO,
                         Field.Index.ANALYZED));
                 document.add(new Field("name", names[i], Field.Store.YES,
                         Field.Index.NOT_ANALYZED));
                 indexWriter.addDocument(document);
             }
         } catch (CorruptIndexException e) {
             e.printStackTrace();
         } catch (LockObtainFailedException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
             try {
                 if (indexWriter != null)
                     indexWriter.close();
             } catch (CorruptIndexException e) {
                 e.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }
 
     /**
      * 删除索引
      */
     public void delete() {
         IndexWriter indexWriter = null;
         try {
             indexWriter = new IndexWriter(directory, new IndexWriterConfig(
                     Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
             indexWriter.deleteDocuments(new Term("id", "aa"));
         } catch (CorruptIndexException e) {
             e.printStackTrace();
         } catch (LockObtainFailedException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
             try {
                 if (indexWriter != null)
                     indexWriter.close();
             } catch (CorruptIndexException e) {
                 e.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }
 
     /**
      * 恢复索引
      */
     public void undelete() {
         IndexReader indexReader = null;
         try {
             indexReader = IndexReader.open(directory, false);
             indexReader.undeleteAll();
         } catch (CorruptIndexException e) {
             e.printStackTrace();
         } catch (LockObtainFailedException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
             try {
                 if (indexReader != null)
                     indexReader.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }
 
     /**
      *  清空回收站索引
      */
     public void clear(){
         IndexWriter indexWriter = null;
         try {
             indexWriter = new IndexWriter(directory, new IndexWriterConfig(
                     Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
             indexWriter.forceMergeDeletes();
         } catch (CorruptIndexException e) {
             e.printStackTrace();
         } catch (LockObtainFailedException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
             try {
                 if (indexWriter != null)
                     indexWriter.close();
             } catch (CorruptIndexException e) {
                 e.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }
     
     /**
      * 更新索引
      */
     public void update(){
         IndexWriter indexWriter = null;
         try {
             indexWriter = new IndexWriter(directory, new IndexWriterConfig(
                     Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
             Document doc=new Document();
             doc.add(new Field("id", "hh", Field.Store.YES,
                     Field.Index.NOT_ANALYZED_NO_NORMS));
             doc.add(new Field("email", emails[2], Field.Store.YES,
                     Field.Index.NOT_ANALYZED));
             doc.add(new Field("content", contents[3], Field.Store.NO,
                     Field.Index.ANALYZED));
             doc.add(new Field("name", names[4], Field.Store.YES,
                     Field.Index.NOT_ANALYZED));
             indexWriter.updateDocument(new Term("id","aa"), doc);
         } catch (CorruptIndexException e) {
             e.printStackTrace();
         } catch (LockObtainFailedException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
             try {
                 if (indexWriter != null)
                     indexWriter.close();
             } catch (CorruptIndexException e) {
                 e.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }
     
     public void query() {
         try {
             IndexReader indexReader = IndexReader.open(directory);
             System.out.println("maxDoc:" + indexReader.maxDoc());
             System.out.println("numDocs:" + indexReader.numDocs());
             System.out.println("numDeleteDocs:"+indexReader.numDeletedDocs());
         } catch (CorruptIndexException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
 
     }

 }

################################

转载地址:http://wiebi.baihongyu.com/

你可能感兴趣的文章
android中shader的使用
查看>>
java LinkedList与ArrayList迭代器遍历和for遍历对比
查看>>
Android DataBinding使用2-Recycleview
查看>>
drat中构造方法
查看>>
JavaScript的一些基础-数据类型
查看>>
JavaScript基础知识(2)
查看>>
转载一个webview开车指南以及实际项目中的使用
查看>>
关于activity保存页面状态的两个方法
查看>>
android中对于非属性动画的整理
查看>>
一个简单的TabLayout的使用
查看>>
关于let{a}=B出现的解构赋值
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
android给文字加边框(修改不能居中的问题)
查看>>
coursesa课程 Python 3 programming course_2_assessment_1
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
coursesa课程 Python 3 programming Dictionary methods 字典的方法
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>