全文检索Lucene (1)
Lucene是apache开源的一个全文检索框架,很是出名。今天先来分享一个类似于HelloWorld级别的使用。
工作流程
依赖
我们要想使用Lucene,那就得先引用人家的jar包了。下面列举一下我使用到的jars.
lucene-analyzers-common-6.1.0.jar
: 分析器支持lucene-core-6.1.0.jar
: 全文检索核心支持lucene-highlighter-6.1.0.jar
: 检索到的目标词的高亮显示lucene-memory-6.1.0.jar
: 索引存储相关的支持lucene-queries-6.1.0.jar
: 查询支持lucene-queryparser-6.1.0.jar
: 查询器支持
Lucene HelloWorld
下面就着手实现一个级别为HelloWorld的小例子。实现一个基于文章内容的查询。
Article.java
/**
* @Date 2016年8月1日
*
* @author Administrator
*/
package domain;
/**
* @author 郭瑞彪
*
*/
public class Article {
private Integer id;
private String title;
private String content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
@Override
public String toString() {
return "Article [id=" + id + ", title=" + title + ", content=" + content + "]";
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
创建索引库
@Test
public void createIndex() throws Exception {
// 模拟一条文章数据
Article a = new Article();
a.setId(1);
a.setTitle("全文检索");
a.setContent("我们主要是做站内搜索(或叫系统内搜索),即对系统内的资源进行搜索");
// 建立索引
Directory dir = FSDirectory.open(Paths.get("./indexDir/"));
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
IndexWriter indexWriter = new IndexWriter(dir, indexWriterConfig);
Document doc = new Document();
doc.add(new StringField("id", a.getId().toString(), Field.Store.YES));
doc.add(new TextField("title", a.getTitle(), Field.Store.YES));
doc.add(new TextField("content", a.getContent(), Field.Store.YES));
indexWriter.addDocument(doc);
indexWriter.close();
}
从索引库中获取查询结果
@Test
public void search() throws Exception {
String queryString = "资源";
Analyzer analyzer = new StandardAnalyzer();
analyzer.setVersion(Version.LUCENE_6_1_0);
QueryParser queryParser = new QueryParser("content", analyzer);
Query query = queryParser.parse(queryString);
// IndexReader indexReader =
// DirectoryReader.open(FSDirectory.open(Paths.get("./indexDir/")));
DirectoryReader directoryReader = DirectoryReader.open(FSDirectory.open(Paths.get("./indexDir/")));
IndexReader indexReader = directoryReader;
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
TopDocs topDocs = indexSearcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
List<Article> articles = new ArrayList<Article>();
for (int i = 0; i < scoreDocs.length; i++) {
ScoreDoc scoreDoc = scoreDocs[i];
Document doc = indexSearcher.doc(scoreDoc.doc);
Article a = new Article();
a.setId(Integer.parseInt(doc.get("id")));
a.setTitle(doc.get("title"));
a.setContent(doc.get("content"));
System.out.println(a.toString());
articles.add(a);
}
// 显示结果
System.out.println("总的记录数为: " + topDocs.totalHits);
System.out.println(articles.toString());
for (Article a : articles) {
System.out.println("-----------搜索结果如下-----------------");
System.out.println(">>>id: " + a.getId());
System.out.println(">>>title:" + a.getTitle());
System.out.println(">>>content:" + a.getContent());
}
indexReader.close();
analyzer.close();
}
查询结果
总的记录数为: 4
-----------搜索结果如下-----------------
>>>id: 1
>>>title:全文检索
>>>content:我们主要是做站内搜索(或叫系统内搜索),即对系统内的资源进行搜索
-----------搜索结果如下-----------------
>>>id: 2
>>>title:全文检索2
>>>content:我们主要是做站内搜索(或叫系统内搜索),即对系统内的资源进行搜索,hahahahahhaha
总结
Lucene全文检索的功能可以这么简单的实现,但是里面有更多的用法等着我们去挖掘。
全文检索Lucene (1)的更多相关文章
- 全文检索 Lucene(4)
经过了前面几篇文章的学习,我们基本上可以适用Lucene来开发我们的站内搜索应用了.但是观察一下目前的主流的搜索引擎,我们会发现查询结果会有高亮的显示效果.所以,今天我们就来学习一下,给Lucene添 ...
- 全文检索 Lucene(3)
看完前两篇博客之后,想必大家对于Lucene的使用都有了一个比较清晰的认识了.如果对Lucene的知识点还是有点模糊的话,个人建议还是先看看这两篇文章. 全文检索 Lucene(1) 全文检索 Luc ...
- 全文检索Lucene (2)
接着全文检索Lucene (1) . 下面我们来深入的研究一下,如何使用Lucene! 从全文检索Lucene (1)中我们可以看出,Lucene就好比一个双向的工作流,一方面是对索引库的维护,另一方 ...
- Lucene 全文检索 Lucene的使用
Lucene 全文检索 Lucene的使用 一.简介: 参考百度百科: http://baike.baidu.com/link?url=eBcEVuUL3TbUivRvtgRnMr1s44nTE7 ...
- 全文检索--Lucene & ElasticSearch
全文检索--Lucene 2.1 全文检索和以前高级查询的比较 1.高级查询 缺点:1.like让数据库索引失效 2.每次查询都是查询数据库 ,如果访问的人比较多,压力也是比较大 2.全文检索框架:A ...
- [全文检索]Lucene基础入门.
本打算直接来学习Solr, 现在先把Lucene的只是捋一遍. 本文内容: 1. 搜索引擎的发展史 2. Lucene入门 3. Lucene的API详解 4. 索引调优 5. Lucene搜索结果排 ...
- 全文检索-Lucene.net
Lucene.net是Lucene的.net移植版本,在较早之前是比较受欢迎的一个开源的全文检索引擎开发包,即它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎. ...
- 全文检索Lucene框架---查询索引
一. Lucene索引库查询 对要搜索的信息创建Query查询对象,Lucene会根据Query查询对象生成最终的查询语法,类似关系数据库Sql语法一样Lucene也有自己的查询语法,比如:“name ...
- ]NET Core Lucene.net和PanGu分词实现全文检索
Lucene.net和PanGu分词实现全文检索 Lucene.net(4.8.0) 学习问题记录五: JIEba分词和Lucene的结合,以及对分词器的思考 前言:目前自己在做使用Lucene. ...
随机推荐
- 接口自动化测试:python+json+requests+数据驱动
接口测试是单元测试的一个子集,但又不等同于单元测试.从测试的角度来看,接口测试的价值在于其测试投入比单元测试少,而且技术难度也比单元测试小.一般来说,接口测试的粒度要比单元测试更粗,它主要是基于子系统 ...
- HDU 3094 A tree game
Problem Description Alice and Bob want to play an interesting game on a tree.Given is a tree on N ve ...
- Codeforces Round #460 E. Congruence Equation
Description 题面 \(n*a^n≡b (\mod P),1<=n<=x\) Solution 令 \(n=(P-1)*i+j\) \([(P-1)*i+j]*a^{[(P-1) ...
- ●BZOJ 4408 [Fjoi 2016]神秘数
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4408 题解: 主席树 首先,对于一些数来说, 如果可以我们可以使得其中的某些数能够拼出 1- ...
- 计蒜客NOIP2017提高组模拟赛(四)day1
T1:小X的质数 小 X 是一位热爱数学的男孩子,在茫茫的数字中,他对质数更有一种独特的情感.小 X 认为,质数是一切自然数起源的地方. 在小 X 的认知里,质数是除了本身和 1 以外,没有其他因数的 ...
- [SHOI2001]化工厂装箱员
题目描述 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须 ...
- list,tuple,dict,set的使用方法
list list是一种有序的集合,可以随时添加和删除其中的元素 classmates = ['Michael', 'Bob', 'Tracy'] len()函数可以获得list元素的个数.lis ...
- spring boot新建项目问题总结
1.启动报:Unregistering JMX-exposed beans on shutdown 原因:以来的Tomcat没有启动 解决办法:在pom.xml加入依赖 <dependency& ...
- C语言程序设计第四次作业-选择结构
(一)改错题 输出三角形的面积和周长,输入三角形的三条边a.b.c,如果能构成一个三角形,输出面积area和周长perimeter(保留2位小数):否则,输出"These sides do ...
- SpringBoot 中 get/post 请求处理方式,以及requestboy为Json时的处理
GET.POST方式提时, 根据request header Content-Type的值来判断: application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的 ...