GET xby_index_users/_search
{"size":0,"aggs":{"birth_place_bucket":{"terms":{"field":"birth_place"}}}}
Range Aggregation:通过指定数值的范围来设定分桶规则。可以定义多个范围,文档会根据这些范围被分配到不同的桶中。
GET xby_index_users/_search
{"size":0,"aggs":{"age_bucket":{"range":{"field":"age","ranges":[{"from":0,"to":30},{"from":30,"to":60},{"from":60,"to":90}]}}}}
GET xby_index_users/_search
{"size":0,"aggs":{"age_bucket":{"histogram":{"field":"age","interval":30}}}}
Date Histogram Aggregation:日期直方图分桶,根据日期阶梯分组,例如给定阶梯为周,会自动每周分为一组。
GET xby_index_users/_search
{"size":0,"aggs":{"birthday_bucket":{"date_histogram":{"field":"birthday","calendar_interval":"year"}}}}
Metric Aggregation 一些数学运算,可以对文档字段进行统计分析
仅输出一个值的运算 min/max/sum/avg/cardinality
GET xby_index_users/_search
{"size":0,"aggs":{"age_avg":{"avg":{"field":"age"}},"age_max":{"max":{"field":"age"}},"age_min":{"min":{"field":"age"}},"age_sum":{"sum":{"field":"age"}},"age_cardinality":{"cardinality":{"field":"age"}}}}
输出多个值的运算 stats/percenties/percentile_ranks
GET xby_index_users/_search
{"size":0,"aggs":{"age_stats":{"stats":{"field":"age"}},"age_percentiles":{"percentiles":{"field":"age"}}}}
Pipeline Aggregation 对其他运算结果的二次运算
GET xby_index_users/_search
{"size":0,"aggs":{"sales_per_month":{"date_histogram":{"field":"date","interval":"month"},"aggs":{"sales":{"sum":{"field":"price"}}}},"avg_monthly_sales":{"avg_bucket":{"buckets_path":"sales_per_month>sales"}}}}