es多字段分组去重统计

es多字段分组去重统计

oracle中sql:
select a1 ,b1 from dual group by a1 ,b1

select DISTINCT a1 ,b1 from dual

es分组去重统计:
(话不多说,直接贴代码)
TermsAggregationBuilder mobilesBuilder = AggregationBuilders.terms(“mobiles”).field(“mobile”);
TermsAggregationBuilder msgIdsBuilder = AggregationBuilders.terms(“msgIds”).field(“msgId”);
mobilesBuilder.subAggregation(msgIdsBuilder);
SearchResponse searchResponse = this.getClient().prepareSearch(dailyIndexNamesByTime)
.setFrom(0)
.setSize(10000)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.addSort(“createDate”, SortOrder.DESC)
.addAggregation(mobilesBuilder)
.setQuery(boolQueryBuilder)
.execute()
.actionGet();
查询结果:
“aggregations”: {
“mobiles”: {
“doc_count_error_upper_bound”: 0,
“sum_other_doc_count”: 0,
“buckets”: [
{
“key”: “1500111111”,
“doc_count”: 3,
“msgIds”: {
“doc_count_error_upper_bound”: 0,
“sum_other_doc_count”: 0,
“buckets”: [
{
“key”: “5d7e222f873a475790f4877af1fc4269”,
“doc_count”: 2
},
{
“key”: “231d9fd9348645ffa425396f31525f92”,
“doc_count”: 1
}
]
}
},
{
“key”: “1851000000”,
“doc_count”: 3,
“msgIds”: {
“doc_count_error_upper_bound”: 0,
“sum_other_doc_count”: 0,
“buckets”: [
{
“key”: “04209177ebd54f88b21668a38115af37”,
“doc_count”: 1
},
{
“key”: “2866cdbae5b3468d803282a3ba3b038e”,
“doc_count”: 1
},
{
“key”: “ad5b9d0123084fb78b34e34397fc26de”,
“doc_count”: 1
}
]
}
},
{
“key”: “134522222”,
“doc_count”: 1,
“msgIds”: {
“doc_count_error_upper_bound”: 0,
“sum_other_doc_count”: 0,
“buckets”: [ ]
}
}
]
}
}
思路解析:
先按照mobile分组,再根据mobile对每个msgId分组,最终,从aggregations中的每个mobil中遍历出magId个数。
(描述得不太清晰,看代码和执行结果就好)

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值