# How does facets work

**URL:** https://discuss.elastic.co/t/how-does-facets-work/15036
**Category:** Elasticsearch
**Created:** [December 27, 2013, 5:41pm UTC](https://discuss.elastic.co/t/how-does-facets-work/15036 "2013-12-27T17:41:55Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discuss.elastic.co/u/Volker)
#### Post date: [December 27, 2013, 5:41pm UTC](https://discuss.elastic.co/t/how-does-facets-work/15036/1 "2013-12-27T17:41:55Z")

</div>

Dear Readers

I have a question about facets and doing some filtering based on facets.

In the moment I am using hibernate search in combination with bobobrowse  
for facetting and I am thinking about switching to ES. But before that I  
would like to check whether I can still get the same functionality.

lets asume that I have an index about cars and some facets -- eg. model and  
color.

color  
[] red (10)  
[] blue (5)  
[] green (2)

model  
[] bmw (4)  
[] vw (5)  
[] ford (8)

if I select a model I would like to get only color facets for that model,  
but I still would like to get facets for all models. eg:

color  
[] red (2)  
[] blue (2)  
[] green (1)

model  
[] bmw (4)  
[x] vw (5)  
[] ford (8)

I have searched I did not find an example about this usecase. Is this  
possible and if yes, how do I filter a query to get these results?

Kind regards

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/9d1f3008-aff2-4936-8e6c-7611734e7418%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9d1f3008-aff2-4936-8e6c-7611734e7418%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)
#### Post date: [December 27, 2013, 5:58pm UTC](https://discuss.elastic.co/t/how-does-facets-work/15036/2 "2013-12-27T17:58:13Z")

</div>

Facets work on the documents returned by the query. This behavior will not  
work in your case since you would like to gather facets on a greater set of  
documents, not just the ones returned by the query. To solve this issue,  
elasticsearch provides a post filter, which affects the result document  
set, but not the set of documents that the facets work on.

The term "filter" is a bit overloaded in elasticsearch, so the team renamed  
the post filter to a more explicit "post\_filter":

> **[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.

> <https://github.com/elastic/elasticsearch/issues/4119>
>
> Users tend to use the top-level \`filter\` param for general filtering, instead of… using a \`filtered\` query.
> 
> It should be renamed to \`post\_filter\` to dissuade users from using it unless they understand what it is for and \`filter\` should be deprecated.
> 
> The top level \`filter\` is still valid field for backwards compatibility, but it will removed in the future.

The post filter documentation has some insight on how the filters affect  
the facets. In your case, you want a filtered query (

> **[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.

)  
with the color as the filter, but the model filter will be applied as a  
post filter. Hopefully this makes sense. 🙂

Cheers,

Ivan

On Fri, Dec 27, 2013 at 9:41 AM, Volker [svs@klest.de](mailto:svs@klest.de) wrote:

> Dear Readers
> 
> I have a question about facets and doing some filtering based on facets.
> 
> In the moment I am using hibernate search in combination with bobobrowse  
> for facetting and I am thinking about switching to ES. But before that I  
> would like to check whether I can still get the same functionality.
> 
> lets asume that I have an index about cars and some facets -- eg. model  
> and color.
> 
> color  
> red (10)  
> blue (5)  
> green (2)
> 
> model  
> bmw (4)  
> vw (5)  
> ford (8)
> 
> if I select a model I would like to get only color facets for that model,  
> but I still would like to get facets for all models. eg:
> 
> color  
> red (2)  
> blue (2)  
> green (1)
> 
> model  
> bmw (4)  
> vw (5)  
> ford (8)
> 
> I have searched I did not find an example about this usecase. Is this  
> possible and if yes, how do I filter a query to get these results?
> 
> Kind regards
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/9d1f3008-aff2-4936-8e6c-7611734e7418%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9d1f3008-aff2-4936-8e6c-7611734e7418%40googlegroups.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQDj9Q9EsfvH5%2BE1AkZiCZLVuq3RZkSrRFeaW\_NM3Uc6Gw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQDj9Q9EsfvH5%2BE1AkZiCZLVuq3RZkSrRFeaW_NM3Uc6Gw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discuss.elastic.co/u/Volker)
#### Post date: [December 27, 2013, 6:24pm UTC](https://discuss.elastic.co/t/how-does-facets-work/15036/3 "2013-12-27T18:24:02Z")

</div>

Dear Ivan

thank you very much for your pointers. I will read through it and come  
back, if I have further questions!

thanks

Am Freitag, 27. Dezember 2013 18:58:13 UTC+1 schrieb Ivan Brusic:

> Facets work on the documents returned by the query. This behavior will  
> not work in your case since you would like to gather facets on a greater  
> set of documents, not just the ones returned by the query. To solve this  
> issue, elasticsearch provides a post filter, which affects the result  
> document set, but not the set of documents that the facets work on.
> 
> The term "filter" is a bit overloaded in elasticsearch, so the team  
> renamed the post filter to a more explicit "post\_filter":
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-post-filter.html)  
> [Rename filter param to search api "post\_filter" · Issue #4119 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/4119)
> 
> The post filter documentation has some insight on how the filters affect  
> the facets. In your case, you want a filtered query (  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html) )  
> with the color as the filter, but the model filter will be applied as a  
> post filter. Hopefully this makes sense. 🙂
> 
> Cheers,
> 
> Ivan
> 
> On Fri, Dec 27, 2013 at 9:41 AM, Volker \<[s...@klest.de](mailto:s...@klest.de) \<javascript:\>\>wrote:
> 
> > Dear Readers
> > 
> > I have a question about facets and doing some filtering based on facets.
> > 
> > In the moment I am using hibernate search in combination with bobobrowse  
> > for facetting and I am thinking about switching to ES. But before that I  
> > would like to check whether I can still get the same functionality.
> > 
> > lets asume that I have an index about cars and some facets -- eg. model  
> > and color.
> > 
> > color  
> > red (10)  
> > blue (5)  
> > green (2)
> > 
> > model  
> > bmw (4)  
> > vw (5)  
> > ford (8)
> > 
> > if I select a model I would like to get only color facets for that model,  
> > but I still would like to get facets for all models. eg:
> > 
> > color  
> > red (2)  
> > blue (2)  
> > green (1)
> > 
> > model  
> > bmw (4)  
> > vw (5)  
> > ford (8)
> > 
> > I have searched I did not find an example about this usecase. Is this  
> > possible and if yes, how do I filter a query to get these results?
> > 
> > Kind regards
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/9d1f3008-aff2-4936-8e6c-7611734e7418%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9d1f3008-aff2-4936-8e6c-7611734e7418%40googlegroups.com)  
> > .  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/85f17064-1226-4f0b-9435-2b17790afd4e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/85f17064-1226-4f0b-9435-2b17790afd4e%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discuss.elastic.co/u/Volker)
#### Post date: [December 30, 2013, 3:57pm UTC](https://discuss.elastic.co/t/how-does-facets-work/15036/4 "2013-12-30T15:57:17Z")

</div>

> Hopefully this makes sense. 🙂

after reading through your provided links... not totally 😉

If I apply the selected facet before the the facet calculation, the facet  
will count only elements, that are part of the result. That would mean,  
that I would not get numbers for the not selected models (in this case no  
numbers for bmw and ford).

If I apply the selected facet as a post\_filter, I will get the correct  
numbers for the unselected models, but the facets for the color does not  
represent the numbers for the selected model, but for all models.

But probably I have not understood it, as it was meant. 🙂

Could you please elaborate further, how the query/filter for a selected  
facet should be formulated? I have not found any example for this. If there  
is one online, I would appreciate a link to that example.

Kind regards

Am Freitag, 27. Dezember 2013 18:58:13 UTC+1 schrieb Ivan Brusic:

> Facets work on the documents returned by the query. This behavior will  
> not work in your case since you would like to gather facets on a greater  
> set of documents, not just the ones returned by the query. To solve this  
> issue, elasticsearch provides a post filter, which affects the result  
> document set, but not the set of documents that the facets work on.
> 
> The term "filter" is a bit overloaded in elasticsearch, so the team  
> renamed the post filter to a more explicit "post\_filter":
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-post-filter.html)  
> [Rename filter param to search api "post\_filter" · Issue #4119 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/4119)
> 
> The post filter documentation has some insight on how the filters affect  
> the facets. In your case, you want a filtered query (  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html) )  
> with the color as the filter, but the model filter will be applied as a  
> post filter. Hopefully this makes sense. 🙂
> 
> Cheers,
> 
> Ivan
> 
> On Fri, Dec 27, 2013 at 9:41 AM, Volker \<[s...@klest.de](mailto:s...@klest.de) \<javascript:\>\>wrote:
> 
> > Dear Readers
> > 
> > I have a question about facets and doing some filtering based on facets.
> > 
> > In the moment I am using hibernate search in combination with bobobrowse  
> > for facetting and I am thinking about switching to ES. But before that I  
> > would like to check whether I can still get the same functionality.
> > 
> > lets asume that I have an index about cars and some facets -- eg. model  
> > and color.
> > 
> > color  
> > red (10)  
> > blue (5)  
> > green (2)
> > 
> > model  
> > bmw (4)  
> > vw (5)  
> > ford (8)
> > 
> > if I select a model I would like to get only color facets for that model,  
> > but I still would like to get facets for all models. eg:
> > 
> > color  
> > red (2)  
> > blue (2)  
> > green (1)
> > 
> > model  
> > bmw (4)  
> > vw (5)  
> > ford (8)
> > 
> > I have searched I did not find an example about this usecase. Is this  
> > possible and if yes, how do I filter a query to get these results?
> > 
> > Kind regards
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/9d1f3008-aff2-4936-8e6c-7611734e7418%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9d1f3008-aff2-4936-8e6c-7611734e7418%40googlegroups.com)  
> > .  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/2609a3ee-1124-4bc1-95e6-c6e7cdb06128%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2609a3ee-1124-4bc1-95e6-c6e7cdb06128%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)
#### Post date: [December 30, 2013, 5:36pm UTC](https://discuss.elastic.co/t/how-does-facets-work/15036/5 "2013-12-30T17:36:31Z")

</div>

After reading your scenario again, I have modified slightly my suggestion.  
You would still need to use a post filter since the facets need to work on  
set of documents that is greater than the set returned. You can apply the  
same post filter as a facet filter on the facet you want to reduce. Here is  
an example:

> <https://gist.github.com/brusic/8185134>

--  
Ivan

On Mon, Dec 30, 2013 at 7:57 AM, Volker [svs@klest.de](mailto:svs@klest.de) wrote:

> > Hopefully this makes sense. 🙂
> 
> after reading through your provided links... not totally 😉
> 
> If I apply the selected facet before the the facet calculation, the facet  
> will count only elements, that are part of the result. That would mean,  
> that I would not get numbers for the not selected models (in this case no  
> numbers for bmw and ford).
> 
> If I apply the selected facet as a post\_filter, I will get the correct  
> numbers for the unselected models, but the facets for the color does not  
> represent the numbers for the selected model, but for all models.
> 
> But probably I have not understood it, as it was meant. 🙂
> 
> Could you please elaborate further, how the query/filter for a selected  
> facet should be formulated? I have not found any example for this. If there  
> is one online, I would appreciate a link to that example.
> 
> Kind regards
> 
> Am Freitag, 27. Dezember 2013 18:58:13 UTC+1 schrieb Ivan Brusic:
> 
> > Facets work on the documents returned by the query. This behavior will  
> > not work in your case since you would like to gather facets on a greater  
> > set of documents, not just the ones returned by the query. To solve this  
> > issue, elasticsearch provides a post filter, which affects the result  
> > document set, but not the set of documents that the facets work on.
> > 
> > The term "filter" is a bit overloaded in elasticsearch, so the team  
> > renamed the post filter to a more explicit "post\_filter":
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/)  
> > reference/current/search-request-post-filter.html  
> > [Rename filter param to search api "post\_filter" · Issue #4119 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/4119)
> > 
> > The post filter documentation has some insight on how the filters affect  
> > the facets. In your case, you want a filtered query (  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/)  
> > reference/current/query-dsl-filtered-query.html ) with the color as the  
> > filter, but the model filter will be applied as a post filter. Hopefully  
> > this makes sense. 🙂
> > 
> > Cheers,
> > 
> > Ivan
> > 
> > On Fri, Dec 27, 2013 at 9:41 AM, Volker [s...@klest.de](mailto:s...@klest.de) wrote:
> > 
> > > Dear Readers
> > > 
> > > I have a question about facets and doing some filtering based on facets.
> > > 
> > > In the moment I am using hibernate search in combination with bobobrowse  
> > > for facetting and I am thinking about switching to ES. But before that I  
> > > would like to check whether I can still get the same functionality.
> > > 
> > > lets asume that I have an index about cars and some facets -- eg. model  
> > > and color.
> > > 
> > > color  
> > > red (10)  
> > > blue (5)  
> > > green (2)
> > > 
> > > model  
> > > bmw (4)  
> > > vw (5)  
> > > ford (8)
> > > 
> > > if I select a model I would like to get only color facets for that  
> > > model, but I still would like to get facets for all models. eg:
> > > 
> > > color  
> > > red (2)  
> > > blue (2)  
> > > green (1)
> > > 
> > > model  
> > > bmw (4)  
> > > vw (5)  
> > > ford (8)
> > > 
> > > I have searched I did not find an example about this usecase. Is this  
> > > possible and if yes, how do I filter a query to get these results?
> > > 
> > > Kind regards
> > > 
> > > --  
> > > You received this message because you are subscribed to the Google  
> > > Groups "elasticsearch" group.  
> > > To unsubscribe from this group and stop receiving emails from it, send  
> > > an email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).
> > > 
> > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > msgid/elasticsearch/9d1f3008-aff2-4936-8e6c-7611734e7418%  
> > > [40googlegroups.com](http://40googlegroups.com).  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/2609a3ee-1124-4bc1-95e6-c6e7cdb06128%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2609a3ee-1124-4bc1-95e6-c6e7cdb06128%40googlegroups.com)  
> > .
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQC1i0wUL2gMVEt37PdZLNZXyTi7PTNGKUzDedR6Gpx-5w%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQC1i0wUL2gMVEt37PdZLNZXyTi7PTNGKUzDedR6Gpx-5w%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discuss.elastic.co/u/Volker)
#### Post date: [December 30, 2013, 5:55pm UTC](https://discuss.elastic.co/t/how-does-facets-work/15036/6 "2013-12-30T17:55:46Z")

</div>

Dear Ivan

thank you very much for your fast and helpful response. I will create some  
demo data and play with your snippet.

I really appreciate your help!

btw... I think my scenario is a very standard scenario for working with  
facets -- e.g. every online shop is working in this fashion with facets.

kind regards  
Volker

Am Montag, 30. Dezember 2013 18:36:31 UTC+1 schrieb Ivan Brusic:

> After reading your scenario again, I have modified slightly my suggestion.  
> You would still need to use a post filter since the facets need to work on  
> set of documents that is greater than the set returned. You can apply the  
> same post filter as a facet filter on the facet you want to reduce. Here is  
> an example:
> 
> [facet filter example · GitHub](https://gist.github.com/brusic/8185134)
> 
> --  
> Ivan

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/18a41661-021b-4833-86f1-423223da7a20%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/18a41661-021b-4833-86f1-423223da7a20%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [January 1, 2014, 8:16pm UTC](https://discuss.elastic.co/t/how-does-facets-work/15036/7 "2014-01-01T20:16:59Z")

</div>

Hey

Small correction: When using the global flag, facets ignore the query. See

> **[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.

That might help in your case (though from my experience it is likely that  
you will end up with a couple of facets, from which each has a different  
facet filter. But your mileage may vary).

--Alex

On Fri, Dec 27, 2013 at 6:58 PM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> Facets work on the documents returned by the query. This behavior will  
> not work in your case since you would like to gather facets on a greater  
> set of documents, not just the ones returned by the query. To solve this  
> issue, elasticsearch provides a post filter, which affects the result  
> document set, but not the set of documents that the facets work on.
> 
> The term "filter" is a bit overloaded in elasticsearch, so the team  
> renamed the post filter to a more explicit "post\_filter":
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-post-filter.html)  
> [Rename filter param to search api "post\_filter" · Issue #4119 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/4119)
> 
> The post filter documentation has some insight on how the filters affect  
> the facets. In your case, you want a filtered query (  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html) )  
> with the color as the filter, but the model filter will be applied as a  
> post filter. Hopefully this makes sense. 🙂
> 
> Cheers,
> 
> Ivan
> 
> On Fri, Dec 27, 2013 at 9:41 AM, Volker [svs@klest.de](mailto:svs@klest.de) wrote:
> 
> > Dear Readers
> > 
> > I have a question about facets and doing some filtering based on facets.
> > 
> > In the moment I am using hibernate search in combination with bobobrowse  
> > for facetting and I am thinking about switching to ES. But before that I  
> > would like to check whether I can still get the same functionality.
> > 
> > lets asume that I have an index about cars and some facets -- eg. model  
> > and color.
> > 
> > color  
> > red (10)  
> > blue (5)  
> > green (2)
> > 
> > model  
> > bmw (4)  
> > vw (5)  
> > ford (8)
> > 
> > if I select a model I would like to get only color facets for that model,  
> > but I still would like to get facets for all models. eg:
> > 
> > color  
> > red (2)  
> > blue (2)  
> > green (1)
> > 
> > model  
> > bmw (4)  
> > vw (5)  
> > ford (8)
> > 
> > I have searched I did not find an example about this usecase. Is this  
> > possible and if yes, how do I filter a query to get these results?
> > 
> > Kind regards
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/9d1f3008-aff2-4936-8e6c-7611734e7418%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9d1f3008-aff2-4936-8e6c-7611734e7418%40googlegroups.com)  
> > .  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQDj9Q9EsfvH5%2BE1AkZiCZLVuq3RZkSrRFeaW\_NM3Uc6Gw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQDj9Q9EsfvH5%2BE1AkZiCZLVuq3RZkSrRFeaW_NM3Uc6Gw%40mail.gmail.com)  
> .
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAGCwEM-FX\_La\_JB3kyEp7af%2B3A2hEQ2DQ2x0skFi-c\_fFppPjw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGCwEM-FX_La_JB3kyEp7af%2B3A2hEQ2DQ2x0skFi-c_fFppPjw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 1:58am UTC](https://discuss.elastic.co/t/how-does-facets-work/15036/8 "2017-07-06T01:58:45Z")

</div>


