全文检索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. ...
随机推荐
- svg从入门到装逼(一)
svg文件是基于xml的矢量图,而canvas是基于html和js的位图.关于两者的比较,在粗就不赘述了. 1. 首先来上一个svg的基本结构: <?xml version="1.0 ...
- codevs 2621 土地侵蚀
提交地址:http://codevs.cn/problem/2621/ 2621 土地侵蚀 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 De ...
- [HNOI 2008]GT考试
Description 题库链接 问你长度为 \(n\) 的可含前导零的数字串中,不含长度为 \(m\) 的子串 \(X\) 有多少个,取模. \(1\leq n\leq 10^9,1\leq m\l ...
- [WC 2014]紫荆花之恋
Description 强强和萌萌是一对好朋友.有一天他们在外面闲逛,突然看到前方有一棵紫荆树.这已经是紫荆花飞舞的季节了,无数的花瓣以肉眼可见的速度从紫荆树上长了出来. 仔细看看的话,这个大树实际上 ...
- [Codeforces 864A]Fair Game
Description Petya and Vasya decided to play a game. They have n cards (n is an even number). A singl ...
- Topcoder口胡记 SRM 562 Div 1 ~ SRM 599 Div 1
据说做TC题有助于提高知识水平? :) 传送门:https://284914869.github.io/AEoj/index.html 转载请注明链接:http://www.cnblogs.com/B ...
- ●BZOJ 2337 [HNOI2011]XOR和路径
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2337题解: 概率dp, 因为异或的每一位之间没有关系,我们就依次考虑每一位k.(即边权要么为 ...
- ubuntu 16.04 安装 tensorflow-gpu 包括 CUDA ,CUDNN,CONDA
ubuntu 16.04 安装 tensorflow-gpu 包括 CUDA ,CUDNN,CONDA 显卡驱动装好了,如图: 英文原文链接: https://github.com/williamFa ...
- js去除空格,判断是否包含
js去除空格 function trimStr(str){ return str.replace(/(^\s*)|(\s*$)/g,""); } js判断是否包含 //是否包含 f ...
- diango-团队介绍
1.使用django-admin startproject show创建项目,并使用python manage.py startapp team_show创建应用 2.进行相关的配置 3.代码的实现