全文检索lucene6.1的检索方式
背景:
一:按被搜索的资源类型
1、可以转为文本的
2、多媒体类型的
二:按照搜索方式:
1、不处理语义,只是找出现了指定词语的所有文本。(指对词语进行匹配)
基本概念:
1、使用流程:先建立索引,(索引库)在进行搜索。
2、使用Lucene的数据结构,document、field。
建立索引的过程:
1、定义一个语法分词器
2、确定索引存储的位置
3、创建IndexWriter,进行索引的写入
4、内容提取,进行索引文件的写入
5、关闭indexWriter
从索引库中搜索的过程:
1、打开存储位置
2、创建搜索器
3、类似SQL进行查询
4、处理结果
5、关闭DirectoryReader
- /**
- * @项目名称:lucene
- * @类名称:Article
- * @类描述:这是一个文章实体类
- * @创建人:YangChao
- * @创建时间:2016年8月30日 下午3:11:38
- * @version 1.0.0
- */
- public class Article {
- private Integer id;
- private String title;
- private String content;
- }
- /**
- * @项目名称:lucene
- * @类名称:DocumentUtils
- * @类描述:文章实体类和Document的转换工具
- * @创建人:YangChao
- * @创建时间:2016年8月31日 上午10:15:22
- * @version 1.0.0
- */
- public class DocumentUtils {
- public static Document article2Document(Article article) {
- Document doc = new Document();
- doc.add(new Field("id", article.getId().toString(), TextField.TYPE_STORED));
- doc.add(new Field("title", article.getTitle(), TextField.TYPE_STORED));
- doc.add(new Field("content", article.getContent(), TextField.TYPE_STORED));
- return doc;
- }
- public static Article document2Ariticle(Document doc) {
- Article article = new Article();
- article.setId(Integer.parseInt(doc.get("id")));
- article.setTitle(doc.get("title"));
- article.setContent(doc.get("content"));
- return article;
- }
- }
- /**
- * @项目名称:lucene
- * @类名称:LuceneUtils
- * @类描述:获取分词器和索引位置
- * @创建人:YangChao
- * @创建时间:2016年8月31日 上午9:48:06
- * @version 1.0.0
- */
- public class LuceneUtils {
- private static Logger logger = Logger.getLogger(LuceneUtils.class);
- private static Directory directory;
- private static Analyzer analyzer;
- static {
- try {
- directory = FSDirectory.open(Paths.get("./tmp/testindex"));
- // analyzer = new StandardAnalyzer();
- analyzer = new SmartChineseAnalyzer();
- } catch (Exception e) {
- logger.error("LuceneUtils error!", e);
- }
- }
- public static Directory getDirectory() {
- return directory;
- }
- public static Analyzer getAnalyzer() {
- return analyzer;
- }
- public static void closeIndexWriter(IndexWriter indexWriter) {
- if (indexWriter != null) {
- try {
- indexWriter.close();
- } catch (Exception e2) {
- logger.error("indexWriter.close error", e2);
- }
- }
- }
- }
- **
- * @项目名称:lucene
- * @类名称:QueryResult
- * @类描述:结果集
- * @创建人:YangChao
- * @创建时间:2016年8月31日 下午4:56:24
- * @version 1.0.0
- */
- public class QueryResult {
- private int count;
- private List list;
- public QueryResult() {
- super();
- }
- public QueryResult(int count, List list) {
- super();
- this.count = count;
- this.list = list;
- }
- }
- /**
- * @项目名称:lucene
- * @类名称:IndexDao
- * @类描述:
- * @创建人:YangChao
- * @创建时间:2016年8月31日 上午10:12:05
- * @version 1.0.0
- */
- public class IndexDao {
- private static Logger logger = Logger.getLogger(IndexDao.class);
- public void save(Article article) {
- Document doc = DocumentUtils.article2Document(article);
- IndexWriter indexWriter = null;
- try {
- IndexWriterConfig config = new IndexWriterConfig(LuceneUtils.getAnalyzer());
- indexWriter = new IndexWriter(LuceneUtils.getDirectory(), config);
- indexWriter.addDocument(doc);
- } catch (Exception e) {
- logger.error("IndexDao.save error", e);
- } finally {
- LuceneUtils.closeIndexWriter(indexWriter);
- }
- }
- public void delete(String id) {
- IndexWriter indexWriter = null;
- try {
- Term term = new Term("id", id);
- IndexWriterConfig config = new IndexWriterConfig(LuceneUtils.getAnalyzer());
- indexWriter = new IndexWriter(LuceneUtils.getDirectory(), config);
- indexWriter.deleteDocuments(term);// 删除含有指定term的所有文档
- } catch (Exception e) {
- logger.error("IndexDao.save error", e);
- } finally {
- LuceneUtils.closeIndexWriter(indexWriter);
- }
- }
- public void update(Article article) {
- Document doc = DocumentUtils.article2Document(article);
- IndexWriter indexWriter = null;
- try {
- Term term = new Term("id", article.getId().toString());
- IndexWriterConfig config = new IndexWriterConfig(LuceneUtils.getAnalyzer());
- indexWriter = new IndexWriter(LuceneUtils.getDirectory(), config);
- indexWriter.updateDocument(term, doc);// 先删除,后创建。
- } catch (Exception e) {
- logger.error("IndexDao.save error", e);
- } finally {
- LuceneUtils.closeIndexWriter(indexWriter);
- }
- }
- public QueryResult search(String queryString, int firstResult, int maxResult) {
- List<Article> list = new ArrayList<Article>();
- try {
- DirectoryReader ireader = DirectoryReader.open(LuceneUtils.getDirectory());
- // 2、第二步,创建搜索器
- IndexSearcher isearcher = new IndexSearcher(ireader);
- // 3、第三步,类似SQL,进行关键字查询
- String[] fields = { "title", "content" };
- QueryParser parser = new MultiFieldQueryParser(fields, LuceneUtils.getAnalyzer());
- Query query = parser.parse("检索");
- TopDocs topDocs = isearcher.search(query, firstResult + maxResult);
- int count = topDocs.totalHits;// 总记录数
- System.out.println("总记录数为:" + topDocs.totalHits);// 总记录数
- ScoreDoc[] hits = topDocs.scoreDocs;// 第二个参数,指定最多返回前n条结果
- // 高亮
- Formatter formatter = new SimpleHTMLFormatter("<font color='red'>", "</font>");
- Scorer source = new QueryScorer(query);
- Highlighter highlighter = new Highlighter(formatter, source);
- // 摘要
- // Fragmenter fragmenter = new SimpleFragmenter(5);
- // highlighter.setTextFragmenter(fragmenter);
- // 处理结果
- int endIndex = Math.min(firstResult + maxResult, hits.length);
- for (int i = firstResult; i < endIndex; i++) {
- Document hitDoc = isearcher.doc(hits[i].doc);
- Article article = DocumentUtils.document2Ariticle(hitDoc);
- //
- String text = highlighter.getBestFragment(LuceneUtils.getAnalyzer(), "content", hitDoc.get("content"));
- if (text != null) {
- article.setContent(text);
- }
- list.add(article);
- }
- ireader.close();
- return new QueryResult(count, list);
- } catch (Exception e) {
- logger.error("IndexDao.search error", e);
- }
- return null;
- }
- }
- lucence详细学习地址:http://www.cnblogs.com/zhuxiaojie/p/5277219.html
全文检索lucene6.1的检索方式的更多相关文章
- Hibernate —— HQL、QBC检索方式
一.HQL 检索方式 以双向的一对多来测试 HQL 检索方式.以 Department 和 Employee 为例. 建表语句: CREATE TABLE department ( dept_id ) ...
- Hibernate的检索方式
Hibernate的检索方式 检索方式(查询的方式) 导航对象图检索方式: 根据已经加载的对象导航到其他对象 Customer customer = (Customer)session.get(Cus ...
- 攻城狮在路上(壹) Hibernate(十四)--- Hibernate的检索方式(下)
本节介绍HQL和QBC的高级用法:各种连接查询.投影查询.报表查询.动态查询.集合过滤和子查询等.另外将归纳优化查询程序代码,从而提高查询性能的各种技巧.一.连接查询: HQL与QBC支持的各种连接类 ...
- 攻城狮在路上(壹) Hibernate(十三)--- Hibernate的检索方式(上)
Hibernate提供了以下几种检索对象的方式: A.导航对象图检索方式. B.OID检索方式.Session.get() load(); C.HQL检索方式.Query. D.QBC检索方式.Que ...
- hibernate检索方式(HQL 检索方式,QBC 检索方式,本地 SQL 检索方式)
hibernate有五种检索方式,这儿用 单向的一对多的映射关系 例子,这儿有后三种的方式: 导航对象图检索方式: 根据已经加载的对象导航到其他对象 OID 检索方式: 按照对象的 OID 来检索对象 ...
- Hibernate 检索方式
概述 •Hibernate 提供了以下几种检索对象的方式 –导航对象图检索方式: 根据已经加载的对象导航到其他对象 –OID 检索方式: 按照对象的 OID 来检索对象 –HQL 检索方式: 使用 ...
- Hibernate入门6.Hibernate检索方式
Hibernate入门6.Hibernate检索方式 20131128 代码下载 链接: http://pan.baidu.com/s/1Ccuup 密码: vqlv Hibernate的整体框架已经 ...
- [原创]java WEB学习笔记89:Hibernate学习之路-- -Hibernate检索方式(5种),HQL介绍,实现功能,实现步骤,
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Hibernate的三种常用检索方式
Hibernate 提供了以下几种检索对象的方式 ¨ 导航对象图检索方式: 根据已经加载的对象导航到其他对象 ¨ OID 检索方式: 按照对象的 OID 来检索对象 ¨ ...
随机推荐
- sass揭秘之@mixin,%,@function(转载)
因为文章内含有很多sass代码,如需自己动手查看编译结果,推荐使用sassmeister这款在线编译工具,方便你阅读学习. 在阅读本文章之前,请先确认你已经阅读了上篇文章sass揭秘之变量,不然会给你 ...
- VA Code编写html(1)
<html> <head> <title>my webside</title> <!--win+‘/’注释行--> <!--防止中文乱 ...
- day25-1 time,datetime模块
目录 time 为什么要有time模块,time模块有什么用 时间戳形式 格式化时间 结构化时间 各种时间格式互相转换 datetime 为什么要有datetime模块,detatime模块有什么用 ...
- SyntaxError Non-ASCII character '\xe5' in file
环境: windows7 Python 2.7.16 在源码中添加注释之后报错如下: (WeChat) E:\WorkHome\Wechat>python firstBlood.py Trace ...
- 嵌入式 ThriftServer in Spark
我们知道在Spark中可以通过start-thriftServer.sh 来启动ThriftServer,之后并可以通过beeline或者JDBC来连接并执行Spark SQL.在一般的Spark应用 ...
- 树状数组||归并排序求逆序对+离散化 nlogn
我好咸鱼. 归并排序之前写过,树状数组就是维护从后往前插入,找比现在插入的数大的数的数量. 如果值域大,可以离散化 #include <cstdio> #include <cstri ...
- maven 依赖的传递性
1.如图我们有三个项目,项目Age,项目Bge,项目Cge 2.我们使Age项目依赖到Bge项目,Bge项目依赖到Cge项目 Age项目和Bge项目分别执行命令:mvn install 打包*.ja ...
- 【codeforces 727D】T-shirts Distribution
[题目链接]:http://codeforces.com/problemset/problem/727/D [题意] 给你6种尺寸的衣服; 他们的尺码依次为S, M, L, XL, XXL, XXXL ...
- asp.net MVC 自定义模型绑定 从客户端中检测到有潜在危险的 Request.QueryString 值
asp.net mvc 自定义模型绑定 有潜在的Requset.Form 自定义了一个模型绑定器.前端会传过来一些敏感字符.调用bindContext. valueProvider.GetValue( ...
- static类型autowired 注入失败
原代码:注入commonService对象失败 @Autowired private static CommonService commonService; public static List< ...