springboot + ES 数据的高量显示

这里代码使用的是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;
    }

}

 

 

这个可以成功的得到高亮度的功能,这里做下记录,以后可以帮助查看。

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值