# Multiple select with facets

**URL:** https://discuss.elastic.co/t/multiple-select-with-facets/3757
**Category:** Elasticsearch
**Created:** [January 9, 2011, 1:25pm UTC](https://discuss.elastic.co/t/multiple-select-with-facets/3757 "2011-01-09T13:25:49Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Erik\_J](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erik_j/32/3285_2.png) [@Erik\_J](https://discuss.elastic.co/u/Erik_J)
#### Post date: [January 9, 2011, 1:25pm UTC](https://discuss.elastic.co/t/multiple-select-with-facets/3757/1 "2011-01-09T13:25:49Z")

</div>

I have a small document that looks like this  
{country: int, state: int, city: int, studio: bool}

I build a facet for listing cities using the following query:  
{"query":  
{"bool":{"must":[  
{"term":{"country":23424954}},  
{"term":{"state":20070561}}]  
}},"size":0,  
"facets":{"role":{"terms":{"size":3,"field":"role"}},"studio":{"terms":  
{"size":2,"field":"studio"}},"location\_lights":{"terms":{"size":  
2,"field":"on\_location\_lights"}},"country":{"terms":{"size":  
300,"field":"country"}},"state":{"terms":{"size":  
100,"field":"state"}},"city":{"terms":{"size":400,"field":"city"}}}}

I works as it should, it gives me the cities and the studio count. All  
is good. The problem is that I want my users to be able to select  
multiple cities and have the hits and studio count reflect the  
choices. This is the query I have right now:  
{"query":  
{"bool":{  
"must":[  
{"term":{"country":23424954}}  
,{"term":{"state":20070561}}],  
"should":[  
{"terms":{"city":[895018]}}]  
}},"size":0,"facets":{"role":{"terms":{"size":  
3,"field":"role"}},"studio":{"terms":{"size":  
2,"field":"studio"}},"location\_lights":{"terms":{"size":  
2,"field":"on\_location\_lights"}},"country":{"terms":{"size":  
300,"field":"country"}},"state":{"terms":{"size":  
100,"field":"state"}},"city":{"terms":{"size":400,"field":"city"}}}}

It gets me as far as still showing all the cities (making multiple  
choices an option), but the returning hits are wrong (size is 0 I  
know, but if I increase it). Also, the studio field doesn't reflect  
the choice of city/cities, but rather the state. Using "must" and  
adding city there removes the other cities from the result which  
remove multiple select as an option.  
Is it possible to generate two facets, one that reflects the choice of  
city and one that just lists the cities?

Right now the only thing I can think of is doing two queries, but that  
will quickly get out of hand.

I'm very new to ES, which is probably kind of obvious 🙂

---

<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: [January 11, 2011, 10:26pm UTC](https://discuss.elastic.co/t/multiple-select-with-facets/3757/2 "2011-01-11T22:26:25Z")

</div>

Two things:

1. Facets can be configured to run globally (not bounded by the search query), and then associate a filter with them.
2. Facets can have an extra filter on them to further reduce the docs they will be executed on.

Both allows you to control the relevant docs the facets will be executed on.  
On Sunday, January 9, 2011 at 3:25 PM, Erik J wrote:

> I have a small document that looks like this  
> {country: int, state: int, city: int, studio: bool}
> 
> I build a facet for listing cities using the following query:  
> {"query":  
> {"bool":{"must":[  
> {"term":{"country":23424954}},  
> {"term":{"state":20070561}}]  
> }},"size":0,  
> "facets":{"role":{"terms":{"size":3,"field":"role"}},"studio":{"terms":  
> {"size":2,"field":"studio"}},"location\_lights":{"terms":{"size":  
> 2,"field":"on\_location\_lights"}},"country":{"terms":{"size":  
> 300,"field":"country"}},"state":{"terms":{"size":  
> 100,"field":"state"}},"city":{"terms":{"size":400,"field":"city"}}}}
> 
> I works as it should, it gives me the cities and the studio count. All  
> is good. The problem is that I want my users to be able to select  
> multiple cities and have the hits and studio count reflect the  
> choices. This is the query I have right now:  
> {"query":  
> {"bool":{  
> "must":[  
> {"term":{"country":23424954}}  
> ,{"term":{"state":20070561}}],  
> "should":[  
> {"terms":{"city":[895018]}}]  
> }},"size":0,"facets":{"role":{"terms":{"size":  
> 3,"field":"role"}},"studio":{"terms":{"size":  
> 2,"field":"studio"}},"location\_lights":{"terms":{"size":  
> 2,"field":"on\_location\_lights"}},"country":{"terms":{"size":  
> 300,"field":"country"}},"state":{"terms":{"size":  
> 100,"field":"state"}},"city":{"terms":{"size":400,"field":"city"}}}}
> 
> It gets me as far as still showing all the cities (making multiple  
> choices an option), but the returning hits are wrong (size is 0 I  
> know, but if I increase it). Also, the studio field doesn't reflect  
> the choice of city/cities, but rather the state. Using "must" and  
> adding city there removes the other cities from the result which  
> remove multiple select as an option.  
> Is it possible to generate two facets, one that reflects the choice of  
> city and one that just lists the cities?
> 
> Right now the only thing I can think of is doing two queries, but that  
> will quickly get out of hand.
> 
> I'm very new to ES, which is probably kind of obvious 🙂

---

<div class="post-metadata">

### Author: ![Erik\_J](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erik_j/32/3285_2.png) [@Erik\_J](https://discuss.elastic.co/u/Erik_J)
#### Post date: [January 11, 2011, 11:54pm UTC](https://discuss.elastic.co/t/multiple-select-with-facets/3757/3 "2011-01-11T23:54:06Z")

</div>

Aha!

With a combination of a global scan and then by dynamically populating  
the facet filter for city depending on other choices I get a multiple  
choice facet with reflected results over the other properties.

Thanks for the pointers, everything is working as intended now 🙂

On Jan 11, 11:26 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Two things:
> 
> 1. Facets can be configured to run globally (not bounded by the search query), and then associate a filter with them.
> 2. Facets can have an extra filter on them to further reduce the docs they will be executed on.
> 
> Both allows you to control the relevant docs the facets will be executed on.
> 
> On Sunday, January 9, 2011 at 3:25 PM, Erik J wrote:
> 
> > I have a small document that looks like this  
> > {country: int, state: int, city: int, studio: bool}
> 
> > I build a facet for listing cities using the following query:  
> > {"query":  
> > {"bool":{"must":[  
> > {"term":{"country":23424954}},  
> > {"term":{"state":20070561}}]  
> > }},"size":0,  
> > "facets":{"role":{"terms":{"size":3,"field":"role"}},"studio":{"terms":  
> > {"size":2,"field":"studio"}},"location\_lights":{"terms":{"size":  
> > 2,"field":"on\_location\_lights"}},"country":{"terms":{"size":  
> > 300,"field":"country"}},"state":{"terms":{"size":  
> > 100,"field":"state"}},"city":{"terms":{"size":400,"field":"city"}}}}
> 
> > I works as it should, it gives me the cities and the studio count. All  
> > is good. The problem is that I want my users to be able to select  
> > multiple cities and have the hits and studio count reflect the  
> > choices. This is the query I have right now:  
> > {"query":  
> > {"bool":{  
> > "must":[  
> > {"term":{"country":23424954}}  
> > ,{"term":{"state":20070561}}],  
> > "should":[  
> > {"terms":{"city":[895018]}}]  
> > }},"size":0,"facets":{"role":{"terms":{"size":  
> > 3,"field":"role"}},"studio":{"terms":{"size":  
> > 2,"field":"studio"}},"location\_lights":{"terms":{"size":  
> > 2,"field":"on\_location\_lights"}},"country":{"terms":{"size":  
> > 300,"field":"country"}},"state":{"terms":{"size":  
> > 100,"field":"state"}},"city":{"terms":{"size":400,"field":"city"}}}}
> 
> > It gets me as far as still showing all the cities (making multiple  
> > choices an option), but the returning hits are wrong (size is 0 I  
> > know, but if I increase it). Also, the studio field doesn't reflect  
> > the choice of city/cities, but rather the state. Using "must" and  
> > adding city there removes the other cities from the result which  
> > remove multiple select as an option.  
> > Is it possible to generate two facets, one that reflects the choice of  
> > city and one that just lists the cities?
> 
> > Right now the only thing I can think of is doing two queries, but that  
> > will quickly get out of hand.
> 
> > I'm very new to ES, which is probably kind of obvious 🙂

---

<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: [January 24, 2011, 2:42pm UTC](https://discuss.elastic.co/t/multiple-select-with-facets/3757/4 "2011-01-24T14:42:38Z")

</div>

Shay, would the scope variable (e.g of RangeFacet) help me here, like  
I mentioned in this issue:

> <https://github.com/elastic/elasticsearch/issues/499>
>
> Current facets are single select, in that once a facet is applied you cannot see… the counts of the other values within the same term to be able to apply another value (which would be OR instead of AND against the existing filters).
> 
> Solr implements this by creating a group, or "tag" on the actions that means that they exclude each other from interfering with the counts. 
> 
> This allows facet selection for things like...
> 
> \> show me all "Red" shoes as I see there are 20 of those
> \> oh, and show me "Blue" shoes as well (which had 5 before and after "Red" being selected)
> \> great, now I have 25 shoes to look at!

Regards,  
Peter.

On 12 Jan., 00:54, Erik J [erikjohansson....@gmail.com](mailto:erikjohansson....@gmail.com) wrote:

> Aha!
> 
> With a combination of a global scan and then by dynamically populating  
> the facet filter for city depending on other choices I get a multiple  
> choice facet with reflected results over the other properties.
> 
> Thanks for the pointers, everything is working as intended now 🙂
> 
> On Jan 11, 11:26 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > Two things:
> 
> > 1. Facets can be configured to run globally (not bounded by the search query), and then associate a filter with them.
> > 2. Facets can have an extra filter on them to further reduce the docs they will be executed on.
> 
> > Both allows you to control the relevant docs the facets will be executed on.
> 
> > On Sunday, January 9, 2011 at 3:25 PM, Erik J wrote:
> > 
> > > I have a small document that looks like this  
> > > {country: int, state: int, city: int, studio: bool}
> 
> > > I build a facet for listing cities using the following query:  
> > > {"query":  
> > > {"bool":{"must":[  
> > > {"term":{"country":23424954}},  
> > > {"term":{"state":20070561}}]  
> > > }},"size":0,  
> > > "facets":{"role":{"terms":{"size":3,"field":"role"}},"studio":{"terms":  
> > > {"size":2,"field":"studio"}},"location\_lights":{"terms":{"size":  
> > > 2,"field":"on\_location\_lights"}},"country":{"terms":{"size":  
> > > 300,"field":"country"}},"state":{"terms":{"size":  
> > > 100,"field":"state"}},"city":{"terms":{"size":400,"field":"city"}}}}
> 
> > > I works as it should, it gives me the cities and the studio count. All  
> > > is good. The problem is that I want my users to be able to select  
> > > multiple cities and have the hits and studio count reflect the  
> > > choices. This is the query I have right now:  
> > > {"query":  
> > > {"bool":{  
> > > "must":[  
> > > {"term":{"country":23424954}}  
> > > ,{"term":{"state":20070561}}],  
> > > "should":[  
> > > {"terms":{"city":[895018]}}]  
> > > }},"size":0,"facets":{"role":{"terms":{"size":  
> > > 3,"field":"role"}},"studio":{"terms":{"size":  
> > > 2,"field":"studio"}},"location\_lights":{"terms":{"size":  
> > > 2,"field":"on\_location\_lights"}},"country":{"terms":{"size":  
> > > 300,"field":"country"}},"state":{"terms":{"size":  
> > > 100,"field":"state"}},"city":{"terms":{"size":400,"field":"city"}}}}
> 
> > > It gets me as far as still showing all the cities (making multiple  
> > > choices an option), but the returning hits are wrong (size is 0 I  
> > > know, but if I increase it). Also, the studio field doesn't reflect  
> > > the choice of city/cities, but rather the state. Using "must" and  
> > > adding city there removes the other cities from the result which  
> > > remove multiple select as an option.  
> > > Is it possible to generate two facets, one that reflects the choice of  
> > > city and one that just lists the cities?
> 
> > > Right now the only thing I can think of is doing two queries, but that  
> > > will quickly get out of hand.
> 
> > > I'm very new to ES, which is probably kind of obvious 🙂

---

<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: [January 26, 2011, 10:57am UTC](https://discuss.elastic.co/t/multiple-select-with-facets/3757/5 "2011-01-26T10:57:41Z")

</div>

The scope allows you to control if its executed globally or not. I just pushed a feature that might help with that: [Search: Allow to pass a search filter, applying only on the query (and not on facets for example) · Issue #650 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/issue/650). Ping if it does...

-shay.banon  
On Monday, January 24, 2011 at 4:42 PM, Karussell wrote:

> Shay, would the scope variable (e.g of RangeFacet) help me here, like  
> I mentioned in this issue:
> 
> [Multi-select facets · Issue #499 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/issue/499)
> 
> Regards,  
> Peter.
> 
> On 12 Jan., 00:54, Erik J [erikjohansson....@gmail.com](mailto:erikjohansson....@gmail.com) wrote:
> 
> > Aha!
> > 
> > With a combination of a global scan and then by dynamically populating  
> > the facet filter for city depending on other choices I get a multiple  
> > choice facet with reflected results over the other properties.
> > 
> > Thanks for the pointers, everything is working as intended now 🙂
> > 
> > On Jan 11, 11:26 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> > 
> > > Two things:
> > 
> > > 1. Facets can be configured to run globally (not bounded by the search query), and then associate a filter with them.
> > > 2. Facets can have an extra filter on them to further reduce the docs they will be executed on.
> > 
> > > Both allows you to control the relevant docs the facets will be executed on.
> > 
> > > On Sunday, January 9, 2011 at 3:25 PM, Erik J wrote:
> > > 
> > > > I have a small document that looks like this  
> > > > {country: int, state: int, city: int, studio: bool}
> > 
> > > > I build a facet for listing cities using the following query:  
> > > > {"query":  
> > > > {"bool":{"must":[  
> > > > {"term":{"country":23424954}},  
> > > > {"term":{"state":20070561}}]  
> > > > }},"size":0,  
> > > > "facets":{"role":{"terms":{"size":3,"field":"role"}},"studio":{"terms":  
> > > > {"size":2,"field":"studio"}},"location\_lights":{"terms":{"size":  
> > > > 2,"field":"on\_location\_lights"}},"country":{"terms":{"size":  
> > > > 300,"field":"country"}},"state":{"terms":{"size":  
> > > > 100,"field":"state"}},"city":{"terms":{"size":400,"field":"city"}}}}
> > 
> > > > I works as it should, it gives me the cities and the studio count. All  
> > > > is good. The problem is that I want my users to be able to select  
> > > > multiple cities and have the hits and studio count reflect the  
> > > > choices. This is the query I have right now:  
> > > > {"query":  
> > > > {"bool":{  
> > > > "must":[  
> > > > {"term":{"country":23424954}}  
> > > > ,{"term":{"state":20070561}}],  
> > > > "should":[  
> > > > {"terms":{"city":[895018]}}]  
> > > > }},"size":0,"facets":{"role":{"terms":{"size":  
> > > > 3,"field":"role"}},"studio":{"terms":{"size":  
> > > > 2,"field":"studio"}},"location\_lights":{"terms":{"size":  
> > > > 2,"field":"on\_location\_lights"}},"country":{"terms":{"size":  
> > > > 300,"field":"country"}},"state":{"terms":{"size":  
> > > > 100,"field":"state"}},"city":{"terms":{"size":400,"field":"city"}}}}
> > 
> > > > It gets me as far as still showing all the cities (making multiple  
> > > > choices an option), but the returning hits are wrong (size is 0 I  
> > > > know, but if I increase it). Also, the studio field doesn't reflect  
> > > > the choice of city/cities, but rather the state. Using "must" and  
> > > > adding city there removes the other cities from the result which  
> > > > remove multiple select as an option.  
> > > > Is it possible to generate two facets, one that reflects the choice of  
> > > > city and one that just lists the cities?
> > 
> > > > Right now the only thing I can think of is doing two queries, but that  
> > > > will quickly get out of hand.
> > 
> > > > I'm very new to ES, which is probably kind of obvious 🙂

---

<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, 4:13am UTC](https://discuss.elastic.co/t/multiple-select-with-facets/3757/6 "2017-07-06T04:13:14Z")

</div>


