示例代码

@Override
    @Transactional
    public void batchAddEsProductRelationGoods(ProductRelationGoodsDTO productRelationGoodsDTO) {
        log.info("批量同步数据到es-开始");
        if (ObjectUtils.isEmpty(productRelationGoodsDTO.getFlag()) || (!productRelationGoodsDTO.getFlag())) {
            if (CollectionUtils.isEmpty(productRelationGoodsDTO.getRelationIds())) {
                throw new ServiceException(Code.Code_ParamError, ServiceException.Level.ERROR);
            }
        }
        //查询商品关系信息
        List<ProductRelationGoods> productRelationGoods = productRelationGoodsMapper.queryProductRelationGoodsByCondition(productRelationGoodsDTO);
        if (CollectionUtils.isEmpty(productRelationGoods)) return;
        List<String> productIds = productRelationGoods.stream().flatMap(v -> Arrays.asList(v.getProductId(), v.getResourceProductId()).stream()).distinct().collect(Collectors.toList());
        ProductParam productParam = new ProductParam();
        productParam.setProductIds(productIds);
        productParam.setCommodityType(CommodityType.Routine_Merchandise);
        //查询商品信息
        List<Product> products = productMapper.queryProductByCondition(productParam);
        if (CollectionUtils.isEmpty(products)) return;
        Map<String, Product> map = products.stream().collect(Collectors.toMap(Product::getProductId, v -> v, (v1, v2) -> v1));
        List<String> classIds = products.stream().map(Product::getClassId).distinct().collect(Collectors.toList());
        //查询科目信息
        ClassSubjectDTO classSubjectDTO = new ClassSubjectDTO();
        classSubjectDTO.setClassIds(classIds);
        List<ClassSubject> classSubjects = classSubjectMapper.queryClassSubjectByCondition(classSubjectDTO);
        if (CollectionUtils.isEmpty(classSubjects)) return;
        Map<String, ClassSubject> subjectMap = classSubjects.stream().collect(Collectors.toMap(k -> k.getClassId() + "_" + k.getType(), v -> v, (v1, v2) -> v1));
        //查询包装信息
        ProductParamDTO paramDTO = new ProductParamDTO();
        paramDTO.setClassIds(classIds);
        List<PackingUnitDTO> packingUnitDTOS = supplierPackingMapper.getPackageListByCondition(paramDTO);
        Map<String, List<PackingUnitDTO>> packagingInfoMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(packingUnitDTOS)) {
            packagingInfoMap = packingUnitDTOS.stream().collect(Collectors.groupingBy(PackingUnitDTO::getClassId));
        }
        //校验商品是否设置基本计量单位下单可用属性
        List<BaseUnitAvailableDTO> baseUnitAvailableDTOS = orderApiService.baseUnitAvailableQueryByParam(new BaseUnitAvailableDTO());
        Map<String, BaseUnitAvailableDTO> baseUnitAvailableMap = null;
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(baseUnitAvailableDTOS)) {
            baseUnitAvailableMap = baseUnitAvailableDTOS.stream().collect(Collectors.toMap(BaseUnitAvailableDTO::getProductId, k -> k, (v1, v2) -> v1));
        }
        //查询限制的包装规格
        List<BasePolicyGoodsStintDTO> basePolicyGoodsStintDTOList = productRelationGoods.stream().map(ProductRelationGoods::getEnterpriseId).distinct().map(v -> {
            BasePolicyGoodsStintDTO basePolicyGoodsStintDTO = new BasePolicyGoodsStintDTO();
            basePolicyGoodsStintDTO.setCustomerId(v);
            return basePolicyGoodsStintDTO;
        }).collect(Collectors.toList());
        List<BasePolicyGoodsStintDTO> restrictionPackList = policyOutBroLoanApiService.selectPackInfoByIdsAll(basePolicyGoodsStintDTOList);
        List<String> restrictionPack = null;
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(restrictionPackList) && org.apache.commons.collections.CollectionUtils.isNotEmpty(packingUnitDTOS)) {
            restrictionPack = restrictionPackList.stream().map(o ->o.getEnterpriseId() + "_" + o.getCustomerId() + "_" + o.getProductId() + "_" + o.getPackingName() + "_" + o.getPackageValue()).collect(Collectors.toList());
        }
        //查询es数据
        EsParamDTO searchParam = new EsParamDTO();
        searchParam.setRelationIds(productRelationGoodsDTO.getRelationIds());
        searchParam.setEnterpriseId(productRelationGoodsDTO.getEnterpriseId());
        List<EsProductRelationGoods> responseVO = new ArrayList<>();
        int i = 0;
        do {
            i++;
            searchParam.setCurrentPage(i);
            searchParam.setPageSize(10000);
            SearchRequest searchRequest = new SearchRequest(new String[]{indexName}, buildDsl(searchParam));
            SearchResponse response = null;
            try {
                response = this.restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            SearchResponseVO searchResponseVO = parseResultForPage(response);
            if (CollectionUtils.isEmpty(searchResponseVO.getData())) break;
            responseVO.addAll(searchResponseVO.getData());
            log.info("查询es条数responseVO:{},i:{}", responseVO.size(), i);
        } while (responseVO.size() % 10000 == 0);
        log.info("查询es总条数responseVO:{}", responseVO.size());
        Map<Long, EsProductRelationGoods> map1 = null;
        Map<String, EsProductRelationGoods> map2 = null;
        if (!CollectionUtils.isEmpty(responseVO)) {
            map1 = responseVO.stream().filter(v -> ObjectUtils.isEmpty(v.getEsPackagingInfo())).collect(Collectors.toMap(EsProductRelationGoods::getProductRelationGoodsId, v -> v, (v1, v2) -> v1));
            map2 = responseVO.stream().filter(v -> !ObjectUtils.isEmpty(v.getEsPackagingInfo())).collect(Collectors.toMap(k -> k.getProductRelationGoodsId() + "_" + k.getEsPackagingInfo().getPackingInfoId(), v -> v, (v1, v2) -> v1));
        }
        List<EsProductRelationGoods> esProductRelationGoodsListSave = new ArrayList<>();
        List<EsProductRelationGoods> esProductRelationGoodsListUpdate = new ArrayList<>();
        for (ProductRelationGoods relationGoods : productRelationGoods) {
            EsProductRelationGoods esProductRelationGoods = mapper.map(relationGoods, EsProductRelationGoods.class);
            Product productInfo = map.get(relationGoods.getProductId());
            if (ObjectUtils.isEmpty(productInfo)) continue;
            productInfo.setProductName(productInfo.getClassName() + productInfo.getBrandName() + productInfo.getGradeName() + productInfo.getStardName() + productInfo.getModelName() + productInfo.getColorName());
            Product resourceProductInfo = map.get(relationGoods.getResourceProductId());
//            log.info("resourceProductId:{}",relationGoods.getProductId());
            if (ObjectUtils.isEmpty(resourceProductInfo)) continue;
            resourceProductInfo.setProductName(resourceProductInfo.getClassName() + resourceProductInfo.getBrandName() + resourceProductInfo.getGradeName() + resourceProductInfo.getStardName() + resourceProductInfo.getModelName() + resourceProductInfo.getColorName());
            ClassSubject classSubject = subjectMap.get((productInfo.getClassId() + "_" + relationGoods.getType()));
//            log.info("classSubject:{}",productInfo.getClassId() + "_" + relationGoods.getType());
            if (ObjectUtils.isEmpty(classSubject)) continue;
            esProductRelationGoods.setId(UUIDUtils.random().toString());
            esProductRelationGoods.setProductInfo(productInfo);
            esProductRelationGoods.setResourceProductInfo(resourceProductInfo);
            esProductRelationGoods.setClassSubject(classSubject);
            esProductRelationGoods.setProductAndSupplier(productInfo.getProductId() + "_" + esProductRelationGoods.getMaterialSrc() + "_" + esProductRelationGoods.getResourceCompanyId());
            esProductRelationGoods.setProductAndPackage(productInfo.getProductId());
            if ((CollectionUtils.isEmpty(map1)) || (ObjectUtils.isEmpty(map1.get(relationGoods.getProductRelationGoodsId())))) {
                if((!CollectionUtils.isEmpty(baseUnitAvailableMap)) && (!ObjectUtils.isEmpty(baseUnitAvailableMap.get(resourceProductInfo.getProductId())))){
                    esProductRelationGoods.setOrderAvailableFlag(StatusFlag.Enable.getValue());
                }else {
                    esProductRelationGoods.setOrderAvailableFlag(StatusFlag.DisEnable.getValue());
                }
                esProductRelationGoodsListSave.add(esProductRelationGoods);
//                log.info("esProductRelationGoodsList条数:{}",esProductRelationGoodsList.size());
                if (esProductRelationGoodsListSave.size() % 10000 == 0) {
                    log.info("被整除-新增:{}", esProductRelationGoodsListSave.size());
                    saveBatch(esProductRelationGoodsListSave);
                    esProductRelationGoodsListSave.clear();
                }
            }else {
                EsProductRelationGoods esProductRelationGoodsUpate = map1.get(relationGoods.getProductRelationGoodsId());
                String id = esProductRelationGoodsUpate.getId();
                mapper.map(esProductRelationGoods,esProductRelationGoodsUpate);
                esProductRelationGoodsUpate.setId(id);
                if((!CollectionUtils.isEmpty(baseUnitAvailableMap)) && (!ObjectUtils.isEmpty(baseUnitAvailableMap.get(resourceProductInfo.getProductId())))){
                    esProductRelationGoods.setOrderAvailableFlag(StatusFlag.Enable.getValue());
                }else {
                    esProductRelationGoods.setOrderAvailableFlag(StatusFlag.DisEnable.getValue());
                }
                esProductRelationGoodsListUpdate.add(esProductRelationGoodsUpate);
//                log.info("esProductRelationGoodsList条数:{}",esProductRelationGoodsList.size());
                if (esProductRelationGoodsListUpdate.size() % 10000 == 0) {
                    log.info("被整除-修改:{}", esProductRelationGoodsListUpdate.size());
                    Map<String, EsProductRelationGoods> collect = esProductRelationGoodsListUpdate.stream().collect(Collectors.toMap(EsProductRelationGoods::getId, v -> v, (v1, v2) -> v1));
                    batchUpdate(collect);
                    esProductRelationGoodsListUpdate.clear();
                }
            }
            if (CollectionUtils.isEmpty(packagingInfoMap)) continue;
            List<PackingUnitDTO> packingDTOList = new ArrayList<>();
            if (MaterialSrcType.SYNCHRONIZATION.getKey() == relationGoods.getMaterialSrc() && SupplierType.COMMON.ordinal() == relationGoods.getSupplierType()) {//有链接
                List<PackingUnitDTO> supplierPackingUnitList = packagingInfoMap.get(resourceProductInfo.getClassId());
                if (!CollectionUtils.isEmpty(supplierPackingUnitList)) {
                    packingDTOList.addAll(supplierPackingUnitList);
                }
            } else {
                List<PackingUnitDTO> supplierPackingUnitList = packagingInfoMap.get(productInfo.getClassId());
                if (!CollectionUtils.isEmpty(supplierPackingUnitList)) {
                    List<PackingUnitDTO> packingUnitDTOList = supplierPackingUnitList.stream().filter(v -> relationGoods.getResourceCompanyId().compareTo(v.getSupplierCompanyId()) == 0).collect(Collectors.toList());
                    packingDTOList.addAll(packingUnitDTOList);
                }
            }
            if (CollectionUtils.isEmpty(packingDTOList)) continue;
//            log.info("包装条数:{}",packingDTOList.size());
            for (PackingUnitDTO packingUnitDTO : packingDTOList) {
                EsProductRelationGoods esRelationGoods = mapper.map(esProductRelationGoods, EsProductRelationGoods.class);
                EsPackagingInfo esPackagingInfo = mapper.map(packingUnitDTO, EsPackagingInfo.class);
                esPackagingInfo.setStatus((int)packingUnitDTO.getStatusFlag().getValue());
                esRelationGoods.setEsPackagingInfo(esPackagingInfo);
                esRelationGoods.setProductAndPackage(productInfo.getProductId() + "_" + packingUnitDTO.getPackingName() + "_" + packingUnitDTO.getPackageValue());
                if(CollectionUtils.isEmpty(restrictionPack) || (!restrictionPack.contains(esRelationGoods.getResourceCompanyId() + "_" + esRelationGoods.getEnterpriseId() + "_" + resourceProductInfo.getProductId() + "_" + packingUnitDTO.getPackingName() + "_" + packingUnitDTO.getPackageValue()))){
                    esRelationGoods.setOrderAvailableFlag(StatusFlag.Enable.getValue());
                }else {
                    esRelationGoods.setOrderAvailableFlag(StatusFlag.DisEnable.getValue());
                }
                if ((CollectionUtils.isEmpty(map2)) || (ObjectUtils.isEmpty(map2.get(relationGoods.getProductRelationGoodsId() + "_" + packingUnitDTO.getPackingInfoId())))) {
                    esRelationGoods.setId(UUIDUtils.random().toString());
                    esProductRelationGoodsListSave.add(esRelationGoods);
//                    log.info("esProductRelationGoodsList条数:{}",esProductRelationGoodsList.size());
                    if (esProductRelationGoodsListSave.size() % 10000 == 0) {
                        log.info("被整除-新增:{}", esProductRelationGoodsListSave.size());
                        saveBatch(esProductRelationGoodsListSave);
                        esProductRelationGoodsListSave.clear();
                    }
                }else {
                    EsProductRelationGoods esProductRelationGoodsUpate = map2.get(relationGoods.getProductRelationGoodsId() + "_" + packingUnitDTO.getPackingInfoId());
                    esRelationGoods.setId(esProductRelationGoodsUpate.getId());
                    esProductRelationGoodsListUpdate.add(esRelationGoods);
//                log.info("esProductRelationGoodsList条数:{}",esProductRelationGoodsList.size());
                    if (esProductRelationGoodsListUpdate.size() % 10000 == 0) {
                        log.info("被整除-修改:{}", esProductRelationGoodsListUpdate.size());
                        Map<String, EsProductRelationGoods> collect = esProductRelationGoodsListUpdate.stream().collect(Collectors.toMap(EsProductRelationGoods::getId, v -> v, (v1, v2) -> v1));
                        batchUpdate(collect);
                        esProductRelationGoodsListUpdate.clear();
                    }
                }
            }
        }
        if (!CollectionUtils.isEmpty(esProductRelationGoodsListSave)){
            log.info("esProductRelationGoodsListSave:{}", esProductRelationGoodsListSave.size());
            saveBatch(esProductRelationGoodsListSave);
        }
        if (!CollectionUtils.isEmpty(esProductRelationGoodsListUpdate)){
            log.info("esProductRelationGoodsListUpdate:{}", esProductRelationGoodsListUpdate.size());
            Map<String, EsProductRelationGoods> collect = esProductRelationGoodsListUpdate.stream().collect(Collectors.toMap(EsProductRelationGoods::getId, v -> v, (v1, v2) -> v1));
            batchUpdate(collect);
        }
        log.info("批量同步数据到es-结束");
    }

    @Override
    public SearchResponseVO search(EsParamDTO searchParam) {
        log.info("调用es查询内部货品计量单位-入参:{}", JSON.toJSONString(searchParam));
        try {
            if (ObjectUtils.isEmpty(searchParam.getEnterpriseId()))
                searchParam.setEnterpriseId(UserThreadLocal.getCid());
            SearchRequest searchRequest = new SearchRequest(new String[]{indexName}, buildDsl(searchParam));
            SearchResponse response = this.restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            SearchResponseVO searchResponseVO = parseResultForPage(response);
            log.info("调用es查询内部货品计量单位-结果:{}", JSON.toJSONString(searchResponseVO));
            return searchResponseVO;
        } catch (IOException e) {
            log.info("Elasticsearch数据查询失败,searchParam={}", searchParam, e);
        }
        return null;
    }

    /**
     * 根据用户发送的参数构建查询条件
     *
     * @return
     */
    @Override
    public SearchSourceBuilder buildDsl(EsParamDTO searchParam) {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // 构建查询条件和过滤条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        sourceBuilder.query(boolQueryBuilder);
        // 构建商品匹配过滤
        if (StringUtils.isNotBlank(searchParam.getProductName())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("productInfo.productName", searchParam.getProductName()).operator(Operator.AND));
        }
        // 构建商品关系主键ID过滤
        if (!ObjectUtils.isEmpty(searchParam.getRelationId())) {
            boolQueryBuilder.filter(QueryBuilders.termQuery("productRelationGoodsId", searchParam.getRelationId().toString()));
        }
        // 构建商品关系主键ID过滤
        if (!CollectionUtils.isEmpty(searchParam.getRelationIds())) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("productRelationGoodsId", searchParam.getRelationIds()));
        }
        // 构建商品下单可用过滤
        if (!ObjectUtils.isEmpty(searchParam.getOrderAvailableFlag())) {
            boolQueryBuilder.filter(QueryBuilders.termQuery("orderAvailableFlag", searchParam.getOrderAvailableFlag().getValue()));
        }
        boolQueryBuilder.must(QueryBuilders.existsQuery("productInfo"));
        boolQueryBuilder.must(QueryBuilders.existsQuery("resourceProductInfo"));
        boolQueryBuilder.must(QueryBuilders.existsQuery("classSubject"));
        if ((!ObjectUtils.isEmpty(searchParam.getExcludeClassify())) && searchParam.getExcludeClassify()) {
            boolQueryBuilder.mustNot(QueryBuilders.existsQuery("productInfo.classify"));
        }
        // 构建商品ID过滤
        if (StringUtils.isNotBlank(searchParam.getProductId())) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("productInfo.productId.keyword", searchParam.getProductId()));
        }
        // 构建商品ID过滤
        if (!CollectionUtils.isEmpty(searchParam.getProductIds())) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("productInfo.productId.keyword", searchParam.getProductIds()));
        }
        // 构建商品上架状态过滤
        if (!ObjectUtils.isEmpty(searchParam.getOnline())) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("productInfo.online", String.valueOf(searchParam.getOnline().getKey())));
        }
        // 构建商品停启用状态过滤
        if (StatusFlag.Enable.equals(searchParam.getStatus())) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("productInfo.status", String.valueOf(searchParam.getStatus().getValue())));
            boolQueryBuilder.filter(QueryBuilders.termsQuery("status", searchParam.getStatus().getValue() + ""));
            boolQueryBuilder.filter(QueryBuilders.termsQuery("classSubject.status", searchParam.getStatus().getValue() + ""));
            BoolQueryBuilder packageStatusBuilder = QueryBuilders.boolQuery();
            packageStatusBuilder.should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("esPackagingInfo")));
            BoolQueryBuilder packageBuilder2 = QueryBuilders.boolQuery();
            packageBuilder2.must(QueryBuilders.termQuery("esPackagingInfo.status", StatusFlag.Enable.getValue()+""));
            if (!ObjectUtils.isEmpty(searchParam.getPackageEnterpriseId())) {
                packageBuilder2.must(QueryBuilders.termsQuery("esPackagingInfo.enterpriseId", searchParam.getPackageEnterpriseId().toString()));
            }
            packageStatusBuilder.should(packageBuilder2);
            boolQueryBuilder.must(packageStatusBuilder);
        }
        // 构建商品code过滤
        if (StringUtils.isNotBlank(searchParam.getProductCode())) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("productInfo.productCode", searchParam.getProductCode()));
        }
        // 构建品类过滤
        if (StringUtils.isNotBlank(searchParam.getClassId())) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("productInfo.classId.keyword", searchParam.getClassId()));
        }
        // 构建品类过滤
        if (!CollectionUtils.isEmpty(searchParam.getClassIds())) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("productInfo.classId.keyword", searchParam.getClassIds()));
        }
        // 构建品类名称查询
        if (StringUtils.isNotBlank(searchParam.getClassName())) {
            boolQueryBuilder.must(QueryBuilders.termsQuery("productInfo.className", searchParam.getClassName()));
        }
        // 构建商品类型过滤
        if (ObjectUtils.isEmpty(searchParam.getCommodityType()))
            searchParam.setCommodityType(CommodityType.Routine_Merchandise);
        boolQueryBuilder.filter(QueryBuilders.termsQuery("productInfo.commodityType", String.valueOf(searchParam.getCommodityType().getKey())));
        // 构建来源商品ID过滤
        if (StringUtils.isNotBlank(searchParam.getResourceProductId())) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("productInfo.resourceProductId.keyword", searchParam.getResourceProductId()));
        }
        // 构建商品ID过滤
        if (!CollectionUtils.isEmpty(searchParam.getResourceProductIds())) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("resourceProductInfo.productId.keyword", searchParam.getResourceProductIds()));
        }
        // 构建供应商ID过滤
        if (!ObjectUtils.isEmpty(searchParam.getSupplierId())) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("resourceCompanyId", searchParam.getSupplierId().toString()));
        }
        // 构建供应商名称匹配过滤
        if (StringUtils.isNotBlank(searchParam.getSupplierName())) {
            boolQueryBuilder.filter(QueryBuilders.matchQuery("companyName", searchParam.getSupplierName()).operator(Operator.AND));
        }
        // 构建包装信息是否存在过滤
        if (!ObjectUtils.isEmpty(searchParam.getPackageExistFlag())) {
            if (searchParam.getPackageExistFlag()) {
                boolQueryBuilder.must(QueryBuilders.existsQuery("esPackagingInfo"));
            } else {
                boolQueryBuilder.mustNot(QueryBuilders.existsQuery("esPackagingInfo"));
            }
        }
        // 构建包装名称过滤
        if (StringUtils.isNotBlank(searchParam.getPackingName())) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("esPackagingInfo.packingName", searchParam.getPackingName()));
        }
        // 构建包装值过滤
        if (!ObjectUtils.isEmpty(searchParam.getPackageValue())) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("esPackagingInfo.packageValue", searchParam.getPackageValue()));
        }
        // 构建商品类型过滤
        if (!ObjectUtils.isEmpty(searchParam.getType())) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("type", searchParam.getType().getKey() + ""));
        }
        // 构建商品来源类型过滤
        if (!ObjectUtils.isEmpty(searchParam.getMaterialSrc())) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("materialSrc", searchParam.getMaterialSrc().getKey() + ""));
        }
        // 构建科目ID过滤
        if (!ObjectUtils.isEmpty(searchParam.getSubjectId())) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("classSubject.subjectId", searchParam.getSubjectId().toString()));
        }
        // 构建科目ID集合过滤
        if (!CollectionUtils.isEmpty(searchParam.getFilterSubjectIds())) {
            boolQueryBuilder.mustNot(QueryBuilders.termsQuery("classSubject.subjectId", searchParam.getFilterSubjectIds()));
        }
        // 构建公司过滤
        if (!ObjectUtils.isEmpty(searchParam.getEnterpriseId())) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("enterpriseId", searchParam.getEnterpriseId().toString()));
        }
        // 构建失效过滤
        boolQueryBuilder.filter(QueryBuilders.termsQuery("productInfo.isValid", ValidFlag.Valid.getValue() + ""));
        boolQueryBuilder.filter(QueryBuilders.termsQuery("resourceProductInfo.isValid", ValidFlag.Valid.getValue() + ""));
        boolQueryBuilder.filter(QueryBuilders.termsQuery("deleteFlag", DeleteFlag.NotDeleted.getValue() + ""));
        boolQueryBuilder.filter(QueryBuilders.termsQuery("classSubject.deleteFlag", DeleteFlag.NotDeleted.getValue() + ""));
        // 构建排序
        sourceBuilder.sort("createDate", SortOrder.DESC);
        // 构建时间区间
        Date startDate = searchParam.getStartDate();
        Date endDate = searchParam.getEndDate();
        if ((!ObjectUtils.isEmpty(startDate)) && (!ObjectUtils.isEmpty(endDate))) { // 判断起始和终止时间是否为null
            RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("createDate");
            rangeQuery.gte(startDate);
            rangeQuery.lte(endDate);
            boolQueryBuilder.filter(rangeQuery);
        }
        if(ObjectUtils.isEmpty(searchParam.getGroupType()))searchParam.setGroupType(GroupTypeEnum.Default);
        switch (searchParam.getGroupType()) {
            case GroupByProduct:
                sourceBuilder.size(0);
                //分组
                AggregationBuilder topScoreAggBuilderByPro = AggregationBuilders.topHits("top").sort("createDate", SortOrder.DESC).size(1);
                AggregationBuilder groupAggBuilderByPro = AggregationBuilders.terms("group_aggs").field("productInfo.productId.keyword").size(Integer.MAX_VALUE).subAggregation(topScoreAggBuilderByPro);
                groupAggBuilderByPro.subAggregation(AggregationBuilders.max("max_createDate").field("createDate"));
                // 聚合分页
                if ((!ObjectUtils.isEmpty(searchParam.getCurrentPage())) && (!ObjectUtils.isEmpty(searchParam.getPageSize()))) {
                    groupAggBuilderByPro.subAggregation(new BucketSortPipelineAggregationBuilder("bucket_field", Arrays.asList(new FieldSortBuilder("max_createDate").order(SortOrder.DESC))).from((searchParam.getCurrentPage() - 1) * searchParam.getPageSize()).size(searchParam.getPageSize()));
                }
                sourceBuilder.aggregation(groupAggBuilderByPro);
                //分组总条数
                TermsAggregationBuilder aggsTotal = AggregationBuilders.terms("group_aggs_total").field("productInfo.productId.keyword").size(Integer.MAX_VALUE);
                sourceBuilder.aggregation(aggsTotal);
                break;
            case GroupByProductAndSupplier:
                BoolQueryBuilder queryBuilderByProAndSup = QueryBuilders.boolQuery();
                queryBuilderByProAndSup.should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("esPackagingInfo")));
                queryBuilderByProAndSup.should(QueryBuilders.termQuery("esPackagingInfo.sort", ProductLevel.ONE.ordinal()));
                boolQueryBuilder.must(queryBuilderByProAndSup);
                sourceBuilder.size(0);
                //分组
                AggregationBuilder topScoreAggBuilderByProAndSup = AggregationBuilders.topHits("top").sort("createDate", SortOrder.DESC).size(1);
                AggregationBuilder groupAggBuilderByProAndSup = AggregationBuilders.terms("group_aggs").field("productAndSupplier.keyword").size(Integer.MAX_VALUE).subAggregation(topScoreAggBuilderByProAndSup);
                groupAggBuilderByProAndSup.subAggregation(AggregationBuilders.max("max_createDate").field("createDate"));
                // 聚合分页
                if ((!ObjectUtils.isEmpty(searchParam.getCurrentPage())) && (!ObjectUtils.isEmpty(searchParam.getPageSize()))) {
                    groupAggBuilderByProAndSup.subAggregation(new BucketSortPipelineAggregationBuilder("bucket_field", Arrays.asList(new FieldSortBuilder("max_createDate").order(SortOrder.DESC))).from((searchParam.getCurrentPage() - 1) * searchParam.getPageSize()).size(searchParam.getPageSize()));
                }
                sourceBuilder.aggregation(groupAggBuilderByProAndSup);
                //分组总条数
                TermsAggregationBuilder termsAggregationBuilder1 = AggregationBuilders.terms("group_aggs_total").field("productAndSupplier.keyword").size(Integer.MAX_VALUE);
                sourceBuilder.aggregation(termsAggregationBuilder1);
                break;
            case GroupByProductAndPackage:
                BoolQueryBuilder queryBuilderByProAndPac = QueryBuilders.boolQuery();
                queryBuilderByProAndPac.should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("esPackagingInfo")));
                queryBuilderByProAndPac.should(QueryBuilders.termQuery("esPackagingInfo.sort", ProductLevel.ONE.ordinal()));
                boolQueryBuilder.must(queryBuilderByProAndPac);
                sourceBuilder.size(0);
                //分组
                AggregationBuilder topScoreAggBuilderByProAndPac = AggregationBuilders.topHits("top").sort("createDate", SortOrder.DESC).size(1);
                AggregationBuilder groupAggBuilderByProAndPac = AggregationBuilders.terms("group_aggs").field("productAndPackage.keyword").size(Integer.MAX_VALUE).subAggregation(topScoreAggBuilderByProAndPac);
                groupAggBuilderByProAndPac.subAggregation(AggregationBuilders.max("max_createDate").field("createDate"));
                // 聚合分页
                if ((!ObjectUtils.isEmpty(searchParam.getCurrentPage())) && (!ObjectUtils.isEmpty(searchParam.getPageSize()))) {
                    groupAggBuilderByProAndPac.subAggregation(new BucketSortPipelineAggregationBuilder("bucket_field", Arrays.asList(new FieldSortBuilder("max_createDate").order(SortOrder.DESC))).from((searchParam.getCurrentPage() - 1) * searchParam.getPageSize()).size(searchParam.getPageSize()));
                }
                sourceBuilder.aggregation(groupAggBuilderByProAndPac);
                //分组总条数
                TermsAggregationBuilder termsAggregationBuilderByProAndPac = AggregationBuilders.terms("group_aggs_total").field("productAndPackage.keyword").size(Integer.MAX_VALUE);
                sourceBuilder.aggregation(termsAggregationBuilderByProAndPac);
                break;
            case GroupByCustomer:
                sourceBuilder.size(0);
                //分组
                AggregationBuilder topScoreAggBuilderByCus = AggregationBuilders.topHits("top").size(1);
                AggregationBuilder groupAggBuilderByCus = AggregationBuilders.terms("group_aggs").field("resourceCompanyId").subAggregation(topScoreAggBuilderByCus).size(Integer.MAX_VALUE);
                // 聚合分页
                if ((!ObjectUtils.isEmpty(searchParam.getCurrentPage())) && (!ObjectUtils.isEmpty(searchParam.getPageSize()))) {
                    groupAggBuilderByCus.subAggregation(new BucketSortPipelineAggregationBuilder("bucket_field", null).from((searchParam.getCurrentPage() - 1) * searchParam.getPageSize()).size(searchParam.getPageSize()));
                }
                sourceBuilder.aggregation(groupAggBuilderByCus);
                //分组总条数
                TermsAggregationBuilder termsAggregationBuilderByCus = AggregationBuilders.terms("group_aggs_total").field("resourceCompanyId").size(Integer.MAX_VALUE);
                sourceBuilder.aggregation(termsAggregationBuilderByCus);
                break;
            default:
                // 构建分页
                if ((!ObjectUtils.isEmpty(searchParam.getCurrentPage())) && (!ObjectUtils.isEmpty(searchParam.getPageSize()))) {
                    Integer pageNum = searchParam.getCurrentPage();
                    Integer pageSize = searchParam.getPageSize();
                    sourceBuilder.from((pageNum - 1) * pageSize);
                    sourceBuilder.size(pageSize);
                } else {
                    sourceBuilder.size(10000);
                }
        }
        return sourceBuilder;
    }

    /**
     * 解析结果集
     *
     * @param response
     * @return
     */
    private SearchResponseVO parseResultForPage(SearchResponse response) {
        SearchResponseVO searchResponseVO = new SearchResponseVO();
        SearchHits hits = response.getHits();
        // 解析hits获取查询记录
        SearchHit[] hitsHits = hits.getHits();
        //明细总条数
        searchResponseVO.setTotal((int) hits.getTotalHits().value);
        List<EsProductRelationGoods> data = new ArrayList<>();
        for (SearchHit hitsHit : hitsHits) {
            String result = hitsHit.getSourceAsString();
            // 对result进行反序列化
            EsProductRelationGoods esProductRelationGoods = JSON.parseObject(result, EsProductRelationGoods.class);
            data.add(esProductRelationGoods);
        }
        //明细数据
        searchResponseVO.setData(data);
        List<EsProductRelationGoods> aggregateResults = new ArrayList<>();
        List<String> aggregateResults2 = new ArrayList<>();
        if (Objects.nonNull(response.getAggregations()) && Objects.equals(response.status(), RestStatus.OK)) {
            Terms groupResult = response.getAggregations().get("group_aggs");
            if (Objects.nonNull(groupResult)) {
                for (Terms.Bucket groupBucket : groupResult.getBuckets()) {
                    TopHits topScoreResult = groupBucket.getAggregations().get("top");
                    if (Objects.nonNull(topScoreResult) && topScoreResult.getHits().getHits().length > 0) {
                        SearchHit searchHit = topScoreResult.getHits().getAt(0);
                        EsProductRelationGoods esProductRelationGoods = JSON.parseObject(searchHit.getSourceAsString(), EsProductRelationGoods.class);
                        aggregateResults.add(esProductRelationGoods);
                    }
                }
            }
            Terms groupResult2 = response.getAggregations().get("group_aggs_total");
            if (Objects.nonNull(groupResult2)) {
                searchResponseVO.setAggsTotal(groupResult2.getBuckets().size());
            }
//            ParsedCardinality parsedCardinality = response.getAggregations().get("aggs_total");
//            if (Objects.nonNull(parsedCardinality)) {
//                //聚合总条数
//                searchResponseVO.setAggsTotal((int) parsedCardinality.getValue());
//            }
        }
        log.info("aggregateResults集合总条数:{}", aggregateResults.size());
        log.info("aggregateResults2集合总条数:{}", aggregateResults2.size());
        //聚合数据
        searchResponseVO.setAggsResults(aggregateResults);
        return searchResponseVO;
    }

    /**
     * 解析结果集
     *
     * @param response
     * @return
     */
    private List<EsProductRelationGoods> parseResultForList(SearchResponse response) {
        SearchHits hits = response.getHits();
        // 解析hits获取查询记录
        SearchHit[] hitsHits = hits.getHits();
        List<EsProductRelationGoods> esProductRelationGoodsList = new ArrayList<>();
        for (SearchHit hitsHit : hitsHits) {
            String result = hitsHit.getSourceAsString();
            // 对result进行反序列化
            EsProductRelationGoods esProductRelationGoods = JSON.parseObject(result, EsProductRelationGoods.class);
            esProductRelationGoodsList.add(esProductRelationGoods);
        }
        return esProductRelationGoodsList;
    }

es查询语句

{
    "size":0,
    "query":{
        "bool":{
            "must":[
                {
                    "exists":{
                        "field":"productInfo",
                        "boost":"1.0"
                    }
                },
                {
                    "exists":{
                        "field":"resourceProductInfo",
                        "boost":"1.0"
                    }
                },
                {
                    "exists":{
                        "field":"classSubject",
                        "boost":"1.0"
                    }
                },
                {
                    "bool":{
                        "should":[
                            {
                                "bool":{
                                    "must_not":[
                                        {
                                            "exists":{
                                                "field":"esPackagingInfo",
                                                "boost":"1.0"
                                            }
                                        }
                                    ],
                                    "adjust_pure_negative":true,
                                    "boost":"1.0"
                                }
                            },
                            {
                                "term":{
                                    "esPackagingInfo.sort":{
                                        "value":0,
                                        "boost":"1.0"
                                    }
                                }
                            }
                        ],
                        "adjust_pure_negative":true,
                        "boost":"1.0"
                    }
                }
            ],
            "filter":[
                {
                    "terms":{
                        "productInfo.commodityType":[
                            "0"
                        ],
                        "boost":"1.0"
                    }
                },
                {
                    "terms":{
                        "type":[
                            "1"
                        ],
                        "boost":"1.0"
                    }
                },
                {
                    "terms":{
                        "enterpriseId":[
                            "1894"
                        ],
                        "boost":"1.0"
                    }
                },
                {
                    "terms":{
                        "productInfo.isValid":[
                            "0"
                        ],
                        "boost":"1.0"
                    }
                },
                {
                    "terms":{
                        "resourceProductInfo.isValid":[
                            "0"
                        ],
                        "boost":"1.0"
                    }
                },
                {
                    "terms":{
                        "deleteFlag":[
                            "0"
                        ],
                        "boost":"1.0"
                    }
                },
                {
                    "terms":{
                        "classSubject.deleteFlag":[
                            "0"
                        ],
                        "boost":"1.0"
                    }
                }
            ],
            "adjust_pure_negative":true,
            "boost":"1.0"
        }
    },
    "sort":[
        {
            "createDate":{
                "order":"desc"
            }
        }
    ],
    "aggregations":{
        "group_aggs":{
            "terms":{
                "field":"productAndSupplier.keyword",
                "size":2147483647,
                "min_doc_count":1,
                "shard_min_doc_count":0,
                "show_term_doc_count_error":false,
                "order":[
                    {
                        "_count":"desc"
                    },
                    {
                        "_key":"asc"
                    }
                ]
            },
            "aggregations":{
                "top":{
                    "top_hits":{
                        "from":0,
                        "size":1,
                        "version":false,
                        "seq_no_primary_term":false,
                        "explain":false,
                        "sort":[
                            {
                                "createDate":{
                                    "order":"desc"
                                }
                            }
                        ]
                    }
                },
                "max_createDate":{
                    "max":{
                        "field":"createDate"
                    }
                },
                "bucket_field":{
                    "bucket_sort":{
                        "sort":[
                            {
                                "max_createDate":{
                                    "order":"desc"
                                }
                            }
                        ],
                        "from":0,
                        "size":20,
                        "gap_policy":"SKIP"
                    }
                }
            }
        },
        "group_aggs_total":{
            "terms":{
                "field":"productAndSupplier.keyword",
                "size":2147483647,
                "min_doc_count":1,
                "shard_min_doc_count":0,
                "show_term_doc_count_error":false,
                "order":[
                    {
                        "_count":"desc"
                    },
                    {
                        "_key":"asc"
                    }
                ]
            }
        }
    }
}