这里代码使用的是springboot 2.2.2 版本
首先安装ES6.2.2 logstash,并且通过logstash把mysql的数据导入的es里面,然后开始下面的工作,
服务层的代码如下:
package com.ext.texs.service; import com.ext.texs.entity.LoginEs; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.MatchQueryBuilder; 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.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; import org.springframework.stereotype.Service; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; @Service public class EsService { @Autowired private ElasticsearchTemplate template; public AggregatedPage testForHigh() throws IOException { String preTag = "<font color='#dd4b39'>";//google的色值 String postTag = "</font>"; BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder() .must(new MatchQueryBuilder("name", "刘")); SearchQuery searchQuery = new NativeSearchQueryBuilder(). //总的查询 withQuery(boolQueryBuilder). //设置bool查询 withHighlightFields(new HighlightBuilder.Field("name").preTags(preTag).postTags(postTag)) //设置高亮效果 // .withHighlightFields(new HighlightBuilder.Field("pwd").preTags(preTag).postTags(postTag)) .build(); AggregatedPage<LoginEs> ideas = template.queryForPage(searchQuery, LoginEs.class, new SearchResultMapper() { @Override public <T> AggregatedPage<T> mapResults(SearchResponse searchResponse, Class<T> aClass, Pageable pageable) { List<LoginEs> list = new ArrayList<>(); for (SearchHit hit : searchResponse.getHits()) {//获取遍历查询结果 if (searchResponse.getHits().getHits().length <= 0) return null; LoginEs bean = new LoginEs(); Map map = hit.getSourceAsMap(); System.out.println(map); bean.setId(map.get("id").toString()); bean.setName(map.get("name").toString()); HighlightField name = hit.getHighlightFields().get("name"); if (name != null) { bean.setName(name.fragments()[0].toString()); //得到高亮的结果 } HighlightField pwd = hit.getHighlightFields().get("pwd"); if (pwd != null) { bean.setPwd(pwd.fragments()[0].toString()); } list.add(bean); } if (list.size() > 0) return new AggregatedPageImpl<>((List<T>) list); return null; } @Override public <T> T mapSearchHit(SearchHit searchHit, Class<T> aClass) { return null; } }); ideas.get().forEach(model -> { System.out.println(model); }); return ideas; } }
这个可以成功的得到高亮度的功能,这里做下记录,以后可以帮助查看。