elasticsearch7.12 agg分组聚合分页同段同句查询

pom 引入依赖:
<!-- elasticsearch 7.16 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
package com.realize.framework.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author hzj
 * @Date 2022/1/6 13:33
 * @Desc ElasticSearch配置
 **/
@Configuration
public class ElasticSearchClientConfig {

    @Value("${spring.elasticsearch.ip}")
    private String ip;
    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost(ip, 9200, "http")));
        return restHighLevelClient;
    }

}

yml:

# ES 地址
spring
  elasticsearch:
    ip: 47.101.207.23

package com.realize.project.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.realize.common.constant.Constants;
import com.realize.common.constant.HttpStatus;
import com.realize.common.utils.StringUtils;
import com.realize.framework.convert.PdfConvertTxtComponent;
import com.realize.framework.redis.RedisCache;
import com.realize.framework.upLoad.AliYunOSSService;
import com.realize.framework.web.page.TableNoticeDataInfo;
import com.realize.project.system.domain.AshareMain;
import com.realize.project.system.domain.AshareOwnership;
import com.realize.project.system.domain.BusiPublicNotice;
import com.realize.project.system.domain.OssMetaGq;
import com.realize.project.system.domain.dto.EnumSecDto;
import com.realize.project.system.domain.vo.BusiPublicNoticeReqVo;
import com.realize.project.system.domain.vo.PublicNoticeClassifyReqVo;
import com.realize.project.system.domain.vo.PublicNoticeMapVo;
import com.realize.project.system.mapper.BusiPublicNoticeMapper;
import com.realize.project.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 股权激励公告Service业务层处理
 *
 * @author realize
 * @date 2022-04-15
 */
@Slf4j
@Service
public class BusiPublicNoticeServiceImpl implements IBusiPublicNoticeService
{

    //全局用-公告检索ES索引库
    private static final String clusterName="public_notice";
    @Autowired
    private BusiPublicNoticeMapper busiPublicNoticeMapper;
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    @Autowired
    private PdfConvertTxtComponent pdfConvertTxtComponent;
    @Autowired
    private AliYunOSSService aliyunOSSService;
    @Autowired
    private IEnumSecService enumSecService;
    @Autowired
    private IAshareMainService ashareMainService;
    @Autowired
    private IAshareOwnershipService ashareOwnershipService;

    @Autowired
    private IOssMetaGqService ossMetaGqService;
    @Autowired
    private RedisCache  redisCache;


    
    /**
     * ES 查询共用条件
     * @Description ES 公告检索
     * @param  publicNoticeReqVo
     * @return  List<Map<String,Object>> 公告检索结果集
     * @return
     */
    public SearchResponse commonConditions(BusiPublicNoticeReqVo publicNoticeReqVo){
        //创建索引请求
        SearchRequest searchRequest = new SearchRequest(clusterName);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        //按照省份分组
        AggregationBuilder aggregationBuilder = AggregationBuilders.terms("agg").field("region.keyword");
        searchSourceBuilder.aggregation(aggregationBuilder);
        //按照父行业/行业分组
        AggregationBuilder parentindustryAggregationBuilder = AggregationBuilders.terms("aggParentindustry").field("parentindustry.keyword");
        AggregationBuilder industryAggregationBuilder = AggregationBuilders.terms("aggIndustry").field("industry.keyword");
        parentindustryAggregationBuilder.subAggregation(industryAggregationBuilder);
        searchSourceBuilder.aggregation(parentindustryAggregationBuilder);
        //按照市场类型分组
        AggregationBuilder marketcodeAggregationBuilder = AggregationBuilders.terms("aggMarketcode").field("markettype.keyword");
        searchSourceBuilder.aggregation(marketcodeAggregationBuilder);
        //按照公告分类分组
        AggregationBuilder noticetypeAggregationBuilder = AggregationBuilders.terms("aggNoticetype").field("noticetype.keyword");
        searchSourceBuilder.aggregation(noticetypeAggregationBuilder);

        //初始化每个字段的属性
        RangeQueryBuilder publishdateQuery = null;
        MatchPhraseQueryBuilder theValidityTQ = null;//不分词
        TermsQueryBuilder termsQueryTQ=null;//会分词

        //1发布时间
        if (publicNoticeReqVo.getPublishdates()!=null && publicNoticeReqVo.getPublishdates().size()>0) {
            publishdateQuery = QueryBuilders.rangeQuery("publishdate")
                    .gte(publicNoticeReqVo.getPublishdates().get(0).getTime())
                    .lte(publicNoticeReqVo.getPublishdates().get(1).getTime());
            boolQueryBuilder.must(publishdateQuery);
        }
        //2证券代码
        if (StringUtils.isNotBlank(publicNoticeReqVo.getStockcode())) {
            //matchPhraseQuery不分词
            theValidityTQ = QueryBuilders.matchPhraseQuery("stockcode",publicNoticeReqVo.getStockcode());
            boolQueryBuilder.must(theValidityTQ);
        }
        //市场类型
        if (StringUtils.isNotBlank(publicNoticeReqVo.getMarketcode())) {
            //matchPhraseQuery不分词
            theValidityTQ = QueryBuilders.matchPhraseQuery("marketcode",publicNoticeReqVo.getMarketcode());
            boolQueryBuilder.must(theValidityTQ);
        }
        //公司性质
        if (StringUtils.isNotBlank(publicNoticeReqVo.getOwnership())) {
            //matchPhraseQuery不分词
            theValidityTQ = QueryBuilders.matchPhraseQuery("ownershipcode",publicNoticeReqVo.getOwnership());
            boolQueryBuilder.must(theValidityTQ);
        }
        //公告类型-查询股权激励tab页数据
        if (StringUtils.isNotBlank(publicNoticeReqVo.getNoticecode())) {
            //matchPhraseQuery不分词
            theValidityTQ = QueryBuilders.matchPhraseQuery("noticecode",publicNoticeReqVo.getNoticecode());
            boolQueryBuilder.must(theValidityTQ);
        }
        //业务类型-查询股权激励tab页数据
        if (StringUtils.isNotBlank(publicNoticeReqVo.getCustomcode())) {
            //matchPhraseQuery不分词
            theValidityTQ = QueryBuilders.matchPhraseQuery("customcode",publicNoticeReqVo.getCustomcode());
            boolQueryBuilder.must(theValidityTQ);
        }

        List<PublicNoticeClassifyReqVo> classifyArr = publicNoticeReqVo.getClassifyArr();
        if(CollectionUtils.isNotEmpty(classifyArr)){
            List<String> parentIndustry0 = classifyArr.stream().filter(b -> b.getParentIndustryPlus()!=null && b.getParentIndustryPlus() == 0).map(b->b.getParentIndustry()).collect(Collectors.toList());
            List<String> parentIndustry1 = classifyArr.stream().filter(b -> b.getParentIndustryPlus()!=null && b.getParentIndustryPlus() == 1).map(b->b.getParentIndustry()).collect(Collectors.toList());
            //父行业分类 加减查询;
           if(CollectionUtils.isNotEmpty(parentIndustry0)){
                termsQueryTQ = QueryBuilders.termsQuery("parentindustry.keyword", parentIndustry0);
                boolQueryBuilder.mustNot(termsQueryTQ);
            }
           if(CollectionUtils.isNotEmpty(parentIndustry1)){
                termsQueryTQ = QueryBuilders.termsQuery("parentindustry.keyword", parentIndustry1);
                boolQueryBuilder.must(termsQueryTQ);
            }
            //行业分类 加减查询;
            List<String> industry0 = classifyArr.stream().filter(b -> b.getIndustryPlus() != null && b.getIndustryPlus() == 0).map(b->b.getIndustry()).collect(Collectors.toList());
            List<String> industry1 = classifyArr.stream().filter(b -> b.getIndustryPlus() != null && b.getIndustryPlus() == 1).map(b->b.getIndustry()).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(industry0)){
                termsQueryTQ = QueryBuilders.termsQuery("industry.keyword", industry0);
                boolQueryBuilder.mustNot(termsQueryTQ);
            }
            if(CollectionUtils.isNotEmpty(industry1)){
                termsQueryTQ = QueryBuilders.termsQuery("industry.keyword", industry1);
                boolQueryBuilder.must(termsQueryTQ);
            }
            //区域分布 加减查询;
            List<String> region0 = classifyArr.stream().filter(b -> b.getRegionPlus() !=null && b.getRegionPlus() == 0).map(b->b.getRegion()).collect(Collectors.toList());
            List<String> region1 = classifyArr.stream().filter(b -> b.getRegionPlus() !=null && b.getRegionPlus() == 1).map(b->b.getRegion()).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(region0)){
                termsQueryTQ = QueryBuilders.termsQuery("region.keyword", region0);
                boolQueryBuilder.mustNot(termsQueryTQ);
            }
            if(CollectionUtils.isNotEmpty(region1)){
                termsQueryTQ = QueryBuilders.termsQuery("region.keyword", region1);
                boolQueryBuilder.must(termsQueryTQ);
            }
            //市场类型 加减查询;
            List<String> marketType0 = classifyArr.stream().filter(b -> b.getMarketTypePlus()!=null && b.getMarketTypePlus() == 0).map(b->b.getMarketType()).collect(Collectors.toList());
            List<String> marketType1 = classifyArr.stream().filter(b -> b.getMarketTypePlus()!=null && b.getMarketTypePlus() == 1).map(b->b.getMarketType()).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(marketType0)){
                termsQueryTQ = QueryBuilders.termsQuery("markettype.keyword", marketType0);
                boolQueryBuilder.mustNot(termsQueryTQ);
            }
            if(CollectionUtils.isNotEmpty(marketType1)){
                termsQueryTQ = QueryBuilders.termsQuery("markettype.keyword", marketType1);
                boolQueryBuilder.must(termsQueryTQ);
            }
            //公告类型 加减查询;
            List<String> noticeType0 = classifyArr.stream().filter(b -> b.getNoticeTypePlus()!=null && b.getNoticeTypePlus() == 0).map(b->b.getNoticeType()).collect(Collectors.toList());
            List<String> noticeType1 = classifyArr.stream().filter(b -> b.getNoticeTypePlus()!=null && b.getNoticeTypePlus() == 1).map(b->b.getNoticeType()).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(noticeType0)){
                termsQueryTQ = QueryBuilders.termsQuery("noticetype.keyword", noticeType0);
                boolQueryBuilder.mustNot(termsQueryTQ);
            }
            if(CollectionUtils.isNotEmpty(noticeType1)){
                termsQueryTQ = QueryBuilders.termsQuery("noticetype.keyword", noticeType1);
                boolQueryBuilder.must(termsQueryTQ);
            }
        }

        //3-1标题-包含全部关键字 使用should需要再套一个bool  must=and 和 should=or
        if (StringUtils.isNotBlank(publicNoticeReqVo.getTitle())) {
            String[] strings = publicNoticeReqVo.getTitle().split(" ");//输入多个关键字 以空格区分
            if(publicNoticeReqVo.getScope()!=null&&publicNoticeReqVo.getScope()==1) {//0同篇;不限制范围
                BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();
                for (int i = 0; i < strings.length; i++) {
                    boolQueryBuilder.must(boolQueryBuilder1.must(QueryBuilders.matchPhraseQuery("title",strings[i])));
                }
            }else {//跨度查询 同段同句;限制范围
                SpanNearQueryBuilder queryBuilderSpanNearQueryInclude = QueryBuilders.spanNearQuery(QueryBuilders.spanTermQuery("title", strings[0]), 10000);
                for (int i = 1; i < strings.length; i++) {
                    queryBuilderSpanNearQueryInclude.addClause(QueryBuilders.spanTermQuery("title", strings[i]));
                }
                SpanTermQueryBuilder queryBuilderSpanNearQueryExclude=null;
                if(publicNoticeReqVo.getScope()!=null&&publicNoticeReqVo.getScope()==2){//同段搜素类似,相邻搜索不能有: 对应分隔符变为 \n,或者  同段搜素类似,对应分隔符变为\n,或者<p>,</p>
                    queryBuilderSpanNearQueryExclude = QueryBuilders.spanTermQuery("title", "\n/<p>");
                }else if(publicNoticeReqVo.getScope()!=null&&publicNoticeReqVo.getScope()==3){//同句搜素类似,相邻搜索不能有: 对应分隔符变为 。 、?、!
                    queryBuilderSpanNearQueryExclude = QueryBuilders.spanTermQuery("title", "。/?/!");
                }else if(publicNoticeReqVo.getScope()!=null&&publicNoticeReqVo.getScope()==4){
                    queryBuilderSpanNearQueryExclude = QueryBuilders.spanTermQuery("title", "。/?/!/不构成/不包含");
                }
                //QueryBuilder queryBuilderSpanNotQuery= QueryBuilders.spanNotQuery(queryBuilderSpanNearQueryInclude,queryBuilderSpanNearQueryExclude); //.include().exclude();

            }
        }

        //3-2标题-包含任一全部关键字 使用should需要再套一个bool  must=and 和 should=or
        if (StringUtils.isNotBlank(publicNoticeReqVo.getContainAnyTitle())) {
            BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();
            String[] strings = publicNoticeReqVo.getContainAnyTitle().split(" ");    //输入多个关键字 以空格区分
            for (int i = 0; i < strings.length; i++) {
                boolQueryBuilder.must(boolQueryBuilder1.should(QueryBuilders.matchPhraseQuery("title",strings[i])));
            }
        }



       //3-3标题-不包含任一全部关键字 使用should需要再套一个bool  must=and 和 should=or
        if (StringUtils.isNotBlank(publicNoticeReqVo.getNoContainAnyTitle())) {
            BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();
            String[] strings = publicNoticeReqVo.getNoContainAnyTitle().split(" ");   //输入多个关键字 以空格区分
            for (int i = 0; i < strings.length; i++) {
                boolQueryBuilder.must(boolQueryBuilder1.mustNot(QueryBuilders.matchPhraseQuery("title",strings[i])));
            }
        }



        //4-1章节 包含全部关键字 使用should需要再套一个bool
        if (StringUtils.isNotBlank(publicNoticeReqVo.getChapter())) {
            BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();
            String[] split = publicNoticeReqVo.getChapter().split(" ");    //输入多个关键字 以空格区分
            for (int i = 0; i < split.length; i++) {
                boolQueryBuilder.must(boolQueryBuilder1.must(QueryBuilders.matchPhraseQuery("parseChapter",split[i])));
            }
        }
        //4-2章节 包含任一全部关键字 使用should需要再套一个bool
        if (StringUtils.isNotBlank(publicNoticeReqVo.getContainAnyChapter())) {
            BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();
            String[] split = publicNoticeReqVo.getContainAnyChapter().split(" ");    //输入多个关键字 以空格区分
            //使用should需要再套一个bool
            for (int i = 0; i < split.length; i++) {
                boolQueryBuilder.must(boolQueryBuilder1.should(QueryBuilders.matchPhraseQuery("parseChapter",split[i])));
            }
        }
        //4-3章节 不包含任一全部关键字 使用should需要再套一个bool
        if (StringUtils.isNotBlank(publicNoticeReqVo.getNoContainAnyChapter())) {
            BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();
            String[] split = publicNoticeReqVo.getNoContainAnyChapter().split(" ");    //输入多个关键字 以空格区分
            //使用should需要再套一个bool
            for (int i = 0; i < split.length; i++) {
                boolQueryBuilder.must(boolQueryBuilder1.mustNot(QueryBuilders.matchPhraseQuery("parseChapter",split[i])));
            }
        }

        //5正文 包含全部关键字 使用should需要再套一个bool
        QueryBuilder queryBuilderSpanNotQuery=null;
        if (StringUtils.isNotBlank(publicNoticeReqVo.getFullText())) {
            String[] split = publicNoticeReqVo.getFullText().split(" ");    //输入多个关键字 以空格区分
            if(publicNoticeReqVo.getScope()!=null&&publicNoticeReqVo.getScope()==1) {//0同篇;不限制范围
                BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();
                for (int i = 0; i < split.length; i++) {
                    boolQueryBuilder.must(boolQueryBuilder1.must(QueryBuilders.matchPhraseQuery("parseContent",split[i])));
                }
            }else {//跨度查询 同段同句;限制范围
                SpanNearQueryBuilder queryBuilderSpanNearQueryInclude = QueryBuilders.spanNearQuery(QueryBuilders.spanTermQuery("parseContent", split[0]), 10000);
                for (int i = 1; i < split.length; i++) {
                    queryBuilderSpanNearQueryInclude.addClause(QueryBuilders.spanTermQuery("parseContent", split[i]));
                }
                SpanTermQueryBuilder queryBuilderSpanNearQueryExclude=null;
                if(publicNoticeReqVo.getScope()!=null&&publicNoticeReqVo.getScope()==2){//同段搜素类似,相邻搜索不能有:对应分隔符变为 \n,或者  ,
                    queryBuilderSpanNearQueryExclude = QueryBuilders.spanTermQuery("parseContent", "\n/,/,");
                }else if(publicNoticeReqVo.getScope()!=null&&publicNoticeReqVo.getScope()==3){//同句搜素类似,相邻搜索不能有:对应分隔符变为 。 、?、!
                    queryBuilderSpanNearQueryExclude = QueryBuilders.spanTermQuery("parseContent", "。/?/!");
                }else if(publicNoticeReqVo.getScope()!=null&&publicNoticeReqVo.getScope()==4){
                    queryBuilderSpanNearQueryExclude = QueryBuilders.spanTermQuery("parseContent", "。/?/!/不构成/不包含");
                }
                 queryBuilderSpanNotQuery= QueryBuilders.spanNotQuery(queryBuilderSpanNearQueryInclude,queryBuilderSpanNearQueryExclude); //.include().exclude();
                //boolQueryBuilder.must(queryBuilderSpanNotQuery);
            }
        }
        //5正文 包含任一全部关键字 使用should需要再套一个bool
        if (StringUtils.isNotBlank(publicNoticeReqVo.getContainAnyFullText())) {
            BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();
            String[] split = publicNoticeReqVo.getContainAnyFullText().split(" ");   //输入多个关键字 以空格区分
            for (int i = 0; i < split.length; i++) {
                boolQueryBuilder.must(boolQueryBuilder1.should(QueryBuilders.matchPhraseQuery("parseContent",split[i])));
            }
        }
        //5正文 不包含任一全部关键字 使用should需要再套一个bool
        if (StringUtils.isNotBlank(publicNoticeReqVo.getNoContainAnyFullText())) {
            BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();
            String[] split = publicNoticeReqVo.getNoContainAnyFullText().split(" ");    //输入多个关键字 以空格区分
            for (int i = 0; i < split.length; i++) {
                boolQueryBuilder.must(boolQueryBuilder1.mustNot(QueryBuilders.matchPhraseQuery("parseContent",split[i])));
            }
        }


        //全文高亮 标题高亮
        if (StringUtils.isNotBlank(publicNoticeReqVo.getTitle()) || StringUtils.isNotBlank(publicNoticeReqVo.getContainAnyTitle())||StringUtils.isNotBlank(publicNoticeReqVo.getNoContainAnyTitle())
                || StringUtils.isNotBlank(publicNoticeReqVo.getFullText())) {
            //实例化高亮构建器
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            //需要高亮的字段(可多个)
            highlightBuilder.field("title").field("parseContent");
            highlightBuilder.requireFieldMatch(true);
            //最大高亮分片数,没有这个配置就会导致全文显示不全问题
            highlightBuilder.fragmentSize(800000);
            //从第一个分片获取高亮片段,没有这个配置就会导致全文显示不全问题
            highlightBuilder.numOfFragments(0);
            //前缀
            highlightBuilder.preTags("<span style='font-weight: bolder;color:red'>");
            //后缀
            highlightBuilder.postTags("</span>");
            //添加查询构建器中,形成查询
            searchSourceBuilder.highlighter(highlightBuilder);
        }
        Integer pageNum = publicNoticeReqVo.getPageNum();
        Integer pageSize = publicNoticeReqVo.getPageSize();
        //分页
        if(pageNum<=0){
            pageNum = 1;
        }
        if(pageSize<=0){
            pageSize=10;
        }
        //起始记录下标
        int from = (pageNum-1) * pageSize;
        searchSourceBuilder.from(from);
        searchSourceBuilder.size(pageSize);
        //排序规则:
        if(StringUtils.isNotBlank(publicNoticeReqVo.getSortField())){//公司聚合
             FieldSortBuilder fieldSortBuilder1 = new FieldSortBuilder("stockticker.keyword").order(SortOrder.ASC);
             searchSourceBuilder.sort(fieldSortBuilder1);
             FieldSortBuilder fieldSortBuilder2 = new FieldSortBuilder("publishdate").order(SortOrder.DESC);
             searchSourceBuilder.sort(fieldSortBuilder2);
        }else{//最近,最早;
             FieldSortBuilder fieldSortBuilder = new FieldSortBuilder("publishdate").order(publicNoticeReqVo.getSortDirection()!=null && publicNoticeReqVo.getSortDirection()==0?SortOrder.ASC:SortOrder.DESC);
             searchSourceBuilder.sort(fieldSortBuilder);
        }


        //执行查询
        if(queryBuilderSpanNotQuery!=null){
            searchSourceBuilder.query(queryBuilderSpanNotQuery);
        }else{
            searchSourceBuilder.query(boolQueryBuilder);
        }
        //打印
        System.out.println("打印方便核查的ES检索条件:   " + searchSourceBuilder.toString());
        //不能超过60秒
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        //限制查询最大数
        searchSourceBuilder.trackTotalHits(true);
        //检索
        searchRequest.source(searchSourceBuilder);
        //客户端调用检索语法
        SearchResponse searchResponse = null;
        try {
            searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return searchResponse;
    }

    /**
     * @Description ES 公告检索
     * @param  publicNoticeReqVo
     * @return  List<Map<String,Object>> 公告检索结果集
     **/
    @Override
    public TableNoticeDataInfo getEsBusiPublicNotice(BusiPublicNoticeReqVo publicNoticeReqVo) {
        SearchResponse searchResponse = this.commonConditions(publicNoticeReqVo);
        //获取响应结果
        SearchHits hits = searchResponse.getHits();
        //记录查询后总数
        Long totalHits = hits.getTotalHits().value;
//        System.out.println("命中数:" + totalHits);
        SearchHit[] hitss = searchResponse.getHits().getHits();
//        System.out.println("命中:" + hitss.length);
        //数据结果集初始化
        List<Map<String, Object>> list = new ArrayList<>();
        //循环存入结果集
        for (SearchHit hit : searchResponse.getHits().getHits()) {
            //理因判断不为空才让加前后缀
            if (StringUtils.isNotBlank(publicNoticeReqVo.getTitle()) || StringUtils.isNotBlank(publicNoticeReqVo.getContainAnyTitle())||StringUtils.isNotBlank(publicNoticeReqVo.getNoContainAnyTitle())
                    ||StringUtils.isNotBlank(publicNoticeReqVo.getFullText())) {
                //设置标题高亮
                Map<String, HighlightField> titlehighlightFields = hit.getHighlightFields();
                HighlightField titleHight = titlehighlightFields.get("title");
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String contentInfo = (String) sourceAsMap.get("title");
                //标题-高亮词替换
                if (titleHight != null ){
                    Text[] fragments = titleHight.fragments();
                    StringBuilder newtitle = new StringBuilder();
                    for (Text text:fragments) {
                        newtitle.append(text);
                    }
                    sourceAsMap.replace("title", newtitle);
                }
                //设置全文高亮
                Map<String, HighlightField> fullTexthighlightFields = hit.getHighlightFields();
                HighlightField fullTextHight = fullTexthighlightFields.get("parseContent");
                //全文-高亮词替换
                if (fullTextHight != null ){
                    Text[] fragments = fullTextHight.getFragments();
                    StringBuilder newfullText = new StringBuilder();
                    for (Text text:fragments) {
                        newfullText.append(text);
                    }
//                    String strc = contentInfo.replace(fullText,preTags+ fullText +postTags);
//                    sourceAsMap.put("contentInfo", strc);
                    sourceAsMap.replace("contentInfo", newfullText);
                }
                list.add(sourceAsMap);
            } else {
                //否则直接添加进结果集,不需要高亮
                list.add(hit.getSourceAsMap());
            }
        }
        //System.out.println("结果集长度:" + list.size());
        //获取聚合结果集
        //按照省份分组
        List<PublicNoticeMapVo> listRegionsMap=new ArrayList();
        Terms terms = searchResponse.getAggregations().get("agg");
        for (Terms.Bucket entry : terms.getBuckets()) {
            PublicNoticeMapVo publicNoticeMapVo=new PublicNoticeMapVo();
            publicNoticeMapVo.setName(entry.getKey().toString());
            publicNoticeMapVo.setCount(entry.getDocCount());
            listRegionsMap.add(publicNoticeMapVo);
        }

        //按照父行业分组
        List<PublicNoticeMapVo> listIndustryMap=new ArrayList();
        Terms terms2 = searchResponse.getAggregations().get("aggParentindustry");
        for (Terms.Bucket entry : terms2.getBuckets()) {
            PublicNoticeMapVo publicNoticeMapVo=new PublicNoticeMapVo();
            publicNoticeMapVo.setName(entry.getKey().toString());
            publicNoticeMapVo.setCount(entry.getDocCount());
            List<PublicNoticeMapVo> listSonIndustryMap=new ArrayList();
            Terms towTerms = (Terms) entry.getAggregations().asMap().get("aggIndustry");
            for (Terms.Bucket entryTwo : towTerms.getBuckets()) {
                PublicNoticeMapVo publicNoticeMapSonVo=new PublicNoticeMapVo();
                publicNoticeMapSonVo.setName(entryTwo.getKey().toString());
                publicNoticeMapSonVo.setCount(entryTwo.getDocCount());
                listSonIndustryMap.add(publicNoticeMapSonVo);
            }
            publicNoticeMapVo.setSonArray(listSonIndustryMap);
            listIndustryMap.add(publicNoticeMapVo);
        }

        //按照市场类型分组
        List<PublicNoticeMapVo> listMarketcodesMap=new ArrayList();
        Terms terms3 = searchResponse.getAggregations().get("aggMarketcode");
        for (Terms.Bucket entry : terms3.getBuckets()) {
            PublicNoticeMapVo publicNoticeMapVo=new PublicNoticeMapVo();
            publicNoticeMapVo.setName(entry.getKey().toString());
            publicNoticeMapVo.setCount(entry.getDocCount());
            listMarketcodesMap.add(publicNoticeMapVo);
        }
        //按照公告类型分组
        List<PublicNoticeMapVo> listNoticetypeMap=new ArrayList();
        Terms terms4 = searchResponse.getAggregations().get("aggNoticetype");
        for (Terms.Bucket entry : terms4.getBuckets()) {
            PublicNoticeMapVo publicNoticeMapVo=new PublicNoticeMapVo();
            publicNoticeMapVo.setName(entry.getKey().toString());
            publicNoticeMapVo.setCount(entry.getDocCount());
            listNoticetypeMap.add(publicNoticeMapVo);
        }
        //获取响应结果
        //SearchHits hits = searchResponse.getHits();
        //Long totalHits = hits.getTotalHits().value;
        TableNoticeDataInfo rspData = new TableNoticeDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(totalHits);

        rspData.setMapRegions(listRegionsMap);
        rspData.setMapParentIndustrycodes(listIndustryMap);
        rspData.setMapMarketcodes(listMarketcodesMap);
        rspData.setMapNoticetypes(listNoticetypeMap);
        return rspData;
    }

    /**
     * @Description ES 公告检索
     * @param  publicNoticeReqVo
     * @return  List<Map<String,Object>> 公告检索结果集
     * @return  int  命中条数
     **/
    @Override
    public Long getEsTotal(BusiPublicNoticeReqVo publicNoticeReqVo) {
        SearchResponse searchResponse = this.commonConditions(publicNoticeReqVo);
        //获取响应结果
        SearchHits hits = searchResponse.getHits();
        Long totalHits = hits.getTotalHits().value;
        //记录总数
//        System.out.println("命中数:"+totalHits);
        //返回总记录数
        return totalHits;
    }

    @Override
    public void insertBatchBusiPublicNotice(List<BusiPublicNotice> noticeList) {
        busiPublicNoticeMapper.insertBatchBusiPublicNotice(noticeList);
    }

    @Override
    public int updateAnnouncementOSSUrl(BusiPublicNotice busiPublicNoticeDto) {
        return busiPublicNoticeMapper.updateAnnouncementOSSUrl(busiPublicNoticeDto);
    }

    /**
     * 前端显示 地域分布 分组
     * @param publicNoticeReqVo
     * @return
     */
    public  Map<String, Long> getTermsAggByRegion(BusiPublicNoticeReqVo publicNoticeReqVo){
        Map<String, Long> groupMap = new HashMap<>();
        //按照省份分组
        SearchResponse searchResponse  = this.commonConditions(publicNoticeReqVo);
        Terms terms = searchResponse.getAggregations().get("agg");
        for (Terms.Bucket entry : terms.getBuckets()) {
            groupMap.put(entry.getKey().toString(), entry.getDocCount());
        }

        Map<String, Long> groupMap2 = new HashMap<>();
        //按照父行业分组
        Terms terms2 = searchResponse.getAggregations().get("aggParentindustry");
        for (Terms.Bucket entry : terms2.getBuckets()) {
            groupMap2.put(entry.getKey().toString(), entry.getDocCount());
        }

        Map<String, Long> groupMap3 = new HashMap<>();
        //按照市场类型分组
        Terms terms3 = searchResponse.getAggregations().get("aggMarketcode");
        for (Terms.Bucket entry : terms3.getBuckets()) {
            groupMap3.put(entry.getKey().toString(), entry.getDocCount());
        }
        return groupMap;
    }



    @Override
    public Map<String, Long> getTermsAggByParentIndustrycode(BusiPublicNoticeReqVo publicNoticeReqVo) {
        Map<String, Long> groupMap = new HashMap<>();
        //按照省份分组
        //客户端调用检索语法
        SearchResponse searchResponse  = this.commonConditions(publicNoticeReqVo);
        Terms terms = searchResponse.getAggregations().get("aggParentindustry");
        for (Terms.Bucket entry : terms.getBuckets()) {
            groupMap.put(entry.getKey().toString(), entry.getDocCount());
        }
        return groupMap;
    }

    @Override
    public Map<String, Long> getTermsAggByIndustrycode(BusiPublicNoticeReqVo publicNoticeReqVo) {
        Map<String, Long> groupMap = new HashMap<>();
        //按照省份分组
        //客户端调用检索语法
        SearchResponse searchResponse  = this.commonConditions(publicNoticeReqVo);
        Terms terms = searchResponse.getAggregations().get("aggIndustry");
        for (Terms.Bucket entry : terms.getBuckets()) {
            groupMap.put(entry.getKey().toString(), entry.getDocCount());
        }
        return groupMap;
    }

    @Override
    public Map<String, Long> getTermsAggByMarketcode(BusiPublicNoticeReqVo publicNoticeReqVo) {
        Map<String, Long> groupMap = new HashMap<>();
        //按照省份分组
        //客户端调用检索语法
        SearchResponse searchResponse  = this.commonConditions(publicNoticeReqVo);
        Terms terms = searchResponse.getAggregations().get("aggMarketcode");
        for (Terms.Bucket entry : terms.getBuckets()) {
            groupMap.put(entry.getKey().toString(), entry.getDocCount());
        }
        return groupMap;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值