Getting avg sub aggregation

谁说我不能喝 提交于 2019-12-25 01:49:49

问题


I'd like to get the avg of a sub aggregation. For example, i have daily profit of each branch. I want to sum them so that i can get total daily profit. and then i want to get the monthly or week average of that daily profit. So far i have done this

{
    "size" : 0,
    "aggs" : {
        "group_by_month": {
          "date_histogram": {
            "field": "Profit_Day",
            "interval": "month",
            "format" : "MM-yyyy"
          },
          "aggs": {
                "avgProf": {
                    "avg": {
                        "field": "ProfitValue"
                    }
                },
                "group_by_day": {
                    "date_histogram": {
                        "field": "Profit_Day",
                        "interval": "day",
                        "format" : "yyyy-MM-dd"
                    },
                    "aggs": {
                        "prof": {
                            "sum": {
                                "field": "ProfitValue"
                            }
                        }
                    }
                }
            }


        }
    }
}

Issue is i am getting daaily sum which is correct but instead of getting monthly average of daily sum i am getting monthly average of profit from each branch.


回答1:


You need to use average bucket aggragetion

Query:

GET sales1/_search
{
  "size": 0,
  "aggs": {
    "group_by_month": {
      "date_histogram": {
        "field": "proffit_day",
        "interval": "month",
        "format": "MM-yyyy"
      },
      "aggs": {
        "group_by_day": {
          "date_histogram": {
            "field": "proffit_day",
            "interval": "day",
            "format": "yyyy-MM-dd"
          },
          "aggs": {
            "prof": {
              "sum": {
                "field": "proffit_value"
              }
            }
          }
        },
        "avg_monthly_sales": {
          "avg_bucket": {
            "buckets_path": "group_by_day>prof"
          }
        }
      }
    }
  }
}

Response:

{
    "group_by_month" : {
      "buckets" : [
        {
          "key_as_string" : "09-2019",
          "key" : 1567296000000,
          "doc_count" : 2,
          "group_by_day" : {
            "buckets" : [
              {
                "key_as_string" : "2019-09-25",
                "key" : 1569369600000,
                "doc_count" : 2,
                "prof" : {
                  "value" : 15.0
                }
              }
            ]
          },
          "avg_monthly_sales" : {
            "value" : 15.0
          }
        },
        {
          "key_as_string" : "10-2019",
          "key" : 1569888000000,
          "doc_count" : 2,
          "group_by_day" : {
            "buckets" : [
              {
                "key_as_string" : "2019-10-01",
                "key" : 1569888000000,
                "doc_count" : 1,
                "prof" : {
                  "value" : 10.0
                }
              },
              {
                "key_as_string" : "2019-10-02",
                "key" : 1569974400000,
                "doc_count" : 0,
                "prof" : {
                  "value" : 0.0
                }
              },
              {
                "key_as_string" : "2019-10-03",
                "key" : 1570060800000,
                "doc_count" : 1,
                "prof" : {
                  "value" : 15.0
                }
              }
            ]
          },
          "avg_monthly_sales" : {
            "value" : 12.5
          }
        }
      ]
    }
  }
}


来源:https://stackoverflow.com/questions/58534642/getting-avg-sub-aggregation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!