# Facet query with custom counts in facet navigation

**URL:** <https://discuss.elastic.co/t/facet-query-with-custom-counts-in-facet-navigation/9914>\
**Category:** Elasticsearch\
**Created:** [December 1, 2012, 10:41pm UTC](https://discuss.elastic.co/t/facet-query-with-custom-counts-in-facet-navigation/9914 "2012-12-01T22:41:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jaromir\_Muller](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaromir_muller/32/2600_2.png) [@Jaromir\_Muller](https://discuss.elastic.co/u/Jaromir_Muller)\
**Post date:** [December 1, 2012, 10:41pm UTC](https://discuss.elastic.co/t/facet-query-with-custom-counts-in-facet-navigation/9914/1 "2012-12-01T22:41:06Z")

</div>

Hi everyone,

I need to use facet navigation for filtering search result. By adding each  
term I can build query filter like\* (Attribute1=term1 OR Attribute1=term2)  
AND (Attribute2=term4)\*. So when I have such query, then in facet  
navigation I'd like to have all available terms with counts showing  
increase with new possible increase in final result set, in short user  
would see how many new documents will be added to search result set.

_Example:_

- Applied filter _(Attribute1=term1 OR Attribute1=term2) AND  
(Attribute2=term4)_

_Facet navigation_

- Attribute 1
- term1 (applied) - count here is 0, because I cannot add more documents  
to final result
  - term2 (applied) - count here is 0, because I cannot add more  
documents to final result
  - term3 - count would be the number of matching documents with filter  
_Attribute2 = term4 AND Attribute=term3_

- Attribute 2
  - term4 (applied) - count here is 0, because I cannot add more  
documents to final result
  - term5 - count would be the number of matching documents with filter  
(_Attribute1=term1 OR Attribute1=term2) AND Attribute2=term5_

- 
- 

Can anyone advice how would search request to ES look like?

Thanks, Jaro.

--

---

<div class="post-metadata">

**Author:** ![Jaromir\_Muller](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaromir_muller/32/2600_2.png) [@Jaromir\_Muller](https://discuss.elastic.co/u/Jaromir_Muller)\
**Post date:** [December 2, 2012, 10:23am UTC](https://discuss.elastic.co/t/facet-query-with-custom-counts-in-facet-navigation/9914/2 "2012-12-02T10:23:30Z")

</div>

Here an example how to replicate it and what I'd like to receive.

Thanks a lot. Jaro

On Sunday, December 2, 2012 12:41:06 AM UTC+2, Jaromír Müller wrote:

> Hi everyone,
> 
> I need to use facet navigation for filtering search result. By adding each  
> term I can build query filter like\* (Attribute1=term1 OR  
> Attribute1=term2) AND (Attribute2=term4)\*. So when I have such query,  
> then in facet navigation I'd like to have all available terms with counts  
> showing increase with new possible increase in final result set, in short  
> user would see how many new documents will be added to search result set.
> 
> _Example:_
> 
> - Applied filter _(Attribute1=term1 OR Attribute1=term2) AND  
> (Attribute2=term4)_
> 
> _Facet navigation_
> 
> - Attribute 1
> - term1 (applied) - count here is 0, because I cannot add more  
> documents to final result
> - term2 (applied) - count here is 0, because I cannot add more  
> documents to final result
> - term3 - count would be the number of matching documents with  
> filter _Attribute2 = term4 AND Attribute=term3_
> 
> - Attribute 2
> - term4 (applied) - count here is 0, because I cannot add more  
> documents to final result
> - term5 - count would be the number of matching documents with  
> filter (_Attribute1=term1 OR Attribute1=term2) AND Attribute2=term5_
> 
> - 
> - 
> 
> Can anyone advice how would search request to ES look like?
> 
> Thanks, Jaro.

--

---

<div class="post-metadata">

**Author:** ![Jaromir\_Muller](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaromir_muller/32/2600_2.png) [@Jaromir\_Muller](https://discuss.elastic.co/u/Jaromir_Muller)\
**Post date:** [December 2, 2012, 7:53pm UTC](https://discuss.elastic.co/t/facet-query-with-custom-counts-in-facet-navigation/9914/3 "2012-12-02T19:53:06Z")

</div>

In the end I found the solution which is based on facet\_filter

curl -XGET '[http://localhost:9200/test\_index/test\_type/\_search?pretty=true](http://localhost:9200/test_index/test_type/_search?pretty=true)' -d  
'{

```
"query":{
    "filtered":{
        "query" : {
            "query_string" : {
                "query" : "Lorem"
            }
        },
        "filter": {
            "and":[
                {
                    "or":[{
                        "term":{"size":"L"}
                    }]
                },{
                    "or":[{
                        "term":{"yesno":"yes"}
                    }]
                }
            ]
        }
    }
},
"facets":{
    "_facet_size":{
        "terms":{ 
            "field":"size",
            "order":"reverse_count",
            "all_terms":true             
        },                 
        "facet_filter": {
            "and":[
                {
                    "query" : {
                    "query_string" : {
                        "query" : "Lorem"
                    }
                }
                },
                {
                    "or":[{
                        "term": {"yesno": "yes"}
                    }]
                }
            ]
        },
        "global": true
    },
    "_facet_yesno":{
        "terms":{ 
            "field":"yesno",
            "order":"reverse_count",
            "all_terms":true             
        },                 
        "facet_filter": {
            "and":[
                {
                    "query" : {
                    "query_string" : {
                        "query" : "Lorem"
                    }
                }
                },
                {
                    "or":[{
                        "term": {"size": "L"}
                    }]
                }
            ]
        },
        "global": true
    }
}

```

}'

From the documentation page  
([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)) wasnt  
quite obvious what query I need to sent to ES.

Could anyone advice if my solution is correct or is there any possible  
improvement?

Thanks, Jaro.

On Sunday, December 2, 2012 12:41:06 AM UTC+2, Jaromír Müller wrote:

> Hi everyone,
> 
> I need to use facet navigation for filtering search result. By adding each  
> term I can build query filter like\* (Attribute1=term1 OR  
> Attribute1=term2) AND (Attribute2=term4)\*. So when I have such query,  
> then in facet navigation I'd like to have all available terms with counts  
> showing increase with new possible increase in final result set, in short  
> user would see how many new documents will be added to search result set.
> 
> _Example:_
> 
> - Applied filter _(Attribute1=term1 OR Attribute1=term2) AND  
> (Attribute2=term4)_
> 
> _Facet navigation_
> 
> - Attribute 1
> - term1 (applied) - count here is 0, because I cannot add more  
> documents to final result
> - term2 (applied) - count here is 0, because I cannot add more  
> documents to final result
> - term3 - count would be the number of matching documents with  
> filter _Attribute2 = term4 AND Attribute=term3_
> 
> - Attribute 2
> - term4 (applied) - count here is 0, because I cannot add more  
> documents to final result
> - term5 - count would be the number of matching documents with  
> filter (_Attribute1=term1 OR Attribute1=term2) AND Attribute2=term5_
> 
> - 
> - 
> 
> Can anyone advice how would search request to ES look like?
> 
> Thanks, Jaro.

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [December 3, 2012, 5:35pm UTC](https://discuss.elastic.co/t/facet-query-with-custom-counts-in-facet-navigation/9914/4 "2012-12-03T17:35:49Z")

</div>

I cannot think of any other approach besides using filter facets. Facets  
are typically used to narrow searches instead of expanding them. In your  
case you are trying to expand your search and that means you have to run  
all these extra searches for each "facet". You could also use multisearch  
to do the same thing, but your solution will probably use caches better.

On Sunday, December 2, 2012 2:53:06 PM UTC-5, Jaromír Müller wrote:

> In the end I found the solution which is based on facet\_filter
> 
> curl -XGET '  
> [http://localhost:9200/test\_index/test\_type/\_search?pretty=true](http://localhost:9200/test_index/test_type/_search?pretty=true)' -d '{
> 
> ```
> "query":{
> "filtered":{
> "query" : {
> "query_string" : {
> "query" : "Lorem"
> }
> },
> "filter": {
> "and":[
> {
> "or":[{
> "term":{"size":"L"}
> }]
> },{
> "or":[{
> "term":{"yesno":"yes"}
> }]
> }
> ]
> }
> }
> },
> "facets":{
> "_facet_size":{
> "terms":{ 
> "field":"size",
> "order":"reverse_count",
> "all_terms":true             
> },                 
> "facet_filter": {
> "and":[
> {
> "query" : {
> "query_string" : {
> "query" : "Lorem"
> }
> }
> },
> {
> "or":[{
> "term": {"yesno": "yes"}
> }]
> }
> ]
> },
> "global": true
> },
> "_facet_yesno":{
> "terms":{ 
> "field":"yesno",
> "order":"reverse_count",
> "all_terms":true             
> },                 
> "facet_filter": {
> "and":[
> {
> "query" : {
> "query_string" : {
> "query" : "Lorem"
> }
> }
> },
> {
> "or":[{
> "term": {"size": "L"}
> }]
> }
> ]
> },
> "global": true
> }
> }
> 
> ```
> 
> }'
> 
> From the documentation page (  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)) wasnt  
> quite obvious what query I need to sent to ES.
> 
> Could anyone advice if my solution is correct or is there any possible  
> improvement?
> 
> Thanks, Jaro.
> 
> On Sunday, December 2, 2012 12:41:06 AM UTC+2, Jaromír Müller wrote:
> 
> > Hi everyone,
> > 
> > I need to use facet navigation for filtering search result. By adding  
> > each term I can build query filter like\* (Attribute1=term1 OR  
> > Attribute1=term2) AND (Attribute2=term4)\*. So when I have such query,  
> > then in facet navigation I'd like to have all available terms with counts  
> > showing increase with new possible increase in final result set, in short  
> > user would see how many new documents will be added to search result set.
> > 
> > _Example:_
> > 
> > - Applied filter _(Attribute1=term1 OR Attribute1=term2) AND  
> > (Attribute2=term4)_
> > 
> > _Facet navigation_
> > 
> > - Attribute 1
> > - term1 (applied) - count here is 0, because I cannot add more  
> > documents to final result
> > - term2 (applied) - count here is 0, because I cannot add more  
> > documents to final result
> > - term3 - count would be the number of matching documents with  
> > filter _Attribute2 = term4 AND Attribute=term3_
> > 
> > - Attribute 2
> > - term4 (applied) - count here is 0, because I cannot add more  
> > documents to final result
> > - term5 - count would be the number of matching documents with  
> > filter (\*Attribute1=term1 OR Attribute1=term2) AND Attribute2=term5
> > 
> > - 
> > 
> > - 
> > - 
> > 
> > Can anyone advice how would search request to ES look like?
> > 
> > Thanks, Jaro.

--

---

<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:01am UTC](https://discuss.elastic.co/t/facet-query-with-custom-counts-in-facet-navigation/9914/5 "2017-07-06T03:01:47Z")

</div>


