Lucene系列(二)luke使用及索引文档的基本操作

系列文章:

Lucene系列(一)快速入门

Lucene系列(二)luke使用及索引文档的基本操作

Lucene系列(三)查询及高亮

<font color="#0066CC">luke入门</font>

<font color="#0066CC">简介:</font>

github地址https://github.com/DmitryKey/luke

下载地址https://github.com/DmitryKey/luke/releases luke图标 Luke是一个用于Lucene/Solr/Elasticsearch 搜索引擎的,方便开发和诊断的 GUI(可视化)工具。

它有以下功能:

  • 查看文档并分析其内容(用于存储字段)
  • 在索引中搜索
  • 执行索引维护:索引运行状况检查;索引优化(运行前需要备份)
  • 从hdfs读取索引
  • 将索引或其部分导出为XML格式
  • 测试定制的Lucene分析工具
  • 创建自己的插件

<font color="#0066CC">luke适用的搜索引擎</font>

  • Apache Lucene. 大多数情况下,luke可以打开由纯Lucene生成的lucene索引。 现在人们做出纯粹的Lucene索引吗?
  • Apache Solr. Solr和Lucene共享相同的代码库,所以luke很自然可以打开Solr生成的Lucene索引。
  • Elasticsearch. Elasticsearch使用Lucene作为其最低级别的搜索引擎基础。 所以luke也可以打开它的索引!

<font color="#0066CC">下载安装与简单使用</font>

<font color="#0066CC">下载安装</font>

1 2. 2 3. 3 4. 4 5. 5

<font color="#0066CC">索引文档的CRUD操作</font>

  1. <font color="#00CC00">创建项目并添加Maven依</font>

    1 <dependency> 2 <groupId>junit</groupId> 3 <artifactId>junit</artifactId> 4 <version>4.12</version> 5 <scope>test</scope> 6 </dependency> 7 <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core --> 8 <!-- Lucene核心库 --> 9 <dependency> 10 <groupId>org.apache.lucene</groupId> 11 <artifactId>lucene-core</artifactId> 12 <version>7.2.1</version> 13 </dependency> 14 <!-- Lucene解析库 --> 15 <dependency> 16 <groupId>org.apache.lucene</groupId> 17 <artifactId>lucene-queryparser</artifactId> 18 <version>7.2.1</version> 19 </dependency> 20 <!-- Lucene附加的分析库 --> 21 <dependency> 22 <groupId>org.apache.lucene</groupId> 23 <artifactId>lucene-analyzers-common</artifactId> 24 <version>7.2.1</version> 25 </dependency>

我们下面要用到单元测试,所以这里我们添加了Junit单元测试的依赖(版本为4.12,2018/3/30日最新的版本)

  1. <font color="#00CC00">相关测试代码</font>

主方法:

1package lucene_index_crud; 2 3import java.nio.file.Paths; 4 5import org.apache.lucene.analysis.Analyzer; 6import org.apache.lucene.analysis.standard.StandardAnalyzer; 7import org.apache.lucene.document.Document; 8import org.apache.lucene.document.Field; 9import org.apache.lucene.document.StringField; 10import org.apache.lucene.document.TextField; 11import org.apache.lucene.index.DirectoryReader; 12import org.apache.lucene.index.IndexReader; 13import org.apache.lucene.index.IndexWriter; 14import org.apache.lucene.index.IndexWriterConfig; 15import org.apache.lucene.index.Term; 16import org.apache.lucene.store.Directory; 17import org.apache.lucene.store.FSDirectory; 18import org.junit.Test; 19 20public class Txt1 { 21 // 下面是测试用到的数据 22 private String ids[] = { "1", "2", "3" }; 23 private String citys[] = { "qingdao", "nanjing", "shanghai" }; 24 private String descs[] = { "Qingdao is a beautiful city.", "Nanjing is a city of culture.", 25 "Shanghai is a bustling city." }; 26 //Directory对象 27 private Directory dir; 28}

相关测试方法编写:

<font color="#99CCCC">1)测试创建索引</font>

1 /** 2 * 创建索引 3 * @throws Exception 4 */ 5 @Test 6 public void testWriteIndex() throws Exception { 7 //写入索引文档的路径 8 dir = FSDirectory.open(Paths.get("D:\\lucene\\index_crud\\indexdata")); 9 IndexWriter writer = getWriter(); 10 for (int i = 0; i < ids.length; i++) { 11 //创建文档对象,文档是索引和搜索的单位。 12 Document doc = new Document(); 13 doc.add(new StringField("id", ids[i], Field.Store.YES)); 14 doc.add(new StringField("city", citys[i], Field.Store.YES)); 15 doc.add(new TextField("desc", descs[i], Field.Store.NO)); 16 // 添加文档 17 writer.addDocument(doc); 18 } 19 writer.close(); 20 }

通过luke查看相关信息: desc city id

注意: 创建索引之后,后续测试方法才能正确运行。

<font color="#99CCCC">2)测试写入了几个文档:</font>

1 /** 2 * 测试写了几个文档 3 * 4 * @throws Exception 5 */ 6 @Test 7 public void testIndexWriter() throws Exception { 8 //写入索引文档的路径 9 dir = FSDirectory.open(Paths.get("D:\\lucene\\index_crud\\indexdata")); 10 IndexWriter writer = getWriter(); 11 System.out.println("写入了" + writer.numDocs() + "个文档"); 12 writer.close(); 13 }

testIndexWriter() <font color="#99CCCC">3)测试读取了几个文档:</font>

1 /** 2 * 测试读取了几个文档 3 * 4 * @throws Exception 5 */ 6 @Test 7 public void testIndexReader() throws Exception { 8 //写入索引文档的路径 9 dir = FSDirectory.open(Paths.get("D:\\lucene\\index_crud\\indexdata")); 10 IndexReader reader = DirectoryReader.open(dir); 11 System.out.println("最大文档数:" + reader.maxDoc()); 12 System.out.println("实际文档数:" + reader.numDocs()); 13 reader.close(); 14 }

testIndexReader() <font color="#99CCCC">4)测试删除 在合并前:</font>

1 /** 2 * 测试删除 在合并前 3 * 4 * @throws Exception 5 */ 6 @Test 7 public void testDeleteBeforeMerge() throws Exception { 8 //写入索引文档的路径 9 dir = FSDirectory.open(Paths.get("D:\\lucene\\index_crud\\indexdata")); 10 IndexWriter writer = getWriter(); 11 System.out.println("删除前:" + writer.numDocs()); 12 writer.deleteDocuments(new Term("id", "1")); 13 writer.commit(); 14 System.out.println("writer.maxDoc():" + writer.maxDoc()); 15 System.out.println("writer.numDocs():" + writer.numDocs()); 16 writer.close(); 17 }

testDeleteBeforeMerge() <font color="#99CCCC">5)测试删除 在合并后:</font>

我们这里先把dataindex目录下的文件删除,然后运行上面的testWriteIndex() 方法之后再测试。

1 /** 2 * 测试删除 在合并后 3 * 4 * @throws Exception 5 */ 6 @Test 7 public void testDeleteAfterMerge() throws Exception { 8 //写入索引文档的路径 9 dir = FSDirectory.open(Paths.get("D:\\lucene\\index_crud\\indexdata")); 10 IndexWriter writer = getWriter(); 11 System.out.println("删除前:" + writer.numDocs()); 12 writer.deleteDocuments(new Term("id", "1")); 13 writer.forceMergeDeletes(); // 强制删除 14 writer.commit(); 15 System.out.println("writer.maxDoc():" + writer.maxDoc()); 16 System.out.println("writer.numDocs():" + writer.numDocs()); 17 writer.close(); 18 }

testDeleteAfterMerge() <font color="#99CCCC">6)测试更新操作:</font>

我们这里先把dataindex目录下的文件删除,然后运行上面的testWriteIndex() 方法之后再测试。

1 /** 2 * 测试更新 3 * 4 * @throws Exception 5 */ 6 @Test 7 public void testUpdate() throws Exception { 8 // 写入索引文档的路径 9 dir = FSDirectory.open(Paths.get("D:\\lucene\\index_crud\\indexdata")); 10 IndexWriter writer = getWriter(); 11 Document doc = new Document(); 12 doc.add(new StringField("id", "1", Field.Store.YES)); 13 doc.add(new StringField("city", "beijing", Field.Store.YES)); 14 doc.add(new TextField("desc", "beijing is a city.", Field.Store.NO)); 15 writer.updateDocument(new Term("id", "1"), doc); 16 writer.close(); 17 }

desc city

欢迎关注我的微信公众号:“Java面试通关手册”(分享各种Java学习资源,面试题,以及企业级Java实战项目回复关键字免费领取): 微信公众号

Lucene我想暂时先更新到这里,仅仅这三篇文章想掌握Lucene是远远不够的。另外我这里三篇文章都用的最新的jar包,Lucene更新太快,5系列后的版本和之前的有些地方还是有挺大差距的,就比如为文档域设置权值的setBoost方法6.6以后已经被废除了等等。因为时间有限,所以我就草草的看了一下Lucene的官方文档,大多数内容还是看java1234网站的这个视频来学习的,然后在版本和部分代码上做了改进。截止2018/4/1,上述代码所用的jar包皆为最新。

最后推荐一下自己觉得还不错的Lucene学习网站/博客:

官方网站:[Welcome to Apache Lucene](Welcome to Apache Lucene)

Github:Apache Lucene and Solr

Lucene专栏

搜索系统18:lucene索引文件结构

Lucene6.6的介绍和使用

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Lucene系列(二)luke使用及索引文档的基本操作 - HelloWorld