elasticsearch 다중 필드 그룹 집합 페이지 조회 기능 구현

7695 단어 Elasticsearch
1. 필드별로 그룹화
2. 시간월별 그룹 포함
3. 페이지 나누기 조회 지원
아래 코드를 붙여서 실현하면 참고만 할 수 있습니다. 좋은 방안이 있으면 메시지를 남겨 주십시오.
@Override
    public Pager findCusMap(CustMapConditionVO vo, Pager pager) {
        // getBoolQueryBuilder  , 
        BoolQueryBuilder b = getBoolQueryBuilder(vo);

        if (pager.getTotal() == null || pager.getTotal() <= 0) {
            DateHistogramAggregationBuilder dateHistogramInterval = AggregationBuilders.dateHistogram("lastEndDateInterval").field("lastEndDate").dateHistogramInterval(new DateHistogramInterval("1M"));

            TermsAggregationBuilder provinceTermsBuilder = AggregationBuilders.terms("provinceName").field("provinceName")
                    .subAggregation(AggregationBuilders.terms("cityName").field("cityName").subAggregation(AggregationBuilders.terms("companyName").field("companyName")));

            CardinalityAggregationBuilder builder12 = AggregationBuilders.cardinality("totalCount").field("companyName");

            dateHistogramInterval.subAggregation(builder12);

            SearchRequestBuilder builder1 = client.prepareSearch(CarInfoCenterES.INDEX).setTypes(CarInfoCenterES.TYPE).setQuery(b)
                    .addAggregation(dateHistogramInterval)
                    .addAggregation(provinceTermsBuilder);


            Aggregations aggregations = builder1.get().getAggregations();

            Histogram histogram = aggregations.get("lastEndDateInterval");
            long sum = 0;
            for (Histogram.Bucket entry : histogram.getBuckets()) {
                Aggregations userNumAggregations = entry.getAggregations();
                Cardinality cardinality = userNumAggregations.get("totalCount");
                sum += cardinality.getValue();

               /* Terms  provinceTerms = entry.getAggregations().get("provinceName");
                for (Terms.Bucket provinceBucket : provinceTerms.getBuckets()) {
                    sum +=provinceBucket.getDocCount();
                }
                Terms  cityTerms = entry.getAggregations().get("cityName");
                for (Terms.Bucket cityBucket : cityTerms.getBuckets()) {
                    sum +=cityBucket.getDocCount();
                }
                Terms  comTerms = entry.getAggregations().get("cityName");
                for (Terms.Bucket comBucket : comTerms.getBuckets()) {
                    sum +=comBucket.getDocCount();
                }*/

            }
            pager.setTotal((int) sum);
        }

        DateHistogramAggregationBuilder aggregationBuilder = AggregationBuilders.dateHistogram("lastEndDateInterval").field("lastEndDate").dateHistogramInterval(new DateHistogramInterval("1M"));

        TermsAggregationBuilder userNumTerms = AggregationBuilders.terms("provinceName").field("provinceName");
        userNumTerms.subAggregation(AggregationBuilders.terms("cityName").field("cityName")
                                    .subAggregation(AggregationBuilders.terms("companyName").field("companyName")));

        int startIndex = (pager.getPageNum() - 1) * pager.getPageSize();
        int endIndex = pager.getPageSize() * pager.getPageNum();

        if (pager.getPageSize() != null || pager.getPageSize() > 0) {
            userNumTerms.size(endIndex);
        }

        aggregationBuilder.subAggregation(userNumTerms);
        SearchRequestBuilder builder = client.prepareSearch(CarInfoCenterES.INDEX).setTypes(CarInfoCenterES.TYPE).setQuery(b)
                .addAggregation(aggregationBuilder);

        builder.addSort("provinceName", SortOrder.ASC);
        builder.addSort("cityName", SortOrder.ASC);

        SearchResponse response = builder.get();
        Histogram lastEndDateInterval = response.getAggregations().get("lastEndDateInterval");
        List dataList = new ArrayList<>(0);

        int i = 0;

        for (Histogram.Bucket entry : lastEndDateInterval.getBuckets()) {
            CustMapVO custMapVO = new CustMapVO();
            String startTime = entry.getKeyAsString();
            custMapVO.setYearMonth(startTime.substring(0, 7));
            Aggregations userNumAggregations = entry.getAggregations();

            Terms terms = userNumAggregations.get("provinceName");
            for (Terms.Bucket proEntry : terms.getBuckets()) {

                String provinceName = proEntry.getKeyAsString();
                if(StringUtils.isBlank(provinceName)){
                    System.out.println("*************");
                    continue;
                }
                custMapVO.setProviceName(provinceName);

                Aggregations cityAggregation = proEntry.getAggregations();
                Terms cityTerms = cityAggregation.get("cityName");
                for (Terms.Bucket cityEntry : cityTerms.getBuckets()) {
                    String cityName = cityEntry.getKeyAsString();
                    custMapVO.setCityName(cityName);

                    Aggregations comAggregation = cityEntry.getAggregations();
                    Terms comTerms = comAggregation.get("companyName");

                    for (Terms.Bucket comEntry : comTerms.getBuckets()) {

                        if (i++ < startIndex) {
                            continue;
                        }

                        if (i > endIndex) {
                            break;
                        }

                        String comName = comEntry.getKeyAsString();
                        long num = comEntry.getDocCount();
                        custMapVO.setComName(comName);
                        custMapVO.setAmount(new BigDecimal(num));

                        CustMapVO custMapVO1 = new CustMapVO();
                        BeanUtils.copyProperties(custMapVO,custMapVO1);
                        dataList.add(custMapVO1);
                    }

                }


            }
        }
        pager.setData(dataList);
        return pager;
    }

결합 질의 기준 코드는 다음과 같습니다.
private BoolQueryBuilder getBoolQueryBuilder(CustMapConditionVO vo) {
        BoolQueryBuilder b = QueryBuilders.boolQuery();

        // id
        if (StringUtils.isNotBlank(vo.getOrderId())) {
            b.must(QueryBuilders.matchQuery("orderId", vo.getOrderId()));
        }
        // 
        if (StringUtils.isNotBlank(vo.getLicenseNo())) {
            b.must(QueryBuilders.matchQuery("licenseNo", vo.getLicenseNo()));
        }
        // 
        if (StringUtils.isNotBlank(vo.getFrameNo())) {
            b.must(QueryBuilders.matchQuery("frameNo", vo.getFrameNo()));
        }
        // 
        if (StringUtils.isNotBlank(vo.getChannelCode())) {
            b.must(QueryBuilders.matchQuery("channelCode", vo.getChannelCode()));
        }
        // 
        if (StringUtils.isNotBlank(vo.getInsureComCode())) {
            b.must(QueryBuilders.matchQuery("insureComCode", vo.getInsureComCode()));
        }
        // 
        if (StringUtils.isNotBlank(vo.getStatus())) {
            b.must(QueryBuilders.matchQuery("status", vo.getStatus()));
        }
        // 
        if (StringUtils.isNotBlank(vo.getReceiptMessage())) {
            String[] cStartEndTimeArray = vo.getReceiptMessage().split("_");
            b.must(QueryBuilders.rangeQuery("sendTime").gte(cStartEndTimeArray[0].trim()).lte(cStartEndTimeArray[1].trim()));
        }
        return b;
    }

좋은 웹페이지 즐겨찾기