基于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团队,或者项目目标是想建立一个企业网使用的 ...
随机推荐
- 『Shell编程』学习记录(2)
例1.文件io #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include & ...
- python中json文件处理涉及的四个函数json.dumps()和json.loads()、json.dump()和json.load()的区分
一.概念理解 1.json.dumps()和json.loads()是json格式处理函数(可以这么理解,json是字符串) (1)json.dumps()函数是将一个Python数据类型列表进行js ...
- pyspider 初次使用
一 安装 pip install pyspider 请安装PhantomJS:http://phantomjs.org/build.html 二 检验是否启动成功 cmd中输入: pyspider 安 ...
- Shell 全局变量、环境变量和局部变量
Shell 变量的作用域(Scope),就是 Shell 变量的有效范围(可以使用的范围). 在不同的作用域中,同名的变量不会相互干涉,就好像 A 班有个叫小明的同学,B 班也有个叫小明的同学,虽然他 ...
- SQL insert into select 语句
遇到权限数据变更的需要批量到别的平台, 在175平台添加一个权限需要, 批量到别的现有平台, 以后的建站, 会把sql放到自动建站里面; 权限的 insert into select 表一: `ous ...
- poium测试库介绍
poium测试库前身为selenium-page-objects测试库,我在以前的文章中也有介绍过:这可能是最简单的Page Object库,项目的核心是基于Page Objects实现元素定位的封装 ...
- webstorm配置svn详解
1. 打开webstorm-> file -> setting -> plguins 输入svn如果没有SVNToolBox就在下面的列表中安装SVNToolBox插件即可. 2.c ...
- SSM框架整合环境构建——基于Spring4和Mybatis3
目录 环境 配置说明 所需jar包 配置db.properties 配置log4j.properties 配置spring.xml 配置mybatis-spring.xml 配置springmvc.x ...
- Appuim的安装步骤
1.下载Appium Desktop并安装 下载地址:https://github.com/appium/appium-desktop/releases 我下载的版本为:appium-desktop- ...
- antd form 自定义验证表单使用方法
import React from 'react'; import classNames from 'classnames'; export default class FormClass exten ...