# Facets : limit the terms values to a list?

**URL:** <https://discuss.elastic.co/t/facets-limit-the-terms-values-to-a-list/6208>\
**Category:** Elasticsearch\
**Created:** [December 21, 2011, 10:19am UTC](https://discuss.elastic.co/t/facets-limit-the-terms-values-to-a-list/6208 "2011-12-21T10:19:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastien\_Charrier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sebastien_charrier/32/2835_2.png) [@Sebastien\_Charrier](https://discuss.elastic.co/u/Sebastien_Charrier)\
**Post date:** [December 21, 2011, 10:19am UTC](https://discuss.elastic.co/t/facets-limit-the-terms-values-to-a-list/6208/1 "2011-12-21T10:19:05Z")

</div>

Hi,

I'm actually working on a faceted search, and I can't find a good  
solution to a problem (in the docs nor with my best friend Google ;)).

One of the facets I have to manage has to be hierarchical (it's a  
category filter, one record can have multiple categories of multiple  
levels). Example :

1. First level on search :  
[] Computers (10)  
[] Sea (5)
2. User checks "Computers". We have to give him all records associated  
to category "computers" (or to any subcategory), and to propose the  
sub-categories facetized :  
[x] Computers (10)  
[] Internet (6)  
[] Hardware (2)

My problem is to get only facet counts for categories under  
"Computers", as a record can be associated to "Internet" and to a  
subcategory of "Sea".

The only solution I can find for the moment, is to use the "regex"  
param with a list of ids corresponding to subcategories of  
"Computer" ( regex : "1|2|3"), but it's not really elegant.

Does somebody have a better idea/solution to filter the terms ?

Thanks !  
Sebastien

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [December 21, 2011, 10:29am UTC](https://discuss.elastic.co/t/facets-limit-the-terms-values-to-a-list/6208/2 "2011-12-21T10:29:41Z")

</div>

Hi Sebastien

> One of the facets I have to manage has to be hierarchical (it's a  
> category filter, one record can have multiple categories of multiple  
> levels). Example :
> 
> 1. First level on search :  
> [] Computers (10)  
> [] Sea (5)
> 2. User checks "Computers". We have to give him all records associated  
> to category "computers" (or to any subcategory), and to propose the  
> sub-categories facetized :  
> [x] Computers (10)  
> [] Internet (6)  
> [] Hardware (2)
> 
> My problem is to get only facet counts for categories under  
> "Computers", as a record can be associated to "Internet" and to a  
> subcategory of "Sea".
> 
> The only solution I can find for the moment, is to use the "regex"  
> param with a list of ids corresponding to subcategories of  
> "Computer" ( regex : "1|2|3"), but it's not really elegant.

I'd suggest storing your tags as:  
computers  
computers\_internet  
computers\_hardware  
sea  
sea\_internet  
sea\_surf

That way, your regex can just look like "^computers\_"

You could additionally store 'internet','hardware',and 'surf' tags to  
allow searching directly on those, if you need to

clint

---

<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:** [December 21, 2011, 12:40pm UTC](https://discuss.elastic.co/t/facets-limit-the-terms-values-to-a-list/6208/3 "2011-12-21T12:40:16Z")

</div>

I've managed to solve this in Solr and the procedure is described  
here:

> **[Use cases of faceted search for Apache Solr](https://karussell.wordpress.com/2010/12/08/use-cases-of-faceted-search-for-apache-solr/)**
>
> GraphHopper – A Java routing engine karussell ads In this post I write about some use cases of facets for Apache Solr. Please submit your own ideas in the comments. This post is splitted into…

and yes, sadly you'll have to filter away via regex (or facet.prefix  
in Solr)

Peter.

On 21 Dez., 11:19, Sébastien Charrier [scharr...@gmail.com](mailto:scharr...@gmail.com) wrote:

> Hi,
> 
> I'm actually working on a faceted search, and I can't find a good  
> solution to a problem (in the docs nor with my best friend Google ;)).
> 
> One of the facets I have to manage has to be hierarchical (it's a  
> category filter, one record can have multiple categories of multiple  
> levels). Example :
> 
> 1. First level on search :  
> [] Computers (10)  
> [] Sea (5)
> 2. User checks "Computers". We have to give him all records associated  
> to category "computers" (or to any subcategory), and to propose the  
> sub-categories facetized :  
> [x] Computers (10)  
> [] Internet (6)  
> [] Hardware (2)
> 
> My problem is to get only facet counts for categories under  
> "Computers", as a record can be associated to "Internet" and to a  
> subcategory of "Sea".
> 
> The only solution I can find for the moment, is to use the "regex"  
> param with a list of ids corresponding to subcategories of  
> "Computer" ( regex : "1|2|3"), but it's not really elegant.
> 
> Does somebody have a better idea/solution to filter the terms ?
> 
> Thanks !  
> Sebastien

---

<div class="post-metadata">

**Author:** ![Sebastien\_Charrier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sebastien_charrier/32/2835_2.png) [@Sebastien\_Charrier](https://discuss.elastic.co/u/Sebastien_Charrier)\
**Post date:** [December 21, 2011, 1:19pm UTC](https://discuss.elastic.co/t/facets-limit-the-terms-values-to-a-list/6208/4 "2011-12-21T13:19:24Z")

</div>

Thanks for your 2 replies !

I have a field wich contains all the ids path ("1 2 3"), wich i'll use  
for filter my terms. So ... let's go for regex !

Sebastien

On Dec 21, 1:40 pm, Karussell [tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com) wrote:

> I've managed to solve this in Solr and the procedure is described  
> here:
> 
> [Use cases of faceted search for Apache Solr | Karussell](http://karussell.wordpress.com/2010/12/08/use-cases-of-faceted-search)...
> 
> and yes, sadly you'll have to filter away via regex (or facet.prefix  
> in Solr)
> 
> Peter.
> 
> On 21 Dez., 11:19, Sébastien Charrier [scharr...@gmail.com](mailto:scharr...@gmail.com) wrote:
> 
> > Hi,
> 
> > I'm actually working on a faceted search, and I can't find a good  
> > solution to a problem (in the docs nor with my best friend Google ;)).
> 
> > One of the facets I have to manage has to be hierarchical (it's a  
> > category filter, one record can have multiple categories of multiple  
> > levels). Example :
> > 
> > 1. First level on search :  
> > [] Computers (10)  
> > [] Sea (5)
> > 2. User checks "Computers". We have to give him all records associated  
> > to category "computers" (or to any subcategory), and to propose the  
> > sub-categories facetized :  
> > [x] Computers (10)  
> > [] Internet (6)  
> > [] Hardware (2)
> 
> > My problem is to get only facet counts for categories under  
> > "Computers", as a record can be associated to "Internet" and to a  
> > subcategory of "Sea".
> 
> > The only solution I can find for the moment, is to use the "regex"  
> > param with a list of ids corresponding to subcategories of  
> > "Computer" ( regex : "1|2|3"), but it's not really elegant.
> 
> > Does somebody have a better idea/solution to filter the terms ?
> 
> > Thanks !  
> > Sebastien

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kloc](https://avatars.discourse-cdn.com/v4/letter/t/958977/32.png) [@Tomasz\_Kloc](https://discuss.elastic.co/u/Tomasz_Kloc)\
**Post date:** [January 2, 2012, 5:40pm UTC](https://discuss.elastic.co/t/facets-limit-the-terms-values-to-a-list/6208/5 "2012-01-02T17:40:40Z")

</div>

Hi,

I store two values in my ES index:  
'category\_id': {'type': 'integer'},  
'category\_ids': {'type': 'integer'},

where the second one is only for facets. It's not a path of ids  
(string), but list of integers. I'm not sure it's the best solution, but  
i don't need to use regex. my query looks like:

"query": {"match\_all": {}}}}, "facets": {"category\_ids": {"terms":  
{"field": "category\_ids", "size": 20}}}, "from": 0, "size": 10}

On 21.12.2011 14:19, SÃ©bastien Charrier wrote:

> Thanks for your 2 replies !
> 
> I have a field wich contains all the ids path ("1 2 3"), wich i'll use  
> for filter my terms. So ... let's go for regex !
> 
> Sebastien

---

<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:44am UTC](https://discuss.elastic.co/t/facets-limit-the-terms-values-to-a-list/6208/6 "2017-07-06T03:44:02Z")

</div>


