1.Sibling
找出所有bucket中值最小的bucket名称和值,如找出平均薪水最低的岗位。

PUT /employee/_mapping
{
"size": 0,
"aggs": {
"job_terms": {
"terms": {
"field": "job",
"size": 10
},
"aggs": {
"salary_avg": {
"avg": {
"field": "salary"
}
}
}
},
"min_salary_by_job": {
"min_bucket": {
"buckets_path": "job_terms>salary_avg"
}
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 7,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"job_terms" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "Java engineer",
"doc_count" : 4,
"salary_avg" : {
"value" : 23500.0
}
},
{
"key" : "Vue engineer",
"doc_count" : 2,
"salary_avg" : {
"value" : 23000.0
}
},
{
"key" : "Technical director",
"doc_count" : 1,
"salary_avg" : {
"value" : 50000.0
}
}
]
},
"min_salary_by_job" : {
"value" : 23000.0,
"keys" : [
"Vue engineer"
]
}
}
}

2.Parent
(1).Derivative
计算bucket值的导数,如先对文档按照5年的间隔进行分桶,然后再求每个桶内的薪水平均值以及其导数。

PUT /employee/_mapping
{
"size": 0,
"aggs": {
"birthday_date_histogram": {
"date_histogram": {
"field": "birthday",
"fixed_interval": "1830d",
"min_doc_count": 1
},
"aggs": {
"salary_avg": {
"avg": {
"field": "salary"
}
},
"salary_avg_derivative": {
"derivative": {
"buckets_path": "salary_avg"
}
}
}
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 7,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"birthday_date_histogram" : {
"buckets" : [
{
"key_as_string" : "1985-01-12",
"key" : 474336000000,
"doc_count" : 2,
"salary_avg" : {
"value" : 39500.0
}
},
{
"key_as_string" : "1990-01-16",
"key" : 632448000000,
"doc_count" : 2,
"salary_avg" : {
"value" : 29000.0
},
"salary_avg_derivative" : {
"value" : -10500.0
}
},
{
"key_as_string" : "1995-01-20",
"key" : 790560000000,
"doc_count" : 3,
"salary_avg" : {
"value" : 17666.666666666668
},
"salary_avg_derivative" : {
"value" : -11333.333333333332
}
}
]
}
}
}

(2).Cumulative Sum
计算bucket值的累加和,如先对文档按照5年的间隔进行分桶,然后再求每个桶内的薪水平均值以及其累加和。

PUT /employee/_mapping
{
"size": 0,
"aggs": {
"birthday_date_histogram": {
"date_histogram": {
"field": "birthday",
"fixed_interval": "1830d",
"min_doc_count": 1
},
"aggs": {
"salary_avg": {
"avg": {
"field": "salary"
}
},
"salary_avg_cumulative_sum": {
"cumulative_sum": {
"buckets_path": "salary_avg"
}
}
}
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 7,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"birthday_date_histogram" : {
"buckets" : [
{
"key_as_string" : "1985-01-12",
"key" : 474336000000,
"doc_count" : 2,
"salary_avg" : {
"value" : 39500.0
},
"salary_avg_cumulative_sum" : {
"value" : 39500.0
}
},
{
"key_as_string" : "1990-01-16",
"key" : 632448000000,
"doc_count" : 2,
"salary_avg" : {
"value" : 29000.0
},
"salary_avg_cumulative_sum" : {
"value" : 68500.0
}
},
{
"key_as_string" : "1995-01-20",
"key" : 790560000000,
"doc_count" : 3,
"salary_avg" : {
"value" : 17666.666666666668
},
"salary_avg_cumulative_sum" : {
"value" : 86166.66666666667
}
}
]
}
}
}

(3).Moving Average
计算bucket值的移动平均值,如先对文档按照5年的间隔进行分桶,然后再求每个桶内的薪水平均值以及其移动平均值。

PUT /employee/_mapping
{
"size": 0,
"aggs": {
"birthday_date_histogram": {
"date_histogram": {
"field": "birthday",
"fixed_interval": "1830d",
"min_doc_count": 1
},
"aggs": {
"salary_avg": {
"avg": {
"field": "salary"
}
},
"salary_avg_moving_avg": {
"moving_avg": {
"buckets_path": "salary_avg"
}
}
}
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 7,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"birthday_date_histogram" : {
"buckets" : [
{
"key_as_string" : "1985-01-12",
"key" : 474336000000,
"doc_count" : 2,
"salary_avg" : {
"value" : 39500.0
}
},
{
"key_as_string" : "1990-01-16",
"key" : 632448000000,
"doc_count" : 2,
"salary_avg" : {
"value" : 29000.0
},
"salary_avg_moving_avg" : {
"value" : 39500.0
}
},
{
"key_as_string" : "1995-01-20",
"key" : 790560000000,
"doc_count" : 3,
"salary_avg" : {
"value" : 17666.666666666668
},
"salary_avg_moving_avg" : {
"value" : 34250.0
}
}
]
}
}
}