假设有三个人A、B、C
A的文档是这样的:
{
“_index” : “customer_v2025”,
“_type” : “customer_info”,
“_id” : “105100015130”,
“_score” : 0.0,
“_source” : {
“assets” : {
“merge” : 10
},
“account_open_date” : {
“normal” : 20191210
},
“customer_name” : “XXX”,
“customer_id” : 105100015130,
“one_account” : {
“one_account_no” : “111”,
“one_account_return_date” : “20201111”,
}
}
}
B的文档:
{
“_index” : “customer_v2025”,
“_type” : “customer_info”,
“_id” : “105100015130”,
“_score” : 0.0,
“_source” : {
“assets” : {
“merge” : 10
},
“account_open_date” : {
“normal” : 20191210
},
“customer_name” : “XXX”,
“customer_id” : 105100015131,
“one_account” : {
“one_account_no” : “111”,
“one_account_return_date” : “20201111”,
}
}
}
C的文档:
{
“_index” : “customer_v2025”,
“_type” : “customer_info”,
“_id” : “105100015130”,
“_score” : 0.0,
“_source” : {
“assets” : {
“merge” : 15
},
“account_open_date” : {
“normal” : 20191210
},
“customer_name” : “XXX”,
“customer_id” : 105100015132,
“one_account” : {
“one_account_no” : “222”,
“one_account_return_date” : “20201111”,
}
}
}
此时我们通过一户通字段one_account_no去聚合总资产merge的和:
"aggregations": {
"statistics_assets": {
"terms": {
"field": "one_account.one_account_no",
"size": 10,
"order": {
"assets": "asc"
}
},
"aggregations": {
"assets": {
"sum": {
"field": "assets.merge"
}
}
}
}
}
结果是如下:
"aggregations" : {
"statistics_assets" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "111",
"doc_count" : 2,
"assets" : {
"value" : 20
}
},
{
"key" : "222",
"doc_count" : 1,
"assets" : {
"value" : 15
}
}
]
}
}
A和B的one_account_no都是111,所以doc_count是2,A和B的总资产相加的和是20。
上面是一个简单的聚合
如果要对聚合后的结果升序可以这样写:
"aggregations": {
"statistics_assets": {
"terms": {
"field": "one_account.one_account_no",
"size": 10,
"order": {
"assets": "asc"
}
},
"aggregations": {
"assets": {
"sum": {
"field": "assets.merge"
}
}
}
},
"count": {
"cardinality": {
"field": "one_account.one_account_no"
}
}
}
statistics_assets聚合里是通过对one_account_no对merge聚合,order里对聚合后的assets升序排序;count聚合里是统计去重后的one_account_no有多少个。
Java代码如下:
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("statistics_assets")
.field("one_account.one_account_no")
.size(10);
//true 为升序,false为降序
termsAggregationBuilder.order(BucketOrder.aggregation("assets", true ));
AggregationBuilder assetsAggregation = AggregationBuilders.sum("assets").field("assets.merge");
termsAggregationBuilder.subAggregation(assetsAggregation);
searchSourceBuilder.aggregation(termsAggregationBuilder)
.aggregation(AggregationBuilders.cardinality("count").field("one_account.one_account_no"));
聚合结果如下:
"aggregations" : {
"count" : {
"value" : 14
},
"statistics_assets" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "105100015097",
"doc_count" : 1,
"assets" : {
"value" : -999999.99
}
},
{
"key" : "105100015130",
"doc_count" : 1,
"assets" : {
"value" : -9.999
}
},
{
"key" : "110600000973",
"doc_count" : 2,
"assets" : {
"value" : 0.0
}
},
{
"key" : "105100015207",
"doc_count" : 1,
"assets" : {
"value" : 9.999
}
},
{
"key" : "105500032110",
"doc_count" : 2,
"assets" : {
"value" : 117694.11
}
}
]
}
}
更新:
其他聚合后排序的例子如下:
"aggregations": {
"countNumber": {
"terms": {
"field": "eventInfo",
"size": 10,
"order": [
{
"_count": "desc"
}
]
}
}
}
通过聚合eventInfo后的结果来降序排序