# Possible to aggregate and filter on the same key?

**URL:** <https://discuss.elastic.co/t/possible-to-aggregate-and-filter-on-the-same-key/102888>\
**Category:** Elasticsearch\
**Created:** [October 5, 2017, 5:51pm UTC](https://discuss.elastic.co/t/possible-to-aggregate-and-filter-on-the-same-key/102888 "2017-10-05T17:51:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bjd14](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@bjd14](https://discuss.elastic.co/u/bjd14)\
**Post date:** [October 5, 2017, 5:51pm UTC](https://discuss.elastic.co/t/possible-to-aggregate-and-filter-on-the-same-key/102888/1 "2017-10-05T17:51:55Z")

</div>

I'm not very experienced with Elastic but is it possible to aggregate and filter on the same key?

Say I want the departments of a certain city that is being filtered on. This may be a dumb question but is there anyway to do this or is my query incorrect? I know it is invalid JSON due to the same key ([department.id](http://department.id)). Would the pipeline aggregation be something viable to use?

The top query would be a "match\_all" query.

Aggregations such as:

```
"aggregations": {
	"department.id": {
		"terms": {
			"field": "department.id",
			"size": 10
		}
	},
	"department.id": {
		"filter": {
			"bool": {
				"filter": {
					"terms": {
						"city": ["chicago"]
					}
				}
			}
		}
	}
}
```

---

<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 2, 2017, 5:52pm UTC](https://discuss.elastic.co/t/possible-to-aggregate-and-filter-on-the-same-key/102888/2 "2017-11-02T17:52:10Z")

</div>

Closed on November 2, 2017, 5:52pm UTC

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