zoukankan      html  css  js  c++  java
  • lucene(全文搜索)_恢复/更新索引操作

    项目结构大家可以先看看:lucene(全文搜索)_根据内容建立索引_源码下载 

    索引的恢复/更新操作

     1 /**
     2      * 把删除的索引进行恢复操作
     3      */
     4     public void recover() {
     5         IndexReader reader = null;
     6         try {
     7             // readOnly默认为true,要把readOnly设置为false
     8             reader = IndexReader.open(directory, false);
     9             reader.undeleteAll();
    10         } catch (CorruptIndexException e) {
    11             e.printStackTrace();
    12         } catch (IOException e) {
    13             e.printStackTrace();
    14         } finally {
    15             if (reader != null) {
    16                 try {
    17                     reader.close();
    18                 } catch (IOException e) {
    19                     e.printStackTrace();
    20                 }
    21             }
    22         }
    23     }
    24 
    25     /**
    26      * 清空回收站,也就是不能够恢复数据啦
    27      */
    28     public void forceMerge() {
    29         IndexWriter writer = null;
    30         try {
    31             writer = new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35,new StandardAnalyzer(Version.LUCENE_35)));
    32             writer.forceMergeDeletes();
    33         } catch (CorruptIndexException e) {
    34             e.printStackTrace();
    35         } catch (IOException e) {
    36             e.printStackTrace();
    37         } finally {
    38             if (writer != null) {
    39                 try {
    40                     writer.close();
    41                 } catch (IOException e) {
    42                     e.printStackTrace();
    43                 }
    44             }
    45         }
    46     }
    47     
    48     /**
    49      * 更新操作
    50      */
    51     public void update(){
    52         IndexWriter writer = null;
    53         try {
    54             writer = new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35,new StandardAnalyzer(Version.LUCENE_35)));
    55             
    56             Document document = new Document();
    57             document.add(new Field("id", "11", Field.Store.YES,
    58                     Field.Index.NOT_ANALYZED_NO_NORMS));
    59             document.add(new Field("email", emails[0], Field.Store.YES,
    60                     Field.Index.NOT_ANALYZED));
    61             document.add(new Field("content", contents[0], Field.Store.YES,
    62                     Field.Index.ANALYZED));
    63             writer.updateDocument(new Term("id","1"), document);
    64         } catch (CorruptIndexException e) {
    65             e.printStackTrace();
    66         } catch (IOException e) {
    67             e.printStackTrace();
    68         } finally {
    69             if (writer != null) {
    70                 try {
    71                     writer.close();
    72                 } catch (IOException e) {
    73                     e.printStackTrace();
    74                 }
    75             }
    76         }
    77     }

    测试代码:

     1 @Test
     2     public void testDelete(){
     3         LuceneUtil util = new LuceneUtil();
     4         System.out.println("删除前 =======");
     5         util.query();
     6         util.delete();
     7         System.out.println("删除后 =======");
     8         util.query();
     9     }
    10     
    11     @Test
    12     public void testRecover(){
    13         LuceneUtil util = new LuceneUtil();
    14         System.out.println("恢复前 ========");
    15         util.query();
    16         util.recover();
    17         System.out.println("恢复后 ========");
    18         util.query();
    19     }
    20     
    21     @Test
    22     public void testUpdate(){
    23         LuceneUtil util = new LuceneUtil();
    24         System.out.println("更新前 ========");
    25         util.query();
    26         util.update();
    27         System.out.println("更新后 ========");
    28         util.query();
    29     }

    I'm Hongten

  • 相关阅读:
    BZOJ3566: [SHOI2014]概率充电器
    BZOJ5018: [Snoi2017]英雄联盟
    BZOJ4627: [BeiJing2016]回转寿司
    BZOJ4719: [Noip2016]天天爱跑步
    BZOJ1511: [POI2006]OKR-Periods of Words
    BZOJ4721: [Noip2016]蚯蚓
    BZOJ1922: [Sdoi2010]大陆争霸
    BZOJ2525: [Poi2011]Dynamite
    单选按钮 / 复选框 样式自定义
    HDU 产生冠军 2094
  • 原文地址:https://www.cnblogs.com/hongten/p/hongten_index_update_recover.html
Copyright © 2011-2022 走看看