原文: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. app--Android 稳定性测试方案

    标准的崩溃日志:Android 应用一般使用 Java 语言开发,在不作特殊处理的情况下,遇到未处理的异常时,会弹框提示“程序遇到异常,即将退出”(有些手机框中提示语不同, 或不弹框).同时会将异常的 ...

  2. 【JS】实用/常用函数/Function方法

    1.获取日月 时分秒 //获取 月日 getMonth=(time)=>{ var date = new Date(time) <?):(date.getMonth()+) ?'+date ...

  3. STM32 MDK摘记

    题记:这人是越懒越懒,记性也也来越差,前段时间改了个链接文件,今天想用,竟然忘了咋写....还是勤记记吧... 随时更新,笔记帖. 不喜勿喷! 1,关于MDK链接文件宏的定义 #! armcc -E ...

  4. LeetCode 278. 第一个错误的版本(First Bad Version)

    278. 第一个错误的版本 LeetCode278. First Bad Version 题目描述 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每 ...

  5. gensim中word2vec

    from gensim.models import Word2Vec Word2Vec(self, sentences=None, size=100, alpha=0.025, window=5, m ...

  6. [转帖]进程上下文频繁切换导致load average过高

    进程上下文频繁切换导致load average过高 2016年6月26日admin发表评论阅读评论 http://www.361way.com/linux-context-switch/5131.ht ...

  7. [转帖]Linux运维工程师的十个基本技能点

    Linux运维工程师的十个基本技能点 https://cloud.tencent.com/developer/article/1115068   本人是Linux运维工程师,对这方面有点心得,现在我说 ...

  8. 【坑】SpringMvc 处理JSON 乱码

    文章目录 前言 方法 前言 在使用 springMvc 的时候,如果向前台返回 JSON 数据,JSON 中的中文会乱码: 即使你在配置了全局的信息编码拦截器,也无济于事: 原因大抵是,JSON 的内 ...

  9. 数据结构-单链表-类定义C++

    原理可访问https://www.cnblogs.com/yang901112/p/11674333.html 头文件 #ifndef RLIST_H #define RLIST_H #include ...

  10. AX 中临时表应用

    临时表,只要让表的Temporary属性设为yes就行. 今天写代码时发现,假如在一个循环里面把数据插入到临时表里, 假如没有在每次开始时没加clear的话,假如有个字段下一条没数据,会自动带到下一条 ...