user4906240
user4906240

Reputation: 625

Getting avg sub aggregation

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.

Upvotes: 0

Views: 125

Answers (1)

jaspreet chahal
jaspreet chahal

Reputation: 9099

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
          }
        }
      ]
    }
  }
}

Upvotes: 2

Related Questions