# Unable to make a bucketed aggregation by date histogram

**URL:** <https://discuss.elastic.co/t/unable-to-make-a-bucketed-aggregation-by-date-histogram/176073>\
**Category:** Elasticsearch\
**Created:** [April 9, 2019, 5:11pm UTC](https://discuss.elastic.co/t/unable-to-make-a-bucketed-aggregation-by-date-histogram/176073 "2019-04-09T17:11:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![User\_User](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/user_user/32/43769_2.png) [@User\_User](https://discuss.elastic.co/u/User_User)\
**Post date:** [April 9, 2019, 5:11pm UTC](https://discuss.elastic.co/t/unable-to-make-a-bucketed-aggregation-by-date-histogram/176073/1 "2019-04-09T17:11:46Z")

</div>

I have multiple documents ingested in my ES which are of the form -

```auto
{
    "weather":"cold",
    "date_1":"2017/07/05",
    "feedback":[
        {
                "date_2":"2017/08/07",
                "value":28,
                "comment":"not cold"
        },{
                "date_2":"2017/08/09",
                "value":48,
                "comment":"a bit chilly"
        },{
                "date_2":"2017/09/07",
                "value":18,
                "comment":"very cold"
        }, ...
     ]
}

```

I want to do 2 date aggregations here, but not sure how to move ahead with it.

1. I want the aggregate average of all "feedback.value" by "date\_1" where "weather" is "cold"
2. I want the aggregate average of all "feedback.value" by "date\_2" where "weather" is "cold"

I am not sure if such aggregate query are supported by ES. I was not able to resolve it going through the documentation.

Thanks for helping with this.

---

<div class="post-metadata">

**Author:** ![User\_User](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/user_user/32/43769_2.png) [@User\_User](https://discuss.elastic.co/u/User_User)\
**Post date:** [April 11, 2019, 8:53am UTC](https://discuss.elastic.co/t/unable-to-make-a-bucketed-aggregation-by-date-histogram/176073/2 "2019-04-11T08:53:43Z")

</div>

I was able to resolve the first part of the problem -

> [@User\_User](#):
>
> I want the aggregate average of all "feedback.value" by "date\_1" where "weather" is "cold"

This can be aggregated using this -

```auto
GET _search
{
"query": {
        "query_string": {
                "query": "cold"
                 }
        },
"size": 0,
"aggs": {
        "temperature": {
                    "date_histogram":{
                                      "field" : "date_1",
                                      "interval" : "month"
                                      },
                      "aggs":{
                              "temperature_agg":{
                                                "terms": {
                                                        "field": "feedback.value"
                                                          }
                                                }
                              }

                    }
        }
}

```

Any help on the 2nd part would be helpful!

---

<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:** [April 11, 2019, 2:16pm UTC](https://discuss.elastic.co/t/unable-to-make-a-bucketed-aggregation-by-date-histogram/176073/3 "2019-04-11T14:16:55Z")

</div>

Hey,

with that data structure you should chedk the [nested datatype](https://www.elastic.co/guide/en/elasticsearch/reference/7.0/nested.html) as well as the [nested aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-nested-aggregation.html) in order to make sure you get the correct results.

Regarding your second problem. You could just add another agg named `temperature_1` that filters on the `date_2` field -as you can have more than one agg in a single request.

--Alex

---

<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:** [May 9, 2019, 2:16pm UTC](https://discuss.elastic.co/t/unable-to-make-a-bucketed-aggregation-by-date-histogram/176073/4 "2019-05-09T14:16:57Z")

</div>

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