# Date range filtering does not affect date histogram aggregation

**URL:** <https://discuss.elastic.co/t/date-range-filtering-does-not-affect-date-histogram-aggregation/112241>\
**Category:** Elasticsearch\
**Created:** [December 18, 2017, 1:51pm UTC](https://discuss.elastic.co/t/date-range-filtering-does-not-affect-date-histogram-aggregation/112241 "2017-12-18T13:51:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![valdemarrolfsen](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@valdemarrolfsen](https://discuss.elastic.co/u/valdemarrolfsen)\
**Post date:** [December 18, 2017, 1:51pm UTC](https://discuss.elastic.co/t/date-range-filtering-does-not-affect-date-histogram-aggregation/112241/1 "2017-12-18T13:51:28Z")

</div>

I have some issues with filtering on a nested date field when doing aggregations. I want to separate the filtering and aggregation logic so that I can use the same filtering for different aggregations. This has been possible for all my queries except when filtering on a date range, as shown below:

```
{
    "query": {
        "nested": {
            "path": "transactions",
            "query": {
                "range": {
                    "transactions.date": {
                        "gte": "2013",
                        "lte": "2014",
                        "format": "yyyy"
                    }
                }
            }
        }
    },
    "aggs" : {
        "transactions": {
            "nested": {
                "path": "transactions"
            },
            "aggs": {
                "count_per_year": {
                    "date_histogram": {
                        "field": "transactions.date",
                        "interval": "year"
                    }
                }   
            }
        }
    }
}

```

Which I was assuming would limit the aggregation to only include transactions in 2013 and 2014, however, the query returns the following result:

```
"aggregations": {
    "transactions": {
        "doc_count": 23302,
        "count_per_year": {
            "buckets": [
                {
                    "key_as_string": "2013-01-01T00:00:00.000Z",
                    "key": 1356998400000,
                    "doc_count": 181
                },
                {
                    "key_as_string": "2014-01-01T00:00:00.000Z",
                    "key": 1388534400000,
                    "doc_count": 8363
                },
                {
                    "key_as_string": "2015-01-01T00:00:00.000Z",
                    "key": 1420070400000,
                    "doc_count": 9621
                },
                {
                    "key_as_string": "2016-01-01T00:00:00.000Z",
                    "key": 1451606400000,
                    "doc_count": 5137
                }
            ]
        }
    }
}

```

Another attempt has been the following:

```
{
	"aggs": {
		"filtered": {
			"filter": {
				"nested": {
					"path": "transactions",
					"query": {
						"range": {
							"transactions.date": {
								"gte": "2014-01-01",
		                        "lte": "2017-01-01",
		                        "format": "yyyy-MM-dd"
							}
						}	
					}
				}
			},
			"aggs": {
				"data": {
					"nested": {
						"path": "transactions"
					},
					"aggs": {
						"spend_per_year": {
							"date_histogram": {
								"field": "transactions.date",
								"interval": "month"
							}
						}
					}
				}
			}
		}
	}
}

```

I have been successful in doing a filter aggregation that limits the aggregation values in the following way:

```
{
		
	"aggs": {
		"transactions": {
			"nested": {
				"path": "transactions"
			},
			"aggs": {
				"filtered": {
					"filter": {
						"range": {
							"transactions.date": {
								"gte": "2013",
								"lte": "2014",
								"format": "yyyy"
							}
						}
					},
					"aggs": {
						"spend_per_year": {
							"date_histogram": {
								"field": "transactions.date",
								"interval": "month"
							}
						}
					}
				}
			}
		}
	}
}

```

But I am wondering what is wrong with the two first queries? Any idea why the filtering is ignored by ES?

---

<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:** [January 15, 2018, 1:52pm UTC](https://discuss.elastic.co/t/date-range-filtering-does-not-affect-date-histogram-aggregation/112241/2 "2018-01-15T13:52:11Z")

</div>

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