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个数。
(描述得不太清晰,看代码和执行结果就好)