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 ...
随机推荐
- ecshop添加商品筛选功能
ecshop商品属性一直是使用问题的难点,而“属性筛选”更是ecshop属性中的难点,那么下面来详细说明一下 属性筛选功能 第一,属性筛选的特点: 属性筛选必须是分类页才会显示,列出所有商品的唯一属性 ...
- Python机器学习基础教程-第2章-监督学习之线性模型
前言 本系列教程基本就是摘抄<Python机器学习基础教程>中的例子内容. 为了便于跟踪和学习,本系列教程在Github上提供了jupyter notebook 版本: Github仓库: ...
- flask 密钥问题
RuntimeError RuntimeError: The session is unavailable because no secret key was set. Set the secret_ ...
- Rsync同步部署web服务端配置
Rsync同步部署web服务端配置 1,参数详解: -v, --verbose 详细模式输出. -q, --quiet 精简输出模式. -c, --checksum 打开校验开关,强制对文件传输进行校 ...
- PHP 菠菜木马代码
<?php error_reporting(E_ERROR);@ini_set('display_errors', 'Off');@ini_set('max_execution_time', 2 ...
- Qt 编译出错“undefined reference to `vtable for”
1. 有时,如果将某个类改为继承自QObject类(以前不继承自该类),编译时会出错. 解决: clean Project, run qmake, rebulid都运行一遍,好了! 因为qmake生成 ...
- [转帖]“腾百万”之后,腾讯的云操作系统VStation单集群调度达10万台
“腾百万”之后,腾讯的云操作系统VStation单集群调度达10万台 https://www.leiphone.com/news/201909/4BsKCJtvvUCEb66c.html 腾讯有超过1 ...
- Oracle 11g xe版本---总结1
一.创建用户和授予权限 1.1 环境: Oracle 11g xe 第三方图形客户端: PLSQL Windows 10 必须登录 HR 用户,下面的查询会使用到 HR 中的表. 1.2 SQL 语句 ...
- 2.33模型--去除字符串两头空格.c
[注:本程序验证是使用vs2013版] #include <stdio.h> #include <stdlib.h> #include <string.h> #pr ...
- 【Linux】Shell批量修改文件名
修改文件名,替换中间字符: 例如:ABC_define_EFG.jpg,要把中间的define替换成argument: 用如下脚本即可: for var in *; do mv "$var& ...