# Filter works, but facets are for entire dataset. How do I limit them?

**URL:** <https://discuss.elastic.co/t/filter-works-but-facets-are-for-entire-dataset-how-do-i-limit-them/6709>\
**Category:** Elasticsearch\
**Created:** [February 15, 2012, 5:51am UTC](https://discuss.elastic.co/t/filter-works-but-facets-are-for-entire-dataset-how-do-i-limit-them/6709 "2012-02-15T05:51:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aschaar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aschaar/32/2991_2.png) [@aschaar](https://discuss.elastic.co/u/aschaar)\
**Post date:** [February 15, 2012, 5:51am UTC](https://discuss.elastic.co/t/filter-works-but-facets-are-for-entire-dataset-how-do-i-limit-them/6709/1 "2012-02-15T05:51:22Z")

</div>

I would like to only show the facets that are found within the  
filter. How can I limit them ? Thanks.

var query = {  
"filter":{  
"terms":{  
"tags.split": ["person","politician"],  
"minimum\_match": 1  
}  
},  
"facets":{  
"tags":{  
"terms" : { "field": "tags", "size":20 }  
},  
},  
"sort":[  
{ "meta.view\_count": "desc" },  
{ "meta.answer\_count": "desc" }  
],  
"size": 20  
}

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [February 15, 2012, 9:07am UTC](https://discuss.elastic.co/t/filter-works-but-facets-are-for-entire-dataset-how-do-i-limit-them/6709/2 "2012-02-15T09:07:39Z")

</div>

have a look at "filtered":

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

> **[Bird’s Eye View on ElasticSearch its Query DSL](https://karussell.wordpress.com/2012/01/19/birds-eye-view-on-elasticsearch-its-query-dsl/)**
>
> I’ve copied the whole post into a gist so that you can simply clone, copy and paste the important stuff and even could contribute easily. Several times per month there pop up questions regard…

Peter.

On Feb 15, 6:51 am, Arron [asch...@gmail.com](mailto:asch...@gmail.com) wrote:

> I would like to only show the facets that are found within the  
> filter. How can I limit them ? Thanks.
> 
> var query = {  
> "filter":{  
> "terms":{  
> "tags.split": ["person","politician"],  
> "minimum\_match": 1  
> }  
> },  
> "facets":{  
> "tags":{  
> "terms" : { "field": "tags", "size":20 }  
> },  
> },  
> "sort":[  
> { "meta.view\_count": "desc" },  
> { "meta.answer\_count": "desc" }  
> ],  
> "size": 20  
> }

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [February 15, 2012, 1:30pm UTC](https://discuss.elastic.co/t/filter-works-but-facets-are-for-entire-dataset-how-do-i-limit-them/6709/3 "2012-02-15T13:30:20Z")

</div>

filted query (within the search query element) will cause the facets to only be aggregated to the provided query (which is filtered). If additional filtering is required per specific facet, you can have a facet\_filter associated with a facet.

On Wednesday, February 15, 2012 at 11:07 AM, Karussell wrote:

> have a look at "filtered":
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html)  
> [Bird’s Eye View on ElasticSearch its Query DSL | Karussell](http://karussell.wordpress.com/2012/01/19/birds-eye-view-on-elasticsearch-its-query-dsl/)
> 
> Peter.
> 
> On Feb 15, 6:51 am, Arron \<[asch...@gmail.com](mailto:asch...@gmail.com) ([http://gmail.com](http://gmail.com))\> wrote:
> 
> > I would like to only show the facets that are found within the  
> > filter. How can I limit them ? Thanks.
> > 
> > var query = {  
> > "filter":{  
> > "terms":{  
> > "tags.split": ["person","politician"],  
> > "minimum\_match": 1  
> > }  
> > },  
> > "facets":{  
> > "tags":{  
> > "terms" : { "field": "tags", "size":20 }  
> > },  
> > },  
> > "sort":[  
> > { "meta.view\_count": "desc" },  
> > { "meta.answer\_count": "desc" }  
> > ],  
> > "size": 20  
> > }

---

<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:** [July 6, 2017, 3:39am UTC](https://discuss.elastic.co/t/filter-works-but-facets-are-for-entire-dataset-how-do-i-limit-them/6709/4 "2017-07-06T03:39:15Z")

</div>


