# Need a little help with facet query

**URL:** <https://discuss.elastic.co/t/need-a-little-help-with-facet-query/5798>\
**Category:** Elasticsearch\
**Created:** [November 7, 2011, 4:00pm UTC](https://discuss.elastic.co/t/need-a-little-help-with-facet-query/5798 "2011-11-07T16:00:38Z")\
**Posts on this page:** 4\
**Page:** 1

<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:** [November 7, 2011, 4:00pm UTC](https://discuss.elastic.co/t/need-a-little-help-with-facet-query/5798/1 "2011-11-07T16:00:38Z")

</div>

Hello,

I cannot find solution in documentation and I'd never used lucene-based  
search engines before.

My current query looks like this:  
{  
"sort": [{"add\_time": "desc"}],  
"query": {  
"filtered": {  
"filter": {  
"bool": {  
"must": [{"term": {"category\_ids": 10}}, {"range":  
{"end\_time": {"include\_upper": false, "from": "2011-11-07T16:45:42",  
"include\_lower": true}}}]  
}  
},  
"query": {"match\_all": {}}  
}  
},  
"facets": {"category\_ids": {"terms": {"field": "category\_ids",  
"size": 5}}},  
"from": 0,  
"size": 10  
}

category\_ids is a list/array of id: [1,2,3,4..]  
currently, facet contains counters for all category\_ids, but i need  
facets only for specific category\_id, for example 1 and 4.  
How can I solve that?

tk

---

<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:** [November 7, 2011, 6:50pm UTC](https://discuss.elastic.co/t/need-a-little-help-with-facet-query/5798/2 "2011-11-07T18:50:52Z")

</div>

Hi Tomasz

> "facets": {"category\_ids": {"terms": {"field": "category\_ids",  
> "size": 5}}},

> category\_ids is a list/array of id: [1,2,3,4..]  
> currently, facet contains counters for all category\_ids, but i need  
> facets only for specific category\_id, for example 1 and 4.  
> How can I solve that?

You can use regex patterns or exclude to filter the faceted terms:

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

clint

> tk

---

<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:** [November 7, 2011, 8:22pm UTC](https://discuss.elastic.co/t/need-a-little-help-with-facet-query/5798/3 "2011-11-07T20:22:17Z")

</div>

Thanks.

I have some more questions:

1. Exclude looks nice, but i rather need to include ~10 terms than  
excluding houndred. can someone give me example how to filter this facet  
only for [1,4] using regex or script?
2. Any idea how to implement this using PYES ?
3. Is 'term facet' the best choice (performance/memory usage) for what  
i try to do ? I need number of (filtered) ads in specific categories

On 07.11.2011 19:50, Clinton Gormley wrote:

> Hi Tomasz
> 
> > ```
> > "facets": {"category_ids": {"terms": {"field": "category_ids",
> > 
> > ```
> > 
> > "size": 5}}},
> 
> > category\_ids is a list/array of id: [1,2,3,4..]  
> > currently, facet contains counters for all category\_ids, but i need  
> > facets only for specific category\_id, for example 1 and 4.  
> > How can I solve that?
> 
> You can use regex patterns or exclude to filter the faceted terms:
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/terms-facet.html)
> 
> clint
> 
> > tk

---

<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:49am UTC](https://discuss.elastic.co/t/need-a-little-help-with-facet-query/5798/4 "2017-07-06T03:49:45Z")

</div>


