@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;
}