# Term aggregation/facet NOT INCLUDING the faceted field

**URL:** <https://discuss.elastic.co/t/term-aggregation-facet-not-including-the-faceted-field/18948>\
**Category:** Elasticsearch\
**Created:** [July 29, 2014, 9:32am UTC](https://discuss.elastic.co/t/term-aggregation-facet-not-including-the-faceted-field/18948 "2014-07-29T09:32:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam\_porat](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@adam\_porat](https://discuss.elastic.co/u/adam_porat)\
**Post date:** [July 29, 2014, 9:32am UTC](https://discuss.elastic.co/t/term-aggregation-facet-not-including-the-faceted-field/18948/1 "2014-07-29T09:32:03Z")

</div>

Hi,

```
Faceted Navigation, as can be seen in *LinkedIn *and *Booking.com*, 

```

include the fundamental characteristic that the value-count of each faceted  
field is constrained by the query (set of matched documents), \*excluding \*any  
constraint on the given field itself.

```
For example, in LinkedIn, If the "locations" facet is currently 

```

filtered with "Canada" (the "Canada" check-box is selected), the facet will  
still include counts for the other locations.

```
It seems that Elasticsearch lacks the feature to count the terms in the 

```

set of matched documents, AS IF the faceted field itself was not a part of  
the query. Am I correct?

```
And thus, if I have *n* faceted fields in my faceted navigation, each 

```

clicked checkbox involves executing _n_ separate queries - one to modify  
the actual result set, and _n-1_ to rebuild the other facets (each one with  
a query which excludes its field).

```
 Did I get this right? any better ways to do this?

```

Thank you.

--  
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/89ecc3ed-2d58-4253-b0d1-a7d12495c89d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/89ecc3ed-2d58-4253-b0d1-a7d12495c89d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 29, 2014, 9:39am UTC](https://discuss.elastic.co/t/term-aggregation-facet-not-including-the-faceted-field/18948/2 "2014-07-29T09:39:53Z")

</div>

In that case, you should look at post filters instead of adding filters to the query.

My 2 cents

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

> Le 29 juil. 2014 à 11:32, Adam Porat [adam.porat@gmail.com](mailto:adam.porat@gmail.com) a écrit :
> 
> Hi,
> 
> ```
> Faceted Navigation, as can be seen in LinkedIn and Booking.com, include the fundamental characteristic that the value-count of each faceted field is constrained by the query (set of matched documents), excluding any constraint on the given field itself.
> 
> For example, in LinkedIn, If the "locations" facet is currently filtered with "Canada" (the "Canada" check-box is selected), the facet will still include counts for the other locations.
> 
> It seems that Elasticsearch lacks the feature to count the terms in the set of matched documents, AS IF the faceted field itself was not a part of the query. Am I correct?
> 
> And thus, if I have n faceted fields in my faceted navigation, each clicked checkbox involves executing n separate queries - one to modify the actual result set, and n-1 to rebuild the other facets (each one with a query which excludes its field).
> 
> Did I get this right? any better ways to do this?
> 
> ```
> 
> Thank you.
> 
> --  
> 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/89ecc3ed-2d58-4253-b0d1-a7d12495c89d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/89ecc3ed-2d58-4253-b0d1-a7d12495c89d%40googlegroups.com).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/50A5A6FC-FDFC-4C69-9E32-1F9B33FC127C%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/50A5A6FC-FDFC-4C69-9E32-1F9B33FC127C%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [July 29, 2014, 9:42am UTC](https://discuss.elastic.co/t/term-aggregation-facet-not-including-the-faceted-field/18948/3 "2014-07-29T09:42:03Z")

</div>

Hi Adam, you can do it today in a single query using post\_filter, but it is  
true that it is not very convenient. See discussion on  
[exclude aggregator by miguelnmiranda · Pull Request #7020 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/pull/7020) for more  
information.

On Tue, Jul 29, 2014 at 11:32 AM, Adam Porat [adam.porat@gmail.com](mailto:adam.porat@gmail.com) wrote:

> Hi,
> 
> ```
> Faceted Navigation, as can be seen in *LinkedIn *and *Booking.com*,
> 
> ```
> 
> include the fundamental characteristic that the value-count of each faceted  
> field is constrained by the query (set of matched documents), \*excluding \*any  
> constraint on the given field itself.
> 
> ```
> For example, in LinkedIn, If the "locations" facet is currently
> 
> ```
> 
> filtered with "Canada" (the "Canada" check-box is selected), the facet will  
> still include counts for the other locations.
> 
> ```
> It seems that Elasticsearch lacks the feature to count the terms in
> 
> ```
> 
> the set of matched documents, AS IF the faceted field itself was not a part  
> of the query. Am I correct?
> 
> ```
> And thus, if I have *n* faceted fields in my faceted navigation, each
> 
> ```
> 
> clicked checkbox involves executing _n_ separate queries - one to modify  
> the actual result set, and _n-1_ to rebuild the other facets (each one  
> with a query which excludes its field).
> 
> ```
> Did I get this right? any better ways to do this?
> 
> ```
> 
> Thank you.
> 
> --  
> 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/89ecc3ed-2d58-4253-b0d1-a7d12495c89d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/89ecc3ed-2d58-4253-b0d1-a7d12495c89d%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/89ecc3ed-2d58-4253-b0d1-a7d12495c89d%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/89ecc3ed-2d58-4253-b0d1-a7d12495c89d%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
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/CAL6Z4j7v075s0C9uLaOSDzWVFF\_B%3Dp0BequyCr3ooAC1Vwp-wA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j7v075s0C9uLaOSDzWVFF_B%3Dp0BequyCr3ooAC1Vwp-wA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![adam\_porat](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@adam\_porat](https://discuss.elastic.co/u/adam_porat)\
**Post date:** [July 29, 2014, 9:48am UTC](https://discuss.elastic.co/t/term-aggregation-facet-not-including-the-faceted-field/18948/4 "2014-07-29T09:48:02Z")

</div>

Hi David,  
post filter could have been a solution for single faceted-field  
scenario. However each facet in a multi-facet scenario needs to be affected  
by constraints on the other faceted-fields.

בתאריך יום שלישי, 29 ביולי 2014 12:40:24 UTC+3, מאת David Pilato:

> In that case, you should look at post filters instead of adding filters to  
> the query.
> 
> My 2 cents
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 29 juil. 2014 à 11:32, Adam Porat \<[adam....@gmail.com](mailto:adam....@gmail.com) \<javascript:\>\> a  
> écrit :
> 
> Hi,
> 
> ```
> Faceted Navigation, as can be seen in *LinkedIn *and *Booking.com 
> 
> ```
> 
> [http://Booking.com](http://Booking.com)\*, include the fundamental characteristic that the  
> value-count of each faceted field is constrained by the query (set of  
> matched documents), \*excluding \*any constraint on the given field itself.
> 
> ```
> For example, in LinkedIn, If the "locations" facet is currently 
> 
> ```
> 
> filtered with "Canada" (the "Canada" check-box is selected), the facet will  
> still include counts for the other locations.
> 
> ```
> It seems that Elasticsearch lacks the feature to count the terms in 
> 
> ```
> 
> the set of matched documents, AS IF the faceted field itself was not a part  
> of the query. Am I correct?
> 
> ```
> And thus, if I have *n* faceted fields in my faceted navigation, each 
> 
> ```
> 
> clicked checkbox involves executing _n_ separate queries - one to modify  
> the actual result set, and _n-1_ to rebuild the other facets (each one  
> with a query which excludes its field).
> 
> ```
> Did I get this right? any better ways to do this?
> 
> ```
> 
> Thank you.
> 
> --  
> 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/89ecc3ed-2d58-4253-b0d1-a7d12495c89d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/89ecc3ed-2d58-4253-b0d1-a7d12495c89d%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/89ecc3ed-2d58-4253-b0d1-a7d12495c89d%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/89ecc3ed-2d58-4253-b0d1-a7d12495c89d%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/05489778-c3f1-42ff-922d-8a3175be17dc%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/05489778-c3f1-42ff-922d-8a3175be17dc%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![adam\_porat](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@adam\_porat](https://discuss.elastic.co/u/adam_porat)\
**Post date:** [July 29, 2014, 10:04am UTC](https://discuss.elastic.co/t/term-aggregation-facet-not-including-the-faceted-field/18948/5 "2014-07-29T10:04:27Z")

</div>

Hi Adrien, it looks like your link gives the best answer, basically use the  
construct:

{  
"query": {  
"filtered": {  
"query": "your query goes here",  
"filter": "filters to take into account for top-hits and aggs"  
}  
},  
"post\_filter" : "filters to take into account for top-hits only",  
"aggs": {  
"my\_filter": {  
"filter": "filter to take into account for aggs only"  
}  
}}

בתאריך יום שלישי, 29 ביולי 2014 12:42:13 UTC+3, מאת Adrien Grand:

> Hi Adam, you can do it today in a single query using post\_filter, but it  
> is true that it is not very convenient. See discussion on  
> [exclude aggregator by miguelnmiranda · Pull Request #7020 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/pull/7020) for more  
> information.
> 
> On Tue, Jul 29, 2014 at 11:32 AM, Adam Porat \<[adam....@gmail.com](mailto:adam....@gmail.com)  
> \<javascript:\>\> wrote:
> 
> > Hi,
> > 
> > ```
> > Faceted Navigation, as can be seen in *LinkedIn *and *Booking.com*, 
> > 
> > ```
> > 
> > include the fundamental characteristic that the value-count of each faceted  
> > field is constrained by the query (set of matched documents), \*excluding  
> > \*any constraint on the given field itself.
> > 
> > ```
> > For example, in LinkedIn, If the "locations" facet is currently 
> > 
> > ```
> > 
> > filtered with "Canada" (the "Canada" check-box is selected), the facet will  
> > still include counts for the other locations.
> > 
> > ```
> > It seems that Elasticsearch lacks the feature to count the terms in 
> > 
> > ```
> > 
> > the set of matched documents, AS IF the faceted field itself was not a part  
> > of the query. Am I correct?
> > 
> > ```
> > And thus, if I have *n* faceted fields in my faceted navigation, 
> > 
> > ```
> > 
> > each clicked checkbox involves executing _n_ separate queries - one to  
> > modify the actual result set, and _n-1_ to rebuild the other facets  
> > (each one with a query which excludes its field).
> > 
> > ```
> > Did I get this right? any better ways to do this?
> > 
> > ```
> > 
> > Thank you.
> > 
> > --  
> > 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/89ecc3ed-2d58-4253-b0d1-a7d12495c89d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/89ecc3ed-2d58-4253-b0d1-a7d12495c89d%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/89ecc3ed-2d58-4253-b0d1-a7d12495c89d%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/89ecc3ed-2d58-4253-b0d1-a7d12495c89d%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> Adrien Grand

--  
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/82cf53b7-6028-42c0-8f68-2ebc430b5cf2%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/82cf53b7-6028-42c0-8f68-2ebc430b5cf2%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:12am UTC](https://discuss.elastic.co/t/term-aggregation-facet-not-including-the-faceted-field/18948/6 "2017-07-06T01:12:34Z")

</div>


