系列文章:java
Lucene系列(二)luke使用及索引文档的基本操做github
github地址:https://github.com/DmitryKey/lukeapache
下载地址:https://github.com/DmitryKey/luke/releases
Luke是一个用于Lucene/Solr/Elasticsearch 搜索引擎的,方便开发和诊断的 GUI(可视化)工具。 微信
它有如下功能:elasticsearch
下载安装工具
1.
2.
3.
4.
5.
单元测试
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core -->
<!-- Lucene核心库 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>7.2.1</version>
</dependency>
<!-- Lucene解析库 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>7.2.1</version>
</dependency>
<!-- Lucene附加的分析库 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>7.2.1</version>
</dependency>
咱们下面要用到单元测试,因此这里咱们添加了Junit单元测试的依赖(版本为4.12,2018/3/30日最新的版本)学习
主方法:
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;
}
相关测试方法编写:
1)测试建立索引
/** * 建立索引 * @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查看相关信息:
注意: 建立索引以后,后续测试方法才能正确运行。
2)测试写入了几个文档:
/** * 测试写了几个文档 * * @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();
}
3)测试读取了几个文档:
/** * 测试读取了几个文档 * * @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();
}
4)测试删除 在合并前:
/** * 测试删除 在合并前 * * @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();
}
5)测试删除 在合并后:
咱们这里先把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();
}
6)测试更新操做:
咱们这里先把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();
}
欢迎关注个人微信公众号(分享各类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