elasticsearch支持很強(qiáng)大的數(shù)據(jù)處理功能,在聚合數(shù)據(jù)方面,elasticsearch提供了很多的api,具體請(qǐng)參考elasticsearch官網(wǎng)。
這里只簡單說一下如何多重聚合數(shù)據(jù)憋肖。
1、最簡單的聚合數(shù)據(jù)婚苹,相當(dāng)于mysql里的group by
比如岸更,再現(xiàn)在按照公司來聚合數(shù)據(jù):
{
"aggs": {
"companies": {
"terms": {
"field": "company",
"size": 0
}
}
},
"size": 0
}
執(zhí)行后,結(jié)果里將會(huì)包含這樣的數(shù)據(jù):
"companies":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"company1",
"doc_count":90
},
{
"key":"company2",
"doc_count":1
}
]
}
2膊升、在結(jié)果里增加另外字段的聚合結(jié)果怎炊,相當(dāng)于mysql里group by兩個(gè)字段
比如,再現(xiàn)在按照公司來聚合數(shù)據(jù),然后再按部門聚合數(shù)據(jù):
{
"aggs": {
"companies": {
"terms": {
"field": "company",
"size": 0
},
"aggs": {
"developments": {
"terms": {
"field": "development",
"size": 0
}
}
}
}
},
"size": 0
}
執(zhí)行后廓译,結(jié)果里將會(huì)包含這樣的數(shù)據(jù):
"companies":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"company1",
"doc_count":90,
"developments":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"部門1",
"doc_count":20
}
]
}
},
{
"key":"company2",
"doc_count":1,
"developments":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"部門2",
"doc_count":10
}
]
}
}
]
}
3评肆、再增強(qiáng)一下,按照某個(gè)日期以后添加的部門數(shù)據(jù)
{
"aggs": {
"companies": {
"terms": {
"field": "company",
"size": 0
},
"aggs": {
"developments": {
"date_range": {
"field": "createtime",
"format": "yyy-MM-dd",
"ranges": [
{
"from": "now-10M/M"
}
]
}
}
}
}
},
"size": 0
}
執(zhí)行后非区,結(jié)果里將會(huì)包含這樣的數(shù)據(jù):
"companies":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"company1",
"doc_count":90,
"developments":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key": "2017-06-15-*",
"from": 1497484800000,
"from_as_string": "2017-06-15",
"doc_count": 8
}
]
}
},
{
"key":"company2",
"doc_count":1,
"developments":{
"buckets":[
{
"key": "2017-06-15-*",
"from": 1497484800000,
"from_as_string": "2017-06-15",
"doc_count": 3
}
]
}
}
]
}
這里的子部門數(shù)據(jù)是從2017-06-15開始到現(xiàn)在添加的瓜挽。
還有很多其它的聚合方法,請(qǐng)到官網(wǎng)查看征绸。