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 ...
随机推荐
- Xray写POC插件
漏洞环境 https://www.vulnhub.com/ https://github.com/vulhub/vulhub https://github.com/QAX-A-Team/Weblogi ...
- 将linux的root用户的家目录由/root切换为/home/root
步骤1,先以root登录,然后创建目录/home/root步骤2,vi /etc/passwd里root用户的家目录为/home/root:步骤3,cp -rf /root/ /home/将原来的ro ...
- conda 创建tensorflow虚拟环境后,无法import tensorflow在jupyter
ensorflow安装好了,在python中也测试过了,但在jupyter中却会报错,原因是先装的Anaconda 之后装的tensorflow,所以环境有问题,所以需要重装jupyter,先激活te ...
- ubuntu16.04 下Mongo数据库搭建
一 数据库安装 1.下载自己需要的数据库版本和系统,下载地址:https://www.mongodb.com/download-center/community . 本文下载的为ubuntu 16.0 ...
- Django 在admin中自定义app名
前提条件,注册时是: 完整的注册,不是直接用app名进行注册 INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'dj ...
- PAT甲级题分类汇编——排序
本文为PAT甲级分类汇编系列文章. 排序题,就是以排序算法为主的题.纯排序,用 std::sort 就能解决的那种,20分都算不上,只能放在乙级,甲级的排序题要么是排序的规则复杂,要么是排完序还要做点 ...
- closed channel
func Test_chanel(t *testing.T) { c := make(chan int, 1) go func() { time.Sleep(time.Second * 3) clos ...
- hadoop 节点退役和服役
节点的服役和退役(hdfs)---------------------- 黑白名单的组合情况-------------------------include //dfs.includeexclude ...
- 【数论】小A进学校
小A进学校 题目描述 近日,清华大学挖出来一个明清古墓.小A决定冒充考古系科研人员去盗墓.他遇到的第一个难关是来自校门口保安的质疑,因为小没有清华学生证,所以保安决定通过问问题的方式验证小A的身份. ...
- Kubernetes 学习笔记(一):基础概念
个人笔记,仅本人查阅使用,不保证正确. 零.微服务 微服务架构专注于应用解耦合,通过将应用彻底地组件化和服务化,每个微服务只包含一个非常小的功能,比如权限管理.日志收集等等.由这一组微服务组合起来,提 ...