5.3 ElasticSearch聚合分析之Pipeline

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
          }
        }
      ]
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值