基于jeesite的cms系统(六):Lucene全文搜索引擎
1、lucene初始化
// @Value("${lucene.index.path}")
private String indexPath = "/Users/vitoyan/Downloads/Projects/jeesite-demo/lucene_index";
private Directory directory;
private DirectoryReader reader;
@PostConstruct
public void init() {
try {
directory = FSDirectory.open(Paths.get(indexPath));
} catch (IOException e) {
e.printStackTrace();
}
}
2、添加索引
/**
* 添加索引
*
* @param article
* @throws Exception
*/
public void add(JsCmsArticlesEntity article) throws GlobalException {
IndexWriter writer = null;
try {
Analyzer analyzer = new ComplexAnalyzer();
IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
writer = new IndexWriter(directory, iwc);
Document doc = new Document();
doc.add(new TextField("id", String.valueOf(article.getId()), Field.Store.YES));
doc.add(new TextField("title", article.getTitle(), Field.Store.YES));
doc.add(new TextField("authorId", String.valueOf(article.getAuthorId()), Field.Store.YES));
doc.add(new TextField("content", article.getContent(), Field.Store.YES));
doc.add(new TextField("tags", article.getTags(), Field.Store.YES));
doc.add(new TextField("createAt", DateUtil.formateToStr(article.getCreateAt(), "yyyy-MM-dd"), Field.Store.YES));
writer.addDocument(doc);
} catch (Exception e) {
throw new GlobalException(500, e.toString());
} finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3、查询数据
/**
* 查询数据
*
* @param keyword
* @return
*/
public List<JsCmsArticlesEntity> query(String keyword) throws GlobalException {
System.out.println(keyword);
try {
IndexSearcher searcher = this.getIndexSearcher();
Analyzer analyzer = new ComplexAnalyzer(); String[] fields = {"title"};// 使用多域查询,便于以后扩展
MultiFieldQueryParser multiFieldQueryParser = new MultiFieldQueryParser(fields, analyzer);
Query query = multiFieldQueryParser.parse(keyword); TopDocs topDocs = searcher.search(query, 100); // 1.格式化对象,设置前缀和后缀
Formatter formatter = new SimpleHTMLFormatter("<font color='red'>","</font>");
// 2.关键词对象
Scorer scorer = new QueryScorer(query);
// 3. 高亮对象
Highlighter highlighter = new Highlighter(formatter, scorer); List<JsCmsArticlesEntity> list = new ArrayList<>();
JsCmsArticlesEntity article;
ScoreDoc[] scoreDocs = topDocs.scoreDocs; for (ScoreDoc scoreDoc : scoreDocs) {
Document document = searcher.doc(scoreDoc.doc);
// if (Integer.parseInt(document.get("authorId")) == 1) {
article = new JsCmsArticlesEntity();
String titleHighLight = highlighter.getBestFragment(analyzer,"title",document.get("title"));
article.setId(Integer.parseInt(document.get("id")));
article.setTitle(titleHighLight);
article.setContent(document.get("content"));
article.setTags(document.get("tags"));
// article.setCreateAt((Timestamp) DateUtil.parseToDate(document.get("createAt"), "yyyy-MM-dd"));
list.add(article);
// }
}
return list;
} catch (Exception e) {
throw new GlobalException(500, e.toString());
}
} @Test
public void test() {
LuceneService luceneService = new LuceneService();
luceneService.init();
luceneService.query("123"); } private IndexSearcher getIndexSearcher() throws IOException {
if (reader == null) {
reader = DirectoryReader.open(directory);
} else {
DirectoryReader changeReader = DirectoryReader.openIfChanged(reader);
if (changeReader != null) {
reader.close();
reader = changeReader;
}
}
return new IndexSearcher(reader);
}
4、使用lucene(相关工具类和全局异常代码可以查看码云)
在发布文章时添加文章索引到文件系统
luceneService.init();
luceneService.add(article);
或者一次添加所有索引
List<JsCmsArticlesEntity> articles = this.frontService.getAllArticles();
LuceneService luceneService = new LuceneService();
for (JsCmsArticlesEntity article : articles) {
luceneService.init();
luceneService.add(article);
System.out.println(article.getTitle());
}
然后查询数据
LuceneService luceneService = new LuceneService();
luceneService.init();
resp.setRespCode(JsCmsResponse.RESPCODE_SUCCESS);
resp.setMsgInfo("获取内容成功");
resp.setRespObj(luceneService.query(keyword));
基于jeesite的cms系统(六):Lucene全文搜索引擎的更多相关文章
- 基于jeesite的cms系统(一):开发环境搭建
基于jeesite的cms系统系列,是对基于jeesite进行二次开发的博客模块开发过程的总结.涉及入门安装,二次开发,部署等 一.概况: JeeSite 是一个 Java 企业级快速开发平台,基于经 ...
- 基于jeesite的cms系统(三):使用RESTful API在前端渲染数据
使用RESTful API可以更好的开发前后分离的应用,后面一节会介绍使用模版引擎Beetl开发后端渲染的应用. 一.配置Swagger(Api 接口文档) 1.使用系统自带 拷贝jeesite-mo ...
- 基于jeesite的cms系统(五):wangEditor富文本编辑器
一.关于wangEditor: wangEditor —— 轻量级 web 富文本编辑器,配置方便,使用简单.支持 IE10+ 浏览器. 官网:www.wangEditor.com 文档:www.ka ...
- 基于jeesite的cms系统(四):使用Beetl模版引擎在后端渲染数据
一.Beetl简介 1. 什么是Beetl Beetl目前版本是2.9.3,相对于其他java模板引擎,具有功能齐全,语法直观,性能超高,以及编写的模板容易维护等特点.使得开发和维护模板有很好的体验. ...
- 基于jeesite的cms系统(二):整体设计
一.菜单设计 在系统管理-菜单管理中可以设置内容管理菜单(自动生成) 注意:归属模块应属于核心模块core.如果新加的的菜单设置为内容管理模块cms,系统下次重启时会重置本次设置,具体原因不详. 二. ...
- 基于jeesite的cms系统(七):GlobalException全局异常和部署
关于全局异常: 在业务代码中专注处理业务,而不是返回各种CodeMsg(比如这里只需要知道登录时成功还是失败,其余情况直接抛出异常),可以直接抛出异常,添加一个全局异常类,根据CodeMsg来生成异常 ...
- 基于Java的开源CMS系统选择(转)
CMS概述 对于网站CMS系统而言,基于PHP的是主流,如Drupal/Joomla在各个主流虚拟机提供商上都是标准配置,也被广泛使用. 但如果你拥有Java团队,或者项目目标是想建立一个企业网使用的 ...
- 一个基于NodeJS开发的APP管理CMS系统
花了大概3周独立开发了一个基于NodeJS的CMS系统,用于公司APP的内容管理( **公司APP?广告放在最后 ^_^ ** ,管理员请理解~~~ )晚上看了部电影还不想睡,闲着也是闲着就作下小小总 ...
- 基于Java的开源CMS系统选择
CMS概述 对于网站CMS系统而言,基于PHP的是主流,如Drupal/Joomla在各个主流虚拟机提供商上都是标准配置,也被广泛使用. 但如果你拥有Java团队,或者项目目标是想建立一个企业网使用的 ...
随机推荐
- LeetCode算法题-Find Pivot Index(Java实现)
这是悦乐书的第304次更新,第323篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第172题(顺位题号是724).给定一个整数nums数组,编写一个返回此数组的" ...
- 用Redis管理Session
maven <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</ar ...
- 在Winform开发框架中对附件文件进行集中归档处理
在我们Winform开发中,往往需要涉及到附件的统一管理,因此我倾向于把它们独立出来作为一个附件管理模块,这样各个模块都可以使用这个附件管理模块,更好的实现模块重用的目的.在涉及附件管理的场景中,一个 ...
- JavaScript判断对象是否是NULL
这个方法是我踩了很多坑之后找到的,对数组等类型的对象都很好使,果断收藏! function isEmpty(obj) { // 检验 undefined 和 null if (!obj &&a ...
- 分布式存储ceph——(1)部署ceph
前言: 很多朋友想学ceph,但是开始ceph部署就让初学者举步为艰,ceph部署时由于国外源的问题(具体大家应该懂得),下载和安装软件便会卡住,停止不前.即使配置搭建了国内源后,执行ceph-dep ...
- Django中使用极验Geetest滑动验证码
一,环境部署 1.创建一个django测试项目 此处省略... 二,文档部署 1.下载安装python对应的SDK 使用命令从Github导入完整项目:git clone https://github ...
- Fire Again CodeForces - 35C (BFS)
After a terrifying forest fire in Berland a forest rebirth program was carried out. Due to it N rows ...
- 第二部分之Redis服务器(第十四章)
Redis服务器复制和多个客户端建立网络连接,处理客户端发送的命令请求,在数据库中保存客户端执行命令所产生的数据. 一,命令请求的执行过程 客户端向服务器发送命令请求 set key value 服务 ...
- vue前端开发。。。
1. 官网下载 https://nodejs.org/en/ 2. 安装cnpm 在命令行: npm install -g cnpm --registry=https://registry.np ...
- Url校验正则
最近需要对HTTP请求合法性做一些校验,在网上查找了一些关于URL合法性的正则表达式. 在github上的有个关于weburl匹配的gist: https://gist.github.com/dper ...