前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >lucene(全文搜索)_恢复/更新索引操作

lucene(全文搜索)_恢复/更新索引操作

作者头像
Hongten
发布2018-09-13 15:34:19
6600
发布2018-09-13 15:34:19
举报
文章被收录于专栏:HongtenHongten

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

索引的恢复/更新操作

代码语言:javascript
复制
 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     }

测试代码:

代码语言:javascript
复制
 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     }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-01-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档