# Facet\_filters question

**URL:** <https://discuss.elastic.co/t/facet-filters-question/6485>\
**Category:** Elasticsearch\
**Created:** [January 25, 2012, 1:39am UTC](https://discuss.elastic.co/t/facet-filters-question/6485 "2012-01-25T01:39:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jessejlt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jessejlt/32/2862_2.png) [@jessejlt](https://discuss.elastic.co/u/jessejlt)\
**Post date:** [January 25, 2012, 1:39am UTC](https://discuss.elastic.co/t/facet-filters-question/6485/1 "2012-01-25T01:39:38Z")

</div>

I'm having a difficult time getting facet\_filters to work. I created a  
gist  
at: [https://gist.github.com/1674000](https://gist.github.com/1674000)

If I understand correctly, I can use a "facet\_filter" to limit the  
scope of  
my requested facet. So given a sample document:

{  
"color": "blue",  
"type": "crayon"  
}

If I wanted to create a facet based on "color" but limit the results  
to  
only records of "type" == "crayon", then a "facet\_filter" is the  
correct  
way to do this, or am I mistaken?

Thanks!  
-jesse

---

<div class="post-metadata">

**Author:** ![David\_Stendardi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_stendardi/32/2990_2.png) [@David\_Stendardi](https://discuss.elastic.co/u/David_Stendardi)\
**Post date:** [January 25, 2012, 7:05am UTC](https://discuss.elastic.co/t/facet-filters-question/6485/2 "2012-01-25T07:05:24Z")

</div>

Hi,

Using facet\_filter you can do something like this :  
{  
facets : {  
"colors" : {  
"terms" : {  
"field": "color"  
},  
"facet\_filter": {  
"term": { "type" : "crayon" }  
}  
}  
}

But unless specific needs, (like multifaceting) you should use a  
filtered query instead and move your filter inside its filter part :

{  
query: {  
filtered: {  
query : ...  
filter: {  
"term": { "type" : "crayon" }  
}  
}  
},  
facets : {  
"colors" : {  
"terms" : {  
"field": "color"  
}  
}  
}

2012/1/25 jessejlt [jessejlt@gmail.com](mailto:jessejlt@gmail.com):

> I'm having a difficult time getting facet\_filters to work. I created a  
> gist  
> at: [gist:1674000 · GitHub](https://gist.github.com/1674000)
> 
> If I understand correctly, I can use a "facet\_filter" to limit the  
> scope of  
> my requested facet. So given a sample document:
> 
> {  
> "color": "blue",  
> "type": "crayon"  
> }
> 
> If I wanted to create a facet based on "color" but limit the results  
> to  
> only records of "type" == "crayon", then a "facet\_filter" is the  
> correct  
> way to do this, or am I mistaken?
> 
> Thanks!  
> -jesse

--  
David Stendardi

---

<div class="post-metadata">

**Author:** ![David\_Stendardi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_stendardi/32/2990_2.png) [@David\_Stendardi](https://discuss.elastic.co/u/David_Stendardi)\
**Post date:** [January 25, 2012, 7:07am UTC](https://discuss.elastic.co/t/facet-filters-question/6485/3 "2012-01-25T07:07:27Z")

</div>

I forgot to mention the error in your gist :  
the facet\_filter is at the same level that the facet name.  
It should be inside your facet (same level as facet\_type, as explained  
here : [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/))

2012/1/25 jessejlt [jessejlt@gmail.com](mailto:jessejlt@gmail.com):

> I'm having a difficult time getting facet\_filters to work. I created a  
> gist  
> at: [gist:1674000 · GitHub](https://gist.github.com/1674000)
> 
> If I understand correctly, I can use a "facet\_filter" to limit the  
> scope of  
> my requested facet. So given a sample document:
> 
> {  
> "color": "blue",  
> "type": "crayon"  
> }
> 
> If I wanted to create a facet based on "color" but limit the results  
> to  
> only records of "type" == "crayon", then a "facet\_filter" is the  
> correct  
> way to do this, or am I mistaken?
> 
> Thanks!  
> -jesse

--  
David Stendardi

---

<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:41am UTC](https://discuss.elastic.co/t/facet-filters-question/6485/4 "2017-07-06T03:41:35Z")

</div>


