博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lucene系列(二)luke使用及索引文档的基本操作
阅读量:7173 次
发布时间:2019-06-29

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

系列文章:

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

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

github地址

下载地址

luke图标
Luke是一个用于Lucene/Solr/Elasticsearch 搜索引擎的,方便开发和诊断的 GUI(可视化)工具。

它有以下功能:

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

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

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

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

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

1.

1

2

3.
3
4.
4

5

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

  1. <font color="#00CC00">创建项目并添加Maven依赖</font>
junit
junit
4.12
test
org.apache.lucene
lucene-core
7.2.1
org.apache.lucene
lucene-queryparser
7.2.1
org.apache.lucene
lucene-analyzers-common
7.2.1

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

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

主方法:

package lucene_index_crud;import java.nio.file.Paths;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.StringField;import org.apache.lucene.document.TextField;import org.apache.lucene.index.DirectoryReader;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.junit.Test;public class Txt1 {    // 下面是测试用到的数据    private String ids[] = { "1", "2", "3" };    private String citys[] = { "qingdao", "nanjing", "shanghai" };    private String descs[] = { "Qingdao is a beautiful city.", "Nanjing is a city of culture.",            "Shanghai is a bustling city." };    //Directory对象      private Directory dir;}

相关测试方法编写:

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

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

通过luke查看相关信息:

desc
city
id

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

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

/**     * 测试写了几个文档     *      * @throws Exception     */    @Test    public void testIndexWriter() throws Exception {        //写入索引文档的路径        dir = FSDirectory.open(Paths.get("D:\\lucene\\index_crud\\indexdata"));        IndexWriter writer = getWriter();        System.out.println("写入了" + writer.numDocs() + "个文档");        writer.close();    }

testIndexWriter()

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

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

testIndexReader()

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

/**     * 测试删除 在合并前     *      * @throws Exception     */    @Test    public void testDeleteBeforeMerge() throws Exception {        //写入索引文档的路径        dir = FSDirectory.open(Paths.get("D:\\lucene\\index_crud\\indexdata"));        IndexWriter writer = getWriter();        System.out.println("删除前:" + writer.numDocs());        writer.deleteDocuments(new Term("id", "1"));        writer.commit();        System.out.println("writer.maxDoc():" + writer.maxDoc());        System.out.println("writer.numDocs():" + writer.numDocs());        writer.close();    }

testDeleteBeforeMerge()

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

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

/**     * 测试删除 在合并后     *      * @throws Exception     */    @Test    public void testDeleteAfterMerge() throws Exception {           //写入索引文档的路径        dir = FSDirectory.open(Paths.get("D:\\lucene\\index_crud\\indexdata"));        IndexWriter writer = getWriter();        System.out.println("删除前:" + writer.numDocs());        writer.deleteDocuments(new Term("id", "1"));        writer.forceMergeDeletes(); // 强制删除        writer.commit();        System.out.println("writer.maxDoc():" + writer.maxDoc());        System.out.println("writer.numDocs():" + writer.numDocs());        writer.close();    }

testDeleteAfterMerge()

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

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

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

desc

city

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

微信公众号

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

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

官方网站:Welcome to Apache Lucene

Github:

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

你可能感兴趣的文章
数据结构--图的定义和存储结构
查看>>
linux常用命令
查看>>
Appium自动化测试1 - 安装部署
查看>>
Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset
查看>>
UVa294 Divisors
查看>>
洛谷P3406 海底高铁
查看>>
1、JUC--volatile 关键字-内存可见性
查看>>
uboot arp地址解析
查看>>
Java字节码 小结
查看>>
sed 替换多个空格为一个
查看>>
ControlTemplate in WPF —— DataGrid
查看>>
ubuntu下安装gedit插件
查看>>
Android圆形旋转菜单,并支持移动换位功能
查看>>
[js高手之路]node js系列课程-图解express+supervisor+ejs用法
查看>>
理解Neural Style
查看>>
对中间过程进行调试
查看>>
怎样获取数据库中某一个字段长度最长的那条记录
查看>>
yourphp常用标签
查看>>
anchor_target_layer层解读
查看>>
7.6 服务远程暴露 - 注册服务到zookeeper
查看>>