# ElasticSearch: restrict results but not facet counts

**URL:** https://discuss.elastic.co/t/elasticsearch-restrict-results-but-not-facet-counts/17472
**Category:** Elasticsearch
**Created:** [May 13, 2014, 7:32am UTC](https://discuss.elastic.co/t/elasticsearch-restrict-results-but-not-facet-counts/17472 "2014-05-13T07:32:56Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Felix\_Schwarz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felix_schwarz/32/1530_2.png) [@Felix\_Schwarz](https://discuss.elastic.co/u/Felix_Schwarz)
#### Post date: [May 13, 2014, 7:32am UTC](https://discuss.elastic.co/t/elasticsearch-restrict-results-but-not-facet-counts/17472/1 "2014-05-13T07:32:56Z")

</div>

I'm struggling writing an ElasticSearch query with facet counts. This is very  
likely a simple question but I'm confused by all the facet filters and filter  
facets.

The query itself needs two conditions:

- a condition which restricts the document set, excluded documents should  
not be used for the facet count ("only display documents for a specific  
user").
- a condition which restricts the result set but excluded documents should  
be part of the facet count ("user filters by tag but should see other tags  
in the facet count")

Example documents:  
{"user": "admin", "tag": "baz"}  
{"user": "editor", "tag": "foo"}  
{"user": "editor", "tag": "bar"}  
( curl -XPUT '[http://localhost:9200/blog/post/1](http://localhost:9200/blog/post/1)' -d '...' )

query:  
{  
"query" : {  
"constant\_score": {  
"filter": {"term": {"user": "editor"} }  
}  
},  
"facets" : {  
"tag" : { "terms" : {"field" : "tag"} }  
}  
}

The result of this query is ok, I see a facet count for tags foo+bar correctly.

Now I'd like to extend the query so the results only display documents with  
tag "foo" BUT keep the facet count as before (so tag "bar" should also appear  
in the facet count).

If I modify the constant\_score filter like this:  
"filter": {  
"and": [{"term": {"user": "editor"}}, {"term": {"tag": "foo"}}]  
}

Then obviously only tag "foo" will appear in the facet count so I need to add  
the {"term": {"tag": "foo"}} filter somewhere else.

How do I do that in ElasticSearch?

Felix

--  
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/5371CAA8.5070801%40oss.schwarz.eu](https://groups.google.com/d/msgid/elasticsearch/5371CAA8.5070801%40oss.schwarz.eu).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [May 13, 2014, 9:37am UTC](https://discuss.elastic.co/t/elasticsearch-restrict-results-but-not-facet-counts/17472/2 "2014-05-13T09:37:37Z")

</div>

Hey Felix,

You need to add your second filter as a "post\_filter":

[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-post-filter.html#search-request-post-filter](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-post-filter.html#search-request-post-filter)

HTH

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr

Le 13 mai 2014 à 10:43:15, Felix Schwarz (felix.schwarz@oss.schwarz.eu) a écrit:

I'm struggling writing an ElasticSearch query with facet counts. This is very  
likely a simple question but I'm confused by all the facet filters and filter  
facets.

The query itself needs two conditions:

- a condition which restricts the document set, excluded documents should  
not be used for the facet count ("only display documents for a specific  
user").
- a condition which restricts the result set but excluded documents should  
be part of the facet count ("user filters by tag but should see other tags  
in the facet count")

Example documents:  
{"user": "admin", "tag": "baz"}  
{"user": "editor", "tag": "foo"}  
{"user": "editor", "tag": "bar"}  
( curl -XPUT '[http://localhost:9200/blog/post/1](http://localhost:9200/blog/post/1)' -d '...' )

query:  
{  
"query" : {  
"constant\_score": {  
"filter": {"term": {"user": "editor"} }  
}  
},  
"facets" : {  
"tag" : { "terms" : {"field" : "tag"} }  
}  
}

The result of this query is ok, I see a facet count for tags foo+bar correctly.

Now I'd like to extend the query so the results only display documents with  
tag "foo" BUT keep the facet count as before (so tag "bar" should also appear  
in the facet count).

If I modify the constant\_score filter like this:  
"filter": {  
"and": [{"term": {"user": "editor"}}, {"term": {"tag": "foo"}}]  
}

Then obviously only tag "foo" will appear in the facet count so I need to add  
the {"term": {"tag": "foo"}} filter somewhere else.

How do I do that in ElasticSearch?

Felix

--  
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/5371CAA8.5070801%40oss.schwarz.eu](https://groups.google.com/d/msgid/elasticsearch/5371CAA8.5070801%40oss.schwarz.eu).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/etPan.5371e7e1.189a769b.a994%40MacBook-Air-de-David.local](https://groups.google.com/d/msgid/elasticsearch/etPan.5371e7e1.189a769b.a994%40MacBook-Air-de-David.local).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Felix\_Schwarz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felix_schwarz/32/1530_2.png) [@Felix\_Schwarz](https://discuss.elastic.co/u/Felix_Schwarz)
#### Post date: [May 14, 2014, 8:08am UTC](https://discuss.elastic.co/t/elasticsearch-restrict-results-but-not-facet-counts/17472/3 "2014-05-14T08:08:28Z")

</div>

Am 13.05.2014 11:37, schrieb David Pilato:

> You need to add your second filter as a "post\_filter":  
> (...)

Thank you very much, that did it. (I noticed that just "filter" would work as  
well in my case).

Felix

--  
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/5373247C.6000907%40oss.schwarz.eu](https://groups.google.com/d/msgid/elasticsearch/5373247C.6000907%40oss.schwarz.eu).  
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:29am UTC](https://discuss.elastic.co/t/elasticsearch-restrict-results-but-not-facet-counts/17472/4 "2017-07-06T01:29:31Z")

</div>


