springboot ElasticSearch 简单的全文检索高亮
生活随笔
收集整理的這篇文章主要介紹了
springboot ElasticSearch 简单的全文检索高亮
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
前陣子和張三豐聊天提到了es。這次正好有機(jī)會(huì)學(xué)習(xí)并使用
首先引入依賴(lài)
<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然后 創(chuàng)建接口并繼承ElasticsearchRepository
idea 類(lèi)繼承 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中 引用
@Autowiredprivate IdeaRepository ideaRepository; //esjap類(lèi)@Autowiredprivate ElasticsearchTemplate elasticsearchTemplate; //es工具使用save方法把數(shù)據(jù)保存到es中
業(yè)務(wù)代碼忽略... 保存就完事了
全文檢索并高亮數(shù)據(jù)
這里注意分頁(yè)的頁(yè)數(shù)是從0開(kāi)始... 搞得我以為沒(méi)查到數(shù)據(jù)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;@Autowiredprivate IdeaRepository ideaRepository; //esjap類(lèi)@Autowiredprivate ElasticsearchTemplate elasticsearchTemplate; //es工具/*** 從es檢索數(shù)據(jù)** @param content 搜索關(guān)鍵字* @param pageNum 頁(yè)* @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() {@Overridepublic <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 memoeHighlightField 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;}其他基礎(chǔ)接口直接使用 ideaRepository.
高亮寫(xiě)法借鑒代碼地址點(diǎn)我
其他方式查詢(xún)寫(xiě)法地址點(diǎn)我
總結(jié)
以上是生活随笔為你收集整理的springboot ElasticSearch 简单的全文检索高亮的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【本周面试题】第5周 - 开发工具相关
- 下一篇: 排序算法之快速排序详解