# Filtering the response into the "Avg Bucket Aggregation"

**URL:** <https://discuss.elastic.co/t/filtering-the-response-into-the-avg-bucket-aggregation/250806>\
**Category:** Elasticsearch\
**Created:** [October 2, 2020, 3:00pm UTC](https://discuss.elastic.co/t/filtering-the-response-into-the-avg-bucket-aggregation/250806 "2020-10-02T15:00:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Famas](https://avatars.discourse-cdn.com/v4/letter/f/b782af/32.png) [@Famas](https://discuss.elastic.co/u/Famas)\
**Post date:** [October 2, 2020, 3:00pm UTC](https://discuss.elastic.co/t/filtering-the-response-into-the-avg-bucket-aggregation/250806/1 "2020-10-02T15:00:07Z")

</div>

Hi all.

I'm trying to use "Avg Bucket Aggregation".  
This is the example given in the help page of Elasticsearch site.

```auto
    POST /_search
    {
      "size": 0,
      "aggs": {
        "sales_per_month": {
          "date_histogram": {
            "field": "date",
            "calendar_interval": "month"
          },
          "aggs": {
            "sales": {
              "sum": {
                "field": "price"
              }
            }
          }
        },
        "avg_monthly_sales": {
          "avg_bucket": {
            "buckets_path": "sales_per_month>sales" 
          }
        }
      }
    }

```

Referring to your example, the aggregation returns a result formatted like this

```auto
  {
      "took": 11,
      "timed_out": false,
      "_shards": ...,
      "hits": ...,
      "aggregations": {
         "sales_per_month": {
            "buckets": [
               {
                  "key_as_string": "2015/01/01 00:00:00",
                  "key": 1420070400000,
                  "doc_count": 3,
                  "sales": {
                     "value": 550.0
                  }
               },
               {
                  "key_as_string": "2015/02/01 00:00:00",
                  "key": 1422748800000,
                  "doc_count": 2,
                  "sales": {
                     "value": 60.0
                  }
               },
               {
                  "key_as_string": "2015/03/01 00:00:00",
                  "key": 1425168000000,
                  "doc_count": 2,
                  "sales": {
                     "value": 375.0
                  }
               }
            ]
         },
         "avg_monthly_sales": {
             "value": 328.33333333333333
         }
      }
   }

```

I would only be interested in the part of the answer that contains the value "avg\_monthly\_sales" and not the buckets of the date histogram, i.e something like this:

```auto
    {
       "took": 11,
       "timed_out": false,
       "_shards": ...,
       "hits": ...,
       "aggregations": {
    "avg_monthly_sales": {
              "value": 328.33333333333333
          }
       }
    }

```

Is there any way to achieve this behavior?

Thanks for your help,  
**Famas**

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 5, 2020, 9:16am UTC](https://discuss.elastic.co/t/filtering-the-response-into-the-avg-bucket-aggregation/250806/2 "2020-10-05T09:16:40Z")

</div>

Not sure I got the question right, but wouldn't that mean in your case that you are just interested in the `avg` of the `price` field for a certain time range - which would be a `range` query with the correct start & end date?

---

<div class="post-metadata">

**Author:** ![Famas](https://avatars.discourse-cdn.com/v4/letter/f/b782af/32.png) [@Famas](https://discuss.elastic.co/u/Famas)\
**Post date:** [October 6, 2020, 6:44am UTC](https://discuss.elastic.co/t/filtering-the-response-into-the-avg-bucket-aggregation/250806/3 "2020-10-06T06:44:53Z")

</div>

Hi Alexander.  
Thank you for your answer!

The query actually calculates the average of the **total monthly sales**  
[ElasticSearch Reference](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-avg-bucket-aggregation.html)

It works very well, but when the buckets to be transferred are many, it would be better, for efficiency questions, to be able to transfer only the average data and not all the buckets that were used to calculate this average.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 3, 2020, 6:45am UTC](https://discuss.elastic.co/t/filtering-the-response-into-the-avg-bucket-aggregation/250806/4 "2020-11-03T06:45:04Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
