# HOw can I get aggregations based on query only and based on filters (post\_filter) at the same time

**URL:** <https://discuss.elastic.co/t/how-can-i-get-aggregations-based-on-query-only-and-based-on-filters-post-filter-at-the-same-time/69562>\
**Category:** Elasticsearch\
**Created:** [December 20, 2016, 11:47am UTC](https://discuss.elastic.co/t/how-can-i-get-aggregations-based-on-query-only-and-based-on-filters-post-filter-at-the-same-time/69562 "2016-12-20T11:47:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrey\_Kaprov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrey_kaprov/32/12262_2.png) [@Andrey\_Kaprov](https://discuss.elastic.co/u/Andrey_Kaprov)\
**Post date:** [December 20, 2016, 11:47am UTC](https://discuss.elastic.co/t/how-can-i-get-aggregations-based-on-query-only-and-based-on-filters-post-filter-at-the-same-time/69562/1 "2016-12-20T11:47:01Z")

</div>

Greetings everyoune!

My questions is:  
Is there any way to process aggregations depend on filter (bool query or post\_filter) and aggregations based on query only?

I.e.  
I have a document - product. I have several products in index.  
The product structure is:  
id  
name  
categoryId  
price.

All I want is to find products by "name" property, then collect information about categories that has the document matches query, then filter them by categoryId (cat1, cat2) then I want to get an information about maximum and minimum product price within the categories (cat1 and cat2)

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [December 20, 2016, 1:07pm UTC](https://discuss.elastic.co/t/how-can-i-get-aggregations-based-on-query-only-and-based-on-filters-post-filter-at-the-same-time/69562/2 "2016-12-20T13:07:07Z")

</div>

I think you can use the filters aggregation to do this. Still your post  
filter in there and the bits that don't match will be in the `other`  
bucket.

---

<div class="post-metadata">

**Author:** ![Andrey\_Kaprov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrey_kaprov/32/12262_2.png) [@Andrey\_Kaprov](https://discuss.elastic.co/u/Andrey_Kaprov)\
**Post date:** [December 20, 2016, 1:10pm UTC](https://discuss.elastic.co/t/how-can-i-get-aggregations-based-on-query-only-and-based-on-filters-post-filter-at-the-same-time/69562/3 "2016-12-20T13:10:54Z")

</div>

Can you, please, show me an example? I can use BoolQuery.Should instead of post\_filter - this is not principal. The only thing I need is to have an aggs before filtering and after filtering.

---

<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 17, 2017, 1:11pm UTC](https://discuss.elastic.co/t/how-can-i-get-aggregations-based-on-query-only-and-based-on-filters-post-filter-at-the-same-time/69562/4 "2017-01-17T13:11:18Z")

</div>

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