es查询和聚合查询

@Autowired
@Qualifier("restHighLevelClientX")
private RestHighLevelClient restClient;

public List<DataGroupBasicDataTopProvincenTableResponse> queryCustomerNum(DataViewRequest dataViewRequest){
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery("",""))
                .must(QueryBuilders.rangeQuery("").gte("").lte(""))
                .must(QueryBuilders.wildcardQuery("",""))
                .must(QueryBuilders.termQuery("",""))
                .must(QueryBuilders.boolQuery())
                .should(QueryBuilders.matchQuery("",""))
                .should(QueryBuilders.fuzzyQuery("",""));


        TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("provinceCode").field("province_code");//分组统计
//        AggregationBuilders.count("provinceCode").field("province_code");
//        AggregationBuilders.cardinality("provinceCode").field("province_code");//去重
//        AggregationBuilders.terms("provinceCode").field("province_code");
//        AggregationBuilders.max("provinceCode").field("province_code");
//        AggregationBuilders.min("provinceCode").field("province_code");
//        AggregationBuilders.avg("provinceCode").field("province_code");
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.aggregation(termsAggregationBuilder);

        SearchRequest searchRequest = new SearchRequest("索引名称");
        searchRequest.types("类型");
        searchRequest.source(searchSourceBuilder);

        try {
            // call es
            SearchResponse searchResponse = restClient.search(searchRequest, ElasticSearchClientFactory.getCommonOptions());
            Terms terms = searchResponse.getAggregations().get("provinceCode");
            long totalHits = searchResponse.getHits().getTotalHits();

            for (int i=0;i<terms.getBuckets().size();i++){
                String key = terms.getBuckets().get(i).getKey().toString();
                long docCount = terms.getBuckets().get(i).getDocCount();

                DataGroupBasicDataTopProvincenTableResponse dataGroupBasicDataTopProvincenTableResponse = new DataGroupBasicDataTopProvincenTableResponse();
                dataGroupBasicDataTopProvincenTableResponse.setProvince(key);
                dataGroupBasicDataTopProvincenTableResponse.setUv(docCount);
                dataGroupBasicDataTopProvincenTableResponse.setProportion(MallDataCalculationUtil.getDivideValueForPercent(docCount, totalHits).longValue());
                dataPageMapVOS.add(dataGroupBasicDataTopProvincenTableResponse);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
		return dataPageMapVOS;
}
上一篇:day39 redis 数据类型 String 字符串、List 列表、Hash 散列


下一篇:软件工程应用于实践:AJ-Report项目 源码分析(1)