

GET cars/_search{"size": 0,"aggs": {"first_agg_name": {"terms": {"field": "color"},"aggs": {"sub_agg_name1": {"avg": {"field": "price"}},"sub_agg_name2": {"terms": {"field": "make"}}}}}}

1)了解每种聚合类型的使用场景,简单而言,就是知道每种聚合是干嘛的,能对数据做怎样的分析;
2)了解其注意事项和重要参数;
3)完成以上2点,我觉得就差不多了,在实际工作中,面对需求,我们知道可以用哪些聚合操作解决需求即可,需要用到的时候再去详细学习具体的语法。

GET blogs_index/_search{"size": 0,"aggs": {"author": {"terms": {"field": "author"}}}}
"aggregations" : {"author" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "方才兄","doc_count" : 3},{"key" : "方才","doc_count" : 1}]}}

GET blogs_index/_search{"size": 0,"aggs": {"author": {"rare_terms": {"field": "author","max_doc_count": 10}}}}
"aggregations" : {"author" : {"buckets" : [{"key" : "方才","doc_count" : 1},{"key" : "方才兄","doc_count" : 3}]}}

GET product/_search{"size": 0,"aggs": {"price": {"histogram": {"field": "price","interval": 2000}}}}
{"aggregations": {"price": {"buckets": [{"key": 0,"doc_count": 3},{"key": 20000,"doc_count": 4},{"key": 80000,"doc_count": 1}]}}}

GET blogs_index/_search{"size": 0,"aggs": {"price": {"date_histogram": {"field": "createAt","calendar_interval": "day","format": "yyyy-MM-dd"}}}}
"aggregations": {"price": {"buckets": [{"key_as_string": "2020-05-22","key": 1590105600000,"doc_count": 1},{"key_as_string": "2020-05-23","key": 1590192000000,"doc_count": 1},{"key_as_string": "2020-05-24","key": 1590278400000,"doc_count": 2}]}}

GET blogs_index/_search{"size": 0,"aggs": {"createTime": {"auto_date_histogram": {"field": "createAt","format": "yyyy-MM-dd","buckets": 3}}}}
"aggregations": {"createTime": {"buckets": [{"key_as_string": "2020-05-22","key": 1590105600000,"doc_count": 1},{"key_as_string": "2020-05-23","key": 1590192000000,"doc_count": 1},{"key_as_string": "2020-05-24","key": 1590278400000,"doc_count": 2}],"interval": "1d"}}

GET product/_search{"aggs": {"price_ranges": {"range": {"field": "price","ranges": [{"to": 100},{"from": 100,"to": 200},{"from": 200}]}}}}
"aggregations": {"price_ranges": {"buckets": [{"key": "*-100.0","to": 100,"doc_count": 0},{"key": "100.0-200.0","from": 100,"to": 200,"doc_count": 0},{"key": "200.0-*","from": 200,"doc_count": 0}]}}

GET product/_search{"aggs": {"range": {"date_range": {"field": "date","format": "yyyy-MM","ranges": [{"to": "now-10M/M"},{"from": "now-10M/M"}]}}}}
"aggregations": {"range": {"buckets": [{"key": "*-2019-09","to": 1567296000000,"to_as_string": "2019-09","doc_count": 20},{"key": "2019-09-*","from": 1567296000000,"from_as_string": "2019-09","doc_count": 50}]}}

GET ip_addresses/_search{"size": 10,"aggs": {"ip_ranges": {"ip_range": {"field": "ip","ranges": [{"to": "10.0.0.5"},{"from": "10.0.0.5"}]}}}}
GET product/_search{"aggregations": {"ip_ranges": {"buckets": [{"key": "*-10.0.0.5","to": "10.0.0.5","doc_count": 10},{"key": "10.0.0.5-*","from": "10.0.0.5","doc_count": 0}]}}}

GET teamwork_task/_search{"size": 0,"aggs": {"my_buckets": {"composite": {"size": 2,"sources": [{"customName": {"terms": {"field": "team_id"}}}]}}}}
"aggregations": {"my_buckets": {"after_key": {"customName": 135},"buckets": [{"key": {"customName": 128},"doc_count": 2},{"key": {"customName": 135},"doc_count": 3}]}}}
GET teamwork_task/_search{"size": 0,"aggs": {"my_buckets": {"composite": {"size": 2,"sources": [{"customName": {"terms": {"field": "team_id"}}}],"after": {"customName": 135}}}}}
GET question_index/_search{"size": 0,"aggs": {"my_buckets": {"composite": {"sources": [{"question_A": {"terms": {"field": "question_A"}}},{"question_B": {"terms": {"field": "question_B"}}}]}}}}
"aggregations": {"my_buckets": {"after_key": {"question_A": "B","question_B": 1},"buckets": [{"key": {"question_A": "A","question_B": 1},"doc_count": 1},{"key": {"question_A": "B","question_B": 1},"doc_count": 1}]}}

POST sales/_search?size=0{"aggs" : {"t_shirts" : {"filter" : { "term": { "type": "t-shirt" } },"aggs" : {"avg_price" : { "avg" : { "field" : "price" } }}}}}

GET logs/_search{"size": 0,"aggs": {"messages": {"filters": {"filters": {"errors": {"match": {"body": "error"}},"warnings": {"match": {"body": "warning"}}}}}}}

POST sales/_search?size=0{"query": {"match": {"type": "t-shirt"}},"aggs": {"all_products": {"global": {},"aggs": {"avg_price": {"avg": {"field": "price"}}}},"t_shirts": {"avg": {"field": "price"}}}}

POST /sales/_search?size=0{"aggs": {"products_without_a_price": {"missing": {"field": "price"}}}}




下期预告:ES的聚合分析02:Metric aggs 【关注公众号:方才编程,系统学习ES】
待续


文章转载自方才编程,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。





