# Exclude one filter per facet

**URL:** <https://discuss.elastic.co/t/exclude-one-filter-per-facet/8761>\
**Category:** Elasticsearch\
**Created:** [August 16, 2012, 4:07pm UTC](https://discuss.elastic.co/t/exclude-one-filter-per-facet/8761 "2012-08-16T16:07:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fermin\_Silva](https://avatars.discourse-cdn.com/v4/letter/f/ebca7d/32.png) [@Fermin\_Silva](https://discuss.elastic.co/u/Fermin_Silva)\
**Post date:** [August 16, 2012, 4:07pm UTC](https://discuss.elastic.co/t/exclude-one-filter-per-facet/8761/1 "2012-08-16T16:07:44Z")

</div>

Hi everyone, my first message here!

In my company we are considering to migrate from SOLR to ES, so I'm looking  
if all business criteria can be met with ES without to much of a hassle.  
So far I have everything pretty much worked out except for filter exclusion.  
Let me give you an example:

I want to filter out documents whose field 'A' is equal to 'a', and I want  
to facet the field 'A' at the same time, excluding of course the previous  
filter.  
I know that you can put the filter 'outside' the query in order to get the  
facets without that filter applied, like:

# ElasticSearch (mind the minor errors that i could have here)

{  
"query : { "match\_all" : { } },  
"filter" : { "term : { "A" : "a" } },  
"facets" : {  
"A" : { "terms" : { "field" : "A" } } //this should exclude the filter A:a  
}  
}

# SOLR

&q=:_:_  
&fq={!tag=Aa}A:a  
&facet=true&facet.field={!ex=Aa}A

This is very nice, but what happens if i have multiple filters and facets  
that each one should exclude each other?  
Example:

filter=A:a  
filter=B:b  
filter=C:c

facet={exclude filter A:a}A  
facet={exclude filter B:b}B  
facet={exclude filter C:c}C

That is, for facet A I want to keep all filters except A:a, for facet B all  
except B:b, and so on.  
The most obvious way would be to do n queries (one per each of the n  
facets), but I'd like to stay away from that.

Thanks in advance!  
Fermin

--

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [August 17, 2012, 8:58am UTC](https://discuss.elastic.co/t/exclude-one-filter-per-facet/8761/2 "2012-08-17T08:58:52Z")

</div>

Hi Fermin,

I think you use the 'facet\_filter' for this. Which is described here:

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

It basically allows you to specify a filter per facet to reduce the  
count, so instead of specifying what filters should be excluded, you  
specify what filters do apply on a specific facet. This is different  
from Solr's excluding facets, but you can basically achieve the  
same behavior.

Martijn

On 16 August 2012 18:07, Fermin Silva [silvafermn@gmail.com](mailto:silvafermn@gmail.com) wrote:

> Hi everyone, my first message here!
> 
> In my company we are considering to migrate from SOLR to ES, so I'm looking  
> if all business criteria can be met with ES without to much of a hassle.  
> So far I have everything pretty much worked out except for filter exclusion.  
> Let me give you an example:
> 
> I want to filter out documents whose field 'A' is equal to 'a', and I want  
> to facet the field 'A' at the same time, excluding of course the previous  
> filter.  
> I know that you can put the filter 'outside' the query in order to get the  
> facets without that filter applied, like:
> 
> # Elasticsearch (mind the minor errors that i could have here)
> 
> {  
> "query : { "match\_all" : { } },  
> "filter" : { "term : { "A" : "a" } },  
> "facets" : {  
> "A" : { "terms" : { "field" : "A" } } //this should exclude the filter A:a  
> }  
> }
> 
> # SOLR
> 
> &q=:_:_  
> &fq={!tag=Aa}A:a  
> &facet=true&facet.field={!ex=Aa}A
> 
> This is very nice, but what happens if i have multiple filters and facets  
> that each one should exclude each other?  
> Example:
> 
> filter=A:a  
> filter=B:b  
> filter=C:c
> 
> facet={exclude filter A:a}A  
> facet={exclude filter B:b}B  
> facet={exclude filter C:c}C
> 
> That is, for facet A I want to keep all filters except A:a, for facet B all  
> except B:b, and so on.  
> The most obvious way would be to do n queries (one per each of the n  
> facets), but I'd like to stay away from that.
> 
> Thanks in advance!  
> Fermin
> 
> --

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<div class="post-metadata">

**Author:** ![Fermin\_Silva](https://avatars.discourse-cdn.com/v4/letter/f/ebca7d/32.png) [@Fermin\_Silva](https://discuss.elastic.co/u/Fermin_Silva)\
**Post date:** [August 17, 2012, 2:40pm UTC](https://discuss.elastic.co/t/exclude-one-filter-per-facet/8761/3 "2012-08-17T14:40:11Z")

</div>

Hi Martijn, thanks for the response.

You mean like:

query for items: match\_all

Facet A: facet\_filter B:b, C:c  
Facet B: facet\_filter A:a, C:c  
Facet C: facet\_filter A:a, B:b

'external' filters (do not apply for facets): filter A:a, B:b, C:c

?

2012/8/17 Martijn v Groningen [martijn.v.groningen@gmail.com](mailto:martijn.v.groningen@gmail.com)

> Hi Fermin,
> 
> I think you use the 'facet\_filter' for this. Which is described here:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)
> 
> It basically allows you to specify a filter per facet to reduce the  
> count, so instead of specifying what filters should be excluded, you  
> specify what filters do apply on a specific facet. This is different  
> from Solr's excluding facets, but you can basically achieve the  
> same behavior.
> 
> Martijn
> 
> On 16 August 2012 18:07, Fermin Silva [silvafermn@gmail.com](mailto:silvafermn@gmail.com) wrote:
> 
> > Hi everyone, my first message here!
> > 
> > In my company we are considering to migrate from SOLR to ES, so I'm  
> > looking  
> > if all business criteria can be met with ES without to much of a hassle.  
> > So far I have everything pretty much worked out except for filter  
> > exclusion.  
> > Let me give you an example:
> > 
> > I want to filter out documents whose field 'A' is equal to 'a', and I  
> > want  
> > to facet the field 'A' at the same time, excluding of course the previous  
> > filter.  
> > I know that you can put the filter 'outside' the query in order to get  
> > the  
> > facets without that filter applied, like:
> > 
> > # Elasticsearch (mind the minor errors that i could have here)
> > 
> > {  
> > "query : { "match\_all" : { } },  
> > "filter" : { "term : { "A" : "a" } },  
> > "facets" : {  
> > "A" : { "terms" : { "field" : "A" } } //this should exclude the filter  
> > A:a  
> > }  
> > }
> > 
> > # SOLR
> > 
> > &q=:_:_  
> > &fq={!tag=Aa}A:a  
> > &facet=true&facet.field={!ex=Aa}A
> > 
> > This is very nice, but what happens if i have multiple filters and facets  
> > that each one should exclude each other?  
> > Example:
> > 
> > filter=A:a  
> > filter=B:b  
> > filter=C:c
> > 
> > facet={exclude filter A:a}A  
> > facet={exclude filter B:b}B  
> > facet={exclude filter C:c}C
> > 
> > That is, for facet A I want to keep all filters except A:a, for facet B  
> > all  
> > except B:b, and so on.  
> > The most obvious way would be to do n queries (one per each of the n  
> > facets), but I'd like to stay away from that.
> > 
> > Thanks in advance!  
> > Fermin
> > 
> > --
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen
> 
> --

--

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [August 17, 2012, 2:50pm UTC](https://discuss.elastic.co/t/exclude-one-filter-per-facet/8761/4 "2012-08-17T14:50:37Z")

</div>

> You mean like:
> 
> query for items: match\_all
> 
> Facet A: facet\_filter B:b, C:c  
> Facet B: facet\_filter A:a, C:c  
> Facet C: facet\_filter A:a, B:b  
> Correct, that is what I meant.
> 
> 'external' filters (do not apply for facets): filter A:a, B:b, C:c?  
> No they don't, only for the query result (hits).

Martijn

--

---

<div class="post-metadata">

**Author:** ![dmitry\_polushkin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dmitry_polushkin/32/1326_2.png) [@dmitry\_polushkin](https://discuss.elastic.co/u/dmitry_polushkin)\
**Post date:** [July 24, 2014, 9:45pm UTC](https://discuss.elastic.co/t/exclude-one-filter-per-facet/8761/5 "2014-07-24T21:45:06Z")

</div>

Is there are a better solution for now, maybe something with the  
aggregations?

On Friday, August 17, 2012 3:50:37 PM UTC+1, Martijn v Groningen wrote:

> > You mean like:
> > 
> > query for items: match\_all
> > 
> > Facet A: facet\_filter B:b, C:c  
> > Facet B: facet\_filter A:a, C:c  
> > Facet C: facet\_filter A:a, B:b  
> > Correct, that is what I meant.
> > 
> > 'external' filters (do not apply for facets): filter A:a, B:b, C:c?  
> > No they don't, only for the query result (hits).
> 
> Martijn

--  
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/7cd5bfd0-c78d-4c9f-b664-2935f703737c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7cd5bfd0-c78d-4c9f-b664-2935f703737c%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:13am UTC](https://discuss.elastic.co/t/exclude-one-filter-per-facet/8761/6 "2017-07-06T01:13:18Z")

</div>


