2019独角兽企业重金招聘Python工程师标准>>>

前言

  搞检索的,应该多少都会了解Lucene一些,它开源而且简单上手,官方API足够编写些小DEMO。并且根据倒排索引,实现快速检索。本文就简单的实现增量添加索引,删除索引,通过关键字查询,以及更新索引等操作。

  目前博猪使用的不爽的地方就是,读取文件内容进行全文检索时,需要自己编写读取过程(这个solr免费帮我们实现)。而且创建索引的过程比较慢,还有很大的优化空间,这个就要细心下来研究了。

  创建索引

  Lucene在进行创建索引时,根据前面一篇博客,已经讲完了大体的流程,这里再简单说下:

1 Directory directory = FSDirectory.open("/tmp/testindex");
2 IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer);
3 IndexWriter iwriter = new IndexWriter(directory, config);
4 Document doc = new Document();
5 String text = "This is the text to be indexed.";
6 doc.add(new Field("fieldname", text, TextField.TYPE_STORED)); iwriter.close();

  1 创建Directory,获取索引目录

  2 创建词法分析器,创建IndexWriter对象

  3 创建document对象,存储数据

  4 关闭IndexWriter,提交

 1 /**
 2      * 建立索引
 3      *
 4      * @param args
 5      */
 6     public static void index() throws Exception {
 7
 8         String text1 = "hello,man!";
 9         String text2 = "goodbye,man!";
10         String text3 = "hello,woman!";
11         String text4 = "goodbye,woman!";
12
13         Date date1 = new Date();
14         analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
15         directory = FSDirectory.open(new File(INDEX_DIR));
16
17         IndexWriterConfig config = new IndexWriterConfig(
18                 Version.LUCENE_CURRENT, analyzer);
19         indexWriter = new IndexWriter(directory, config);
20
21         Document doc1 = new Document();
22         doc1.add(new TextField("filename", "text1", Store.YES));
23         doc1.add(new TextField("content", text1, Store.YES));
24         indexWriter.addDocument(doc1);
25
26         Document doc2 = new Document();
27         doc2.add(new TextField("filename", "text2", Store.YES));
28         doc2.add(new TextField("content", text2, Store.YES));
29         indexWriter.addDocument(doc2);
30
31         Document doc3 = new Document();
32         doc3.add(new TextField("filename", "text3", Store.YES));
33         doc3.add(new TextField("content", text3, Store.YES));
34         indexWriter.addDocument(doc3);
35
36         Document doc4 = new Document();
37         doc4.add(new TextField("filename", "text4", Store.YES));
38         doc4.add(new TextField("content", text4, Store.YES));
39         indexWriter.addDocument(doc4);
40
41         indexWriter.commit();
42         indexWriter.close();
43
44         Date date2 = new Date();
45         System.out.println("创建索引耗时:" + (date2.getTime() - date1.getTime()) + "ms\n");
46     }

  增量添加索引

  Lucene拥有增量添加索引的功能,在不会影响之前的索引情况下,添加索引,它会在何时的时机,自动合并索引文件。

 1 /**
 2      * 增加索引
 3      *
 4      * @throws Exception
 5      */
 6     public static void insert() throws Exception {
 7         String text5 = "hello,goodbye,man,woman";
 8         Date date1 = new Date();
 9         analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
10         directory = FSDirectory.open(new File(INDEX_DIR));
11
12         IndexWriterConfig config = new IndexWriterConfig(
13                 Version.LUCENE_CURRENT, analyzer);
14         indexWriter = new IndexWriter(directory, config);
15
16         Document doc1 = new Document();
17         doc1.add(new TextField("filename", "text5", Store.YES));
18         doc1.add(new TextField("content", text5, Store.YES));
19         indexWriter.addDocument(doc1);
20
21         indexWriter.commit();
22         indexWriter.close();
23
24         Date date2 = new Date();
25         System.out.println("增加索引耗时:" + (date2.getTime() - date1.getTime()) + "ms\n");
26     }

  

  删除索引

  Lucene也是通过IndexWriter调用它的delete方法,来删除索引。我们可以通过关键字,删除与这个关键字有关的所有内容。如果仅仅是想要删除一个文档,那么最好就顶一个唯一的ID域,通过这个ID域,来进行删除操作。

 1 /**
 2      * 删除索引
 3      *
 4      * @param str 删除的关键字
 5      * @throws Exception
 6      */
 7     public static void delete(String str) throws Exception {
 8         Date date1 = new Date();
 9         analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
10         directory = FSDirectory.open(new File(INDEX_DIR));
11
12         IndexWriterConfig config = new IndexWriterConfig(
13                 Version.LUCENE_CURRENT, analyzer);
14         indexWriter = new IndexWriter(directory, config);
15
16         indexWriter.deleteDocuments(new Term("filename",str));
17
18         indexWriter.close();
19
20         Date date2 = new Date();
21         System.out.println("删除索引耗时:" + (date2.getTime() - date1.getTime()) + "ms\n");
22     }

  

  更新索引

  Lucene没有真正的更新操作,通过某个fieldname,可以更新这个域对应的索引,但是实质上,它是先删除索引,再重新建立的。

 1 /**
 2      * 更新索引
 3      *
 4      * @throws Exception
 5      */
 6     public static void update() throws Exception {
 7         String text1 = "update,hello,man!";
 8         Date date1 = new Date();
 9          analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
10          directory = FSDirectory.open(new File(INDEX_DIR));
11
12          IndexWriterConfig config = new IndexWriterConfig(
13                  Version.LUCENE_CURRENT, analyzer);
14          indexWriter = new IndexWriter(directory, config);
15
16          Document doc1 = new Document();
17         doc1.add(new TextField("filename", "text1", Store.YES));
18         doc1.add(new TextField("content", text1, Store.YES));
19
20         indexWriter.updateDocument(new Term("filename","text1"), doc1);
21
22          indexWriter.close();
23
24          Date date2 = new Date();
25          System.out.println("更新索引耗时:" + (date2.getTime() - date1.getTime()) + "ms\n");
26     }

  

  通过索引查询关键字

  Lucene的查询方式有很多种,这里就不做详细介绍了。它会返回一个ScoreDoc的集合,类似ResultSet的集合,我们可以通过域名获取想要获取的内容。

 1 /**
 2      * 关键字查询
 3      *
 4      * @param str
 5      * @throws Exception
 6      */
 7     public static void search(String str) throws Exception {
 8         directory = FSDirectory.open(new File(INDEX_DIR));
 9         analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
10         DirectoryReader ireader = DirectoryReader.open(directory);
11         IndexSearcher isearcher = new IndexSearcher(ireader);
12
13         QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "content",analyzer);
14         Query query = parser.parse(str);
15
16         ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
17         for (int i = 0; i < hits.length; i++) {
18             Document hitDoc = isearcher.doc(hits[i].doc);
19             System.out.println(hitDoc.get("filename"));
20             System.out.println(hitDoc.get("content"));
21         }
22         ireader.close();
23         directory.close();
24     }

  全部代码

  1 package test;
  2
  3 import java.io.File;
  4 import java.util.Date;
  5 import java.util.List;
  6
  7 import org.apache.lucene.analysis.Analyzer;
  8 import org.apache.lucene.analysis.standard.StandardAnalyzer;
  9 import org.apache.lucene.document.Document;
 10 import org.apache.lucene.document.LongField;
 11 import org.apache.lucene.document.TextField;
 12 import org.apache.lucene.document.Field.Store;
 13 import org.apache.lucene.index.DirectoryReader;
 14 import org.apache.lucene.index.IndexWriter;
 15 import org.apache.lucene.index.IndexWriterConfig;
 16 import org.apache.lucene.index.Term;
 17 import org.apache.lucene.queryparser.classic.QueryParser;
 18 import org.apache.lucene.search.IndexSearcher;
 19 import org.apache.lucene.search.Query;
 20 import org.apache.lucene.search.ScoreDoc;
 21 import org.apache.lucene.store.Directory;
 22 import org.apache.lucene.store.FSDirectory;
 23 import org.apache.lucene.util.Version;
 24
 25 public class TestLucene {
 26     // 保存路径
 27     private static String INDEX_DIR = "D:\\luceneIndex";
 28     private static Analyzer analyzer = null;
 29     private static Directory directory = null;
 30     private static IndexWriter indexWriter = null;
 31
 32     public static void main(String[] args) {
 33         try {
 34 //            index();
 35             search("man");
 36 //            insert();
 37 //            delete("text5");
 38 //            update();
 39         } catch (Exception e) {
 40             e.printStackTrace();
 41         }
 42     }
 43     /**
 44      * 更新索引
 45      *
 46      * @throws Exception
 47      */
 48     public static void update() throws Exception {
 49         String text1 = "update,hello,man!";
 50         Date date1 = new Date();
 51          analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
 52          directory = FSDirectory.open(new File(INDEX_DIR));
 53
 54          IndexWriterConfig config = new IndexWriterConfig(
 55                  Version.LUCENE_CURRENT, analyzer);
 56          indexWriter = new IndexWriter(directory, config);
 57
 58          Document doc1 = new Document();
 59         doc1.add(new TextField("filename", "text1", Store.YES));
 60         doc1.add(new TextField("content", text1, Store.YES));
 61
 62         indexWriter.updateDocument(new Term("filename","text1"), doc1);
 63
 64          indexWriter.close();
 65
 66          Date date2 = new Date();
 67          System.out.println("更新索引耗时:" + (date2.getTime() - date1.getTime()) + "ms\n");
 68     }
 69     /**
 70      * 删除索引
 71      *
 72      * @param str 删除的关键字
 73      * @throws Exception
 74      */
 75     public static void delete(String str) throws Exception {
 76         Date date1 = new Date();
 77         analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
 78         directory = FSDirectory.open(new File(INDEX_DIR));
 79
 80         IndexWriterConfig config = new IndexWriterConfig(
 81                 Version.LUCENE_CURRENT, analyzer);
 82         indexWriter = new IndexWriter(directory, config);
 83
 84         indexWriter.deleteDocuments(new Term("filename",str));
 85
 86         indexWriter.close();
 87
 88         Date date2 = new Date();
 89         System.out.println("删除索引耗时:" + (date2.getTime() - date1.getTime()) + "ms\n");
 90     }
 91     /**
 92      * 增加索引
 93      *
 94      * @throws Exception
 95      */
 96     public static void insert() throws Exception {
 97         String text5 = "hello,goodbye,man,woman";
 98         Date date1 = new Date();
 99         analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
100         directory = FSDirectory.open(new File(INDEX_DIR));
101
102         IndexWriterConfig config = new IndexWriterConfig(
103                 Version.LUCENE_CURRENT, analyzer);
104         indexWriter = new IndexWriter(directory, config);
105
106         Document doc1 = new Document();
107         doc1.add(new TextField("filename", "text5", Store.YES));
108         doc1.add(new TextField("content", text5, Store.YES));
109         indexWriter.addDocument(doc1);
110
111         indexWriter.commit();
112         indexWriter.close();
113
114         Date date2 = new Date();
115         System.out.println("增加索引耗时:" + (date2.getTime() - date1.getTime()) + "ms\n");
116     }
117     /**
118      * 建立索引
119      *
120      * @param args
121      */
122     public static void index() throws Exception {
123
124         String text1 = "hello,man!";
125         String text2 = "goodbye,man!";
126         String text3 = "hello,woman!";
127         String text4 = "goodbye,woman!";
128
129         Date date1 = new Date();
130         analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
131         directory = FSDirectory.open(new File(INDEX_DIR));
132
133         IndexWriterConfig config = new IndexWriterConfig(
134                 Version.LUCENE_CURRENT, analyzer);
135         indexWriter = new IndexWriter(directory, config);
136
137         Document doc1 = new Document();
138         doc1.add(new TextField("filename", "text1", Store.YES));
139         doc1.add(new TextField("content", text1, Store.YES));
140         indexWriter.addDocument(doc1);
141
142         Document doc2 = new Document();
143         doc2.add(new TextField("filename", "text2", Store.YES));
144         doc2.add(new TextField("content", text2, Store.YES));
145         indexWriter.addDocument(doc2);
146
147         Document doc3 = new Document();
148         doc3.add(new TextField("filename", "text3", Store.YES));
149         doc3.add(new TextField("content", text3, Store.YES));
150         indexWriter.addDocument(doc3);
151
152         Document doc4 = new Document();
153         doc4.add(new TextField("filename", "text4", Store.YES));
154         doc4.add(new TextField("content", text4, Store.YES));
155         indexWriter.addDocument(doc4);
156
157         indexWriter.commit();
158         indexWriter.close();
159
160         Date date2 = new Date();
161         System.out.println("创建索引耗时:" + (date2.getTime() - date1.getTime()) + "ms\n");
162     }
163
164     /**
165      * 关键字查询
166      *
167      * @param str
168      * @throws Exception
169      */
170     public static void search(String str) throws Exception {
171         directory = FSDirectory.open(new File(INDEX_DIR));
172         analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
173         DirectoryReader ireader = DirectoryReader.open(directory);
174         IndexSearcher isearcher = new IndexSearcher(ireader);
175
176         QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "content",analyzer);
177         Query query = parser.parse(str);
178
179         ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
180         for (int i = 0; i < hits.length; i++) {
181             Document hitDoc = isearcher.doc(hits[i].doc);
182             System.out.println(hitDoc.get("filename"));
183             System.out.println(hitDoc.get("content"));
184         }
185         ireader.close();
186         directory.close();
187     }
188 }

View Code

  参考资料

  http://www.cnblogs.com/xing901022/p/3933675.html

转载于:https://my.oschina.net/u/204616/blog/545370

【手把手教你全文检索】Lucene索引的【增、删、改、查】相关推荐

  1. 表单的增 删 改 查

    django单表操作 增 删 改 查 一.实现:增.删.改.查 1.获取所有数据显示在页面上 model.Classes.object.all(),拿到数据后,渲染给前端;前端通过for循环的方式,取 ...

  2. properties(map)增.删.改.查.遍历

    import java.util.Map; import java.util.Properties; import java.util.Set;/*** properties(map)增.删.改.查. ...

  3. python学生姓名添加删除_python-函数-实现学生管理系统,完成对学员的增,删,改,查和退出学生管理系统。...

    实现学生管理系统,完成对学员的增,删,改,查和退出学生管理系统. 要求1:使用一个list用于保存学生的姓名. 要求2:输入0显示所有学员信息,1代表增加,2代表删除,3代表修改,4代表查询,exit ...

  4. PySpark︱DataFrame操作指南:增/删/改/查/合并/统计与数据处理

    笔者最近需要使用pyspark进行数据整理,于是乎给自己整理一份使用指南.pyspark.dataframe跟pandas的差别还是挺大的. 文章目录 1.-------- 查 -------- -- ...

  5. Python 操作 Elasticsearch 实现 增 删 改 查

    Github 地址:https://github.com/elastic/elasticsearch-py/blob/master/docs/index.rst 官网地址:https://elasti ...

  6. list 增 删 改 查 及 公共方法

    1 # 热身题目:增加名字,并且按q(不论大小写)退出程序 2 li = ['taibai','alex','wusir','egon','女神'] 3 while 1: 4 username = i ...

  7. pyRedis - 操作指南:增/删/改/查、管道与发布订阅功能

    文章目录 1 redis docker 部署与安装 2 py - redis的使用 2.1 redis的连接 2.2 常规属性查看 2.2.2 关于删除 2.3 STRING 字符串的操作 2.4 H ...

  8. Linux技术--mysql数据库增-删-改-查

    # mysql 数据库 ## 数据库的操作 ### 五个单位 * 数据库服务器   Linux或者 windows  * 数据库  * 数据表 * 数据字段 * 数据行 ### 连接数据库 ``` 1 ...

  9. 学生信息管理系统——JAVA 语言版(主页面+增+删+改+查+退)

    学生信息管理系统 前言 一.问题分析 二.学生信息管理系统程序实现思路 三.Student类的创建 程序思路 Student类代码 四.StudentManager类的创建 程序思路 StudentM ...

  10. 简单的php数据库操作类代码(增,删,改,查)

    数据库操纵基本流程为: 1.连接数据库服务器 2.选择数据库 3.执行SQL语句 4.处理结果集 5.打印操作信息 其中用到的相关函数有 •resource mysql_connect ( [stri ...

最新文章

  1. 【CV项目实战】纯新手如何从零开始完成一个工业级的图像分类任务?
  2. 博弈论 斯坦福game theory stanford week 1.1
  3. vue 父组件与子组件之间的传值(普通传值)
  4. 黑客马拉松 招募_我如何赢得第一次黑客马拉松-研究,设计和编码的2个狂野日子
  5. 会移动的文字(Marquee)
  6. C++Socket编程总结
  7. 云搜索服务在APP搜索场景的应用
  8. Arduino十大滤波算法程序大全(精编无错版)
  9. 解决SVN汉化不成功问题,下载历史版本
  10. c语言程序调试方法有哪些,c语言程序的调试方法有哪些
  11. echarts中国地图,省市标注代码
  12. java 逆矩阵_用Java实现求逆矩阵
  13. hotfox(gyb v2.6)增强
  14. oracle快速复制一个表
  15. 计算机的新兴技术在测绘工程领域的应用,测绘新技术在测绘工程测量中应用探讨.doc...
  16. 如何消除文法的二义性
  17. 逆向爬虫28 webpack扣代码
  18. windowbuilder怎么加背景图_抖音吸粉新套路,主页背景图太太太太有心机了
  19. 【算法】高精度计算π(pi)值
  20. React 函数组件导出自定义方法的办法说明

热门文章

  1. c 连接mysql数据库_C++连接mysql数据库的两种方法
  2. ubuntu linux 搭建ftp虚拟目录
  3. 网卡设置 多网卡的7种bond模式原理
  4. linux postgresql 恢复数据库,PostgreSQL数据库备份和恢复
  5. Java中this关键字的详解
  6. cvs数据导入工具 oracle_oracle数据库的导入导出
  7. func服务器报警显示C,serve_func.c
  8. unity 下一帧执行_Unity中的Havok Physics物理引擎
  9. @bean 什么时候执行_离婚冷静期什么时候开始执行?
  10. 根据生日计算年龄 java_Java 根据出生日期获得年龄