springboot ElasticSearch 简单的全文检索高亮
原文:https://segmentfault.com/a/1190000017324038?utm_source=tag-newest
首先引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
配置文件
spring.data.elasticsearch.local=true
spring.data.elasticsearch.repositories.enabled=true
spring.data.elasticsearch.cluster-name=yourname
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
然后 创建接口并继承ElasticsearchRepository
idea 类继承 ElasticsearchRepository
package com.school.service;
import com.school.model.Idea;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;
@Component
public interface IdeaRepository extends ElasticsearchRepository<Idea, Long> {
}
下一步在需要使用的service 或 Controller中 引用
@Autowired
private IdeaRepository ideaRepository; //esjap类
@Autowired
private ElasticsearchTemplate elasticsearchTemplate; //es工具
使用save方法把数据保存到es中
业务代码忽略... 保存就完事了
public void save(Long ideaId) {
// 插入数据到es中
Idea idea = this.selectById(ideaId);
idea.setId(ideaId);
ideaRepository.save(idea);
}
全文检索并高亮数据
这里注意分页的页数是从0开始... 搞得我以为没查到数据debug了很久
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.SearchResultMapper;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
@Autowired
private IdeaRepository ideaRepository; //esjap类
@Autowired
private ElasticsearchTemplate elasticsearchTemplate; //es工具
/**
* 从es检索数据
*
* @param content 搜索关键字
* @param pageNum 页
* @param pageSzie 条
* @return
*/
public AggregatedPage<Idea> getIdeaListBySrt(String content, Integer pageNum, Integer pageSzie) {
Pageable pageable = PageRequest.of(pageNum, pageSzie);
String preTag = "<font color='#dd4b39'>";//google的色值
String postTag = "</font>";
SearchQuery searchQuery = new NativeSearchQueryBuilder().
withQuery(matchQuery("ideaTitle", content)).
withQuery(matchQuery("ideaContent", content)).
withHighlightFields(new HighlightBuilder.Field("ideaTitle").preTags(preTag).postTags(postTag),
new HighlightBuilder.Field("ideaContent").preTags(preTag).postTags(postTag)).build();
searchQuery.setPageable(pageable);
// 不需要高亮直接return ideas
// AggregatedPage<Idea> ideas = elasticsearchTemplate.queryForPage(searchQuery, Idea.class);
// 高亮字段
AggregatedPage<Idea> ideas = elasticsearchTemplate.queryForPage(searchQuery, Idea.class, new SearchResultMapper() {
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
List<Idea> chunk = new ArrayList<>();
for (SearchHit searchHit : response.getHits()) {
if (response.getHits().getHits().length <= 0) {
return null;
}
Idea idea = new Idea();
//name or memoe
HighlightField ideaTitle = searchHit.getHighlightFields().get("ideaTitle");
if (ideaTitle != null) {
idea.setIdeaTitle(ideaTitle.fragments()[0].toString());
}
HighlightField ideaContent = searchHit.getHighlightFields().get("ideaContent");
if (ideaContent != null) {
idea.setIdeaContent(ideaContent.fragments()[0].toString());
}
chunk.add(idea);
}
if (chunk.size() > 0) {
return new AggregatedPageImpl<>((List<T>) chunk);
}
return null;
}
});
return ideas;
}
其他基础接口直接使用 ideaRepository.
springboot ElasticSearch 简单的全文检索高亮的更多相关文章
- springboot elasticsearch 集成注意事项
文章来源: http://www.cnblogs.com/guozp/p/8686904.html 一 elasticsearch基础 这里假设各位已经简单了解过elasticsearch,并不对es ...
- springboot集成elk 一: springboot + Elasticsearch
1.ELK介绍 1> Elasticsearch是实时全文搜索和分析引擎, 提供搜集.分析.存储数据三大功能: 是一套开放REST和JAVA API等结构提供高效搜索功能,可扩展的分布式系统. ...
- NEST.net Client For Elasticsearch简单应用
NEST.net Client For Elasticsearch简单应用 由于最近的一个项目中的搜索部分要用到 Elasticsearch 来实现搜索功能,苦于英文差及该方面的系统性资料不好找,在实 ...
- springboot+thymeleaf简单使用
关于springboot想必很多人都在使用,由于公司项目一直使用的是SpringMVC,所以自己抽空体验了一下springboot的简单使用. 环境搭建 springbooot的环境搭建可以说很灵活, ...
- Elasticsearch简单使用和环境搭建
Elasticsearch简单使用和环境搭建 1 Elasticsearch简介 Elasticsearch是一个可用于构建搜索应用的成品软件,它最早由Shay Bannon创建并于2010年2月发布 ...
- Springboot接口简单实现生成MySQL插入语句
Springboot接口简单实现调用接口生成MySQL插入语句 在实际测试中,有这样一个需求场景,比如:在性能压力测试中,可能需要我们事先插入数据库中一些相关联的数据. 我们在实际测试中,遇到问题,需 ...
- SpringBoot 搭建简单聊天室
SpringBoot 搭建简单聊天室(queue 点对点) 1.引用 SpringBoot 搭建 WebSocket 链接 https://www.cnblogs.com/yi1036943655/p ...
- elasticsearch简单查询
elasticsearch简单查询示例: { "from": "0", //分页,从第一页开始 "size": "10" ...
- SpringBoot 发送简单邮件
使用SpringBoot 发送简单邮件 1. 在pom.xml中导入依赖 <!--邮件依赖--> <dependency> <groupId>org.springf ...
随机推荐
- idm技术下载百度网盘
原使用教程:https://www.haorenka.org/idm.html 使用idm下载器后实际下载速度,比原网盘客户端快很多 相关软件下载 参考上面的参考教程,这里只提供对应所需要的文件,有的 ...
- WXS----数据类型
- linux系统界面转换
普通使用的切换: 命令行->图形 startx 或者 ctrl+alt+F7切换到图形界面,虚拟机里面使用Alt+F7返回到图形界面 图形->命令行 Ctrl+Alt+F1--F6 如果想 ...
- [转帖]nginx sendfile tcp_nopush tcp_nodelay参数解释
nginx sendfile tcp_nopush tcp_nodelay参数解释 2013-06-25 13:59:40 zmj_88888888 阅读数 20425 文章标签: nginxtcp_ ...
- Linux中request_irq()中断申请与处理说明
1. 中断的理解 中断你可以理解为就是一种电信号,是由硬件设备产生的然后发送给处理器,处理器接收到中断后,就会马上向操作系统反映此信号,之后就是系统的工作了. 这里有两个注意的地方,第一中断是随时都 ...
- TZOJ3114: {A}∩{B}
#include<stdio.h> int main() { ],b[],m,i,j,c; scanf("%d",&t); while(t--) { c=; s ...
- hadoop 批量处理脚本编写
编写shell脚本就是解决批量处理 1. 在/usr/local/bin 创建脚本 并授权所有用户 chmod a+x xcall.sh xcall.sh 比如:删除/tmp/*所有文件 批量删 ...
- 开始Jupyter Notebooks
开始Jupyter Notebooks 安装Anaconda 因为不能有空格,所以没有选C:\Program Files 认识Jupyter Notebooks 修改 jupyter notebook ...
- ELK基础配置
前言 近期在研究日志系统的设计,感觉现在公司的子系统和接口太多了,日志看不过来,就想着有没有一种方法可以把各个程序的日志组合到一起.于是乎就搜到了ELK.开始对ELK的概念完全搞不懂,就照着各个平台文 ...
- springboot 的启动流程
1.我们springboot 项目的启动类如下. 方式1 @SpringBootApplicationpublic class SpringbootZkLockApplication { public ...