原文: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 简单的全文检索高亮的更多相关文章

  1. springboot elasticsearch 集成注意事项

    文章来源: http://www.cnblogs.com/guozp/p/8686904.html 一 elasticsearch基础 这里假设各位已经简单了解过elasticsearch,并不对es ...

  2. springboot集成elk 一: springboot + Elasticsearch

    1.ELK介绍 1> Elasticsearch是实时全文搜索和分析引擎, 提供搜集.分析.存储数据三大功能: 是一套开放REST和JAVA API等结构提供高效搜索功能,可扩展的分布式系统. ...

  3. NEST.net Client For Elasticsearch简单应用

    NEST.net Client For Elasticsearch简单应用 由于最近的一个项目中的搜索部分要用到 Elasticsearch 来实现搜索功能,苦于英文差及该方面的系统性资料不好找,在实 ...

  4. springboot+thymeleaf简单使用

    关于springboot想必很多人都在使用,由于公司项目一直使用的是SpringMVC,所以自己抽空体验了一下springboot的简单使用. 环境搭建 springbooot的环境搭建可以说很灵活, ...

  5. Elasticsearch简单使用和环境搭建

    Elasticsearch简单使用和环境搭建 1 Elasticsearch简介 Elasticsearch是一个可用于构建搜索应用的成品软件,它最早由Shay Bannon创建并于2010年2月发布 ...

  6. Springboot接口简单实现生成MySQL插入语句

    Springboot接口简单实现调用接口生成MySQL插入语句 在实际测试中,有这样一个需求场景,比如:在性能压力测试中,可能需要我们事先插入数据库中一些相关联的数据. 我们在实际测试中,遇到问题,需 ...

  7. SpringBoot 搭建简单聊天室

    SpringBoot 搭建简单聊天室(queue 点对点) 1.引用 SpringBoot 搭建 WebSocket 链接 https://www.cnblogs.com/yi1036943655/p ...

  8. elasticsearch简单查询

    elasticsearch简单查询示例: { "from": "0", //分页,从第一页开始 "size": "10" ...

  9. SpringBoot 发送简单邮件

    使用SpringBoot 发送简单邮件 1. 在pom.xml中导入依赖 <!--邮件依赖--> <dependency> <groupId>org.springf ...

随机推荐

  1. [转]gcc的__builtin_函数介绍

    链接地址:https://blog.csdn.net/jasonchen_gbd/article/details/44948523

  2. 如何固定table表格宽度,样式不受容器影响

    之前有篇关于LODOP打印超文本表格,两个样式相同的表格,出现错位的情况.该博文地址:LODOP打印表格错位的几种情况该文试验了两种现象,第一种浏览器页面显示错位,打印预览也错位,第二种浏览器页面表格 ...

  3. Linq中demo,用力看看吧

    本文导读:LINQ to SQL全称基于关系数据的.NET语言集成查询,用于以对象形式管理关系数据,并提供了丰富的查询功能.Linq中where查询与SQL命令中的Where作用相似,都是起到范围限定 ...

  4. angular2-cookie 如何升级到 ngx-cookie

    angular2-cookie 如何升级到  ngx-cookie https://github.com/salemdar/angular2-cookie#readme

  5. ggplot绘图之基本语法

    ggplot绘图之基本语法 2018年09月03日 22:29:56 一个人旅行*-* 阅读数 4332更多 分类专栏: R语言   1.ggplot2图形之基本语法: ggplot2的核心理念是将绘 ...

  6. 【GStreamer开发】GStreamer播放教程06——可视化音频

    目标 GStreamer有一系列把音频转换成视频的element.他们可以用于科学的目的或者增加音乐播放器的趣味性.本教程展示: 如何允许音频的可视化 如何选择可视化element 介绍 在playb ...

  7. svn查看登录过的账号密码

    直接下载:http://www.leapbeyond.com/ric/TSvnPD/

  8. 什么是netty--通俗易懂

      一.Netty介绍 1.什么是netty Netty 是由 JBOSS 提供的一个 Java 开源框架.Netty 提供异步的.基于事件驱动的网络应用程序框架,用以快速开发高性能.高可靠性的网络 ...

  9. Maven学习存档(2)——settings.xml配置

    二.settings.xml配置 2.1 原文 <?xml version="1.0" encoding="UTF-8"?> <!-- Lic ...

  10. Java基础IO类之数据流

    DataInputStream: 数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本java数据类型.应用程序可以使用数据输出流 写入稍后由数据输入流读取的数据.DataInputStrea ...