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.

高亮写法借鉴代码地址点我
其他方式查询写法地址点我

转载于:https://www.cnblogs.com/shihaiming/p/11062809.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值