# Inconsistent facet count

**URL:** <https://discuss.elastic.co/t/inconsistent-facet-count/4463>\
**Category:** Elasticsearch\
**Created:** [May 23, 2011, 10:53pm UTC](https://discuss.elastic.co/t/inconsistent-facet-count/4463 "2011-05-23T22:53:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stratospark](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@stratospark](https://discuss.elastic.co/u/stratospark)\
**Post date:** [May 23, 2011, 10:53pm UTC](https://discuss.elastic.co/t/inconsistent-facet-count/4463/1 "2011-05-23T22:53:19Z")

</div>

Hi,

I'm trying to get a drill down searching interface going, with updated  
facet counts as you drill down. I'm currently mapping all my strings  
as multi\_field, with the non-analyzed string value going into  
"attribute.orig" and the analyzed one going into plain "attribute"

Here are two sets of requests/responses. The first one I want to get  
the top location\_name count facet, which returns 17. When I narrow  
down the query to only search for location\_name.orig "49296", the  
facet count increases to 24.

I've tried applying a location\_name.orig filter to the match\_all  
query, but that results in the same thing.

Am I doing something wrong?

Thanks for the help!  
-pat

> curl -XPOST [http://127.0.0.1:9200/\_search](http://127.0.0.1:9200/_search) -d '  
> {  
> "query": {  
> "match\_all": {}  
> },  
> "facets": {  
> "location.location\_name": {  
> "terms": {  
> "field": "location.location\_name.orig",  
> "size": 1  
> }  
> }  
> },  
> "size": 0  
> }'

{  
"took":12,  
"timed\_out":false,  
"\_shards":{  
"total":20,  
"successful":20,  
"failed":0  
},  
"hits":{  
"total":41526,  
"max\_score":1.0,  
"hits":[

```
  ]

```

},  
"facets":{  
"location.location\_name":{  
"\_type":"terms",  
"missing":8,  
"terms":[  
{  
"term":"49296",  
"count":17  
}  
]  
}  
}  
}

> curl -XPOST [http://127.0.0.1:9200/\_search](http://127.0.0.1:9200/_search) -d '  
> {  
> "query": {  
> "term": {  
> "location.location\_name.orig": "49296"  
> }  
> },  
> "facets": {  
> "location.location\_name": {  
> "terms": {  
> "field": "location.location\_name.orig",  
> "size": 1  
> }  
> }  
> },  
> "size": 0  
> }'

{  
"took":12,  
"timed\_out":false,  
"\_shards":{  
"total":20,  
"successful":20,  
"failed":0  
},  
"hits":{  
"total":24,  
"max\_score":8.651358,  
"hits":[

```
  ]

```

},  
"facets":{  
"location.location\_name":{  
"\_type":"terms",  
"missing":0,  
"terms":[  
{  
"term":"49296",  
"count":24  
}  
]  
}  
}  
}

---

<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:** [May 23, 2011, 10:55pm UTC](https://discuss.elastic.co/t/inconsistent-facet-count/4463/2 "2011-05-23T22:55:43Z")

</div>

Can you _gist_ a recreation where you also index sample docs?  
On Tuesday, May 24, 2011 at 1:53 AM, stratospark wrote:

> Hi,
> 
> I'm trying to get a drill down searching interface going, with updated  
> facet counts as you drill down. I'm currently mapping all my strings  
> as multi\_field, with the non-analyzed string value going into  
> "attribute.orig" and the analyzed one going into plain "attribute"
> 
> Here are two sets of requests/responses. The first one I want to get  
> the top location\_name count facet, which returns 17. When I narrow  
> down the query to only search for location\_name.orig "49296", the  
> facet count increases to 24.
> 
> I've tried applying a location\_name.orig filter to the match\_all  
> query, but that results in the same thing.
> 
> Am I doing something wrong?
> 
> Thanks for the help!  
> -pat
> 
> > curl -XPOST [http://127.0.0.1:9200/\_search](http://127.0.0.1:9200/_search) -d '  
> > {  
> > "query": {  
> > "match\_all": {}  
> > },  
> > "facets": {  
> > "location.location\_name": {  
> > "terms": {  
> > "field": "location.location\_name.orig",  
> > "size": 1  
> > }  
> > }  
> > },  
> > "size": 0  
> > }'
> 
> {  
> "took":12,  
> "timed\_out":false,  
> "\_shards":{  
> "total":20,  
> "successful":20,  
> "failed":0  
> },  
> "hits":{  
> "total":41526,  
> "max\_score":1.0,  
> "hits":[
> 
> ]  
> },  
> "facets":{  
> "location.location\_name":{  
> "\_type":"terms",  
> "missing":8,  
> "terms":[  
> {  
> "term":"49296",  
> "count":17  
> }  
> ]  
> }  
> }  
> }
> 
> > curl -XPOST [http://127.0.0.1:9200/\_search](http://127.0.0.1:9200/_search) -d '  
> > {  
> > "query": {  
> > "term": {  
> > "location.location\_name.orig": "49296"  
> > }  
> > },  
> > "facets": {  
> > "location.location\_name": {  
> > "terms": {  
> > "field": "location.location\_name.orig",  
> > "size": 1  
> > }  
> > }  
> > },  
> > "size": 0  
> > }'
> 
> {  
> "took":12,  
> "timed\_out":false,  
> "\_shards":{  
> "total":20,  
> "successful":20,  
> "failed":0  
> },  
> "hits":{  
> "total":24,  
> "max\_score":8.651358,  
> "hits":[
> 
> ]  
> },  
> "facets":{  
> "location.location\_name":{  
> "\_type":"terms",  
> "missing":0,  
> "terms":[  
> {  
> "term":"49296",  
> "count":24  
> }  
> ]  
> }  
> }  
> }

---

<div class="post-metadata">

**Author:** ![stratospark](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@stratospark](https://discuss.elastic.co/u/stratospark)\
**Post date:** [May 24, 2011, 1:52am UTC](https://discuss.elastic.co/t/inconsistent-facet-count/4463/3 "2011-05-24T01:52:27Z")

</div>

Create a simplified sample and the results are even stranger.

I also noticed it wasn't picking up my "include\_in\_all" option in the  
dynamic template, which I think I need to generate facets on  
multifields?

> <https://gist.github.com/stratospark/987814>

Thanks so much for the help!

-pat

On May 23, 3:55 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Can you _gist_ a recreation where you also index sample docs?
> 
> On Tuesday, May 24, 2011 at 1:53 AM, stratospark wrote:
> 
> > Hi,
> 
> > I'm trying to get a drill down searching interface going, with updated  
> > facet counts as you drill down. I'm currently mapping all my strings  
> > as multi\_field, with the non-analyzed string value going into  
> > "attribute.orig" and the analyzed one going into plain "attribute"
> 
> > Here are two sets of requests/responses. The first one I want to get  
> > the top location\_name count facet, which returns 17. When I narrow  
> > down the query to only search for location\_name.orig "49296", the  
> > facet count increases to 24.
> 
> > I've tried applying a location\_name.orig filter to the match\_all  
> > query, but that results in the same thing.
> 
> > Am I doing something wrong?
> 
> > Thanks for the help!  
> > -pat
> 
> > > curl -XPOSThttp://127.0.0.1:9200/\_search-d '  
> > > {  
> > > "query": {  
> > > "match\_all": {}  
> > > },  
> > > "facets": {  
> > > "location.location\_name": {  
> > > "terms": {  
> > > "field": "location.location\_name.orig",  
> > > "size": 1  
> > > }  
> > > }  
> > > },  
> > > "size": 0  
> > > }'
> 
> > {  
> > "took":12,  
> > "timed\_out":false,  
> > "\_shards":{  
> > "total":20,  
> > "successful":20,  
> > "failed":0  
> > },  
> > "hits":{  
> > "total":41526,  
> > "max\_score":1.0,  
> > "hits":[
> 
> > ]  
> > },  
> > "facets":{  
> > "location.location\_name":{  
> > "\_type":"terms",  
> > "missing":8,  
> > "terms":[  
> > {  
> > "term":"49296",  
> > "count":17  
> > }  
> > ]  
> > }  
> > }  
> > }
> 
> > > curl -XPOSThttp://127.0.0.1:9200/\_search-d '  
> > > {  
> > > "query": {  
> > > "term": {  
> > > "location.location\_name.orig": "49296"  
> > > }  
> > > },  
> > > "facets": {  
> > > "location.location\_name": {  
> > > "terms": {  
> > > "field": "location.location\_name.orig",  
> > > "size": 1  
> > > }  
> > > }  
> > > },  
> > > "size": 0  
> > > }'
> 
> > {  
> > "took":12,  
> > "timed\_out":false,  
> > "\_shards":{  
> > "total":20,  
> > "successful":20,  
> > "failed":0  
> > },  
> > "hits":{  
> > "total":24,  
> > "max\_score":8.651358,  
> > "hits":[
> 
> > ]  
> > },  
> > "facets":{  
> > "location.location\_name":{  
> > "\_type":"terms",  
> > "missing":0,  
> > "terms":[  
> > {  
> > "term":"49296",  
> > "count":24  
> > }  
> > ]  
> > }  
> > }  
> > }

---

<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:** [May 24, 2011, 2:34pm UTC](https://discuss.elastic.co/t/inconsistent-facet-count/4463/4 "2011-05-24T14:34:12Z")

</div>

This happens because of the way the distributed facet calculation works. It gets the top 5 from each shard, and then aggregates it. Because you have an even distribution of terms, it will not return exact matches. if you increase the size, you will get better results. One possible value can be the size times the number of shards, for example: 25.  
On Tuesday, May 24, 2011 at 4:52 AM, stratospark wrote:

> Create a simplified sample and the results are even stranger.
> 
> I also noticed it wasn't picking up my "include\_in\_all" option in the  
> dynamic template, which I think I need to generate facets on  
> multifields?
> 
> [facet counts · GitHub](https://gist.github.com/987814)
> 
> Thanks so much for the help!
> 
> -pat
> 
> On May 23, 3:55 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > Can you _gist_ a recreation where you also index sample docs?
> > 
> > On Tuesday, May 24, 2011 at 1:53 AM, stratospark wrote:
> > 
> > > Hi,
> > 
> > > I'm trying to get a drill down searching interface going, with updated  
> > > facet counts as you drill down. I'm currently mapping all my strings  
> > > as multi\_field, with the non-analyzed string value going into  
> > > "attribute.orig" and the analyzed one going into plain "attribute"
> > 
> > > Here are two sets of requests/responses. The first one I want to get  
> > > the top location\_name count facet, which returns 17. When I narrow  
> > > down the query to only search for location\_name.orig "49296", the  
> > > facet count increases to 24.
> > 
> > > I've tried applying a location\_name.orig filter to the match\_all  
> > > query, but that results in the same thing.
> > 
> > > Am I doing something wrong?
> > 
> > > Thanks for the help!  
> > > -pat
> > 
> > > > curl -XPOSThttp://127.0.0.1:9200/\_search-d '  
> > > > {  
> > > > "query": {  
> > > > "match\_all": {}  
> > > > },  
> > > > "facets": {  
> > > > "location.location\_name": {  
> > > > "terms": {  
> > > > "field": "location.location\_name.orig",  
> > > > "size": 1  
> > > > }  
> > > > }  
> > > > },  
> > > > "size": 0  
> > > > }'
> > 
> > > {  
> > > "took":12,  
> > > "timed\_out":false,  
> > > "\_shards":{  
> > > "total":20,  
> > > "successful":20,  
> > > "failed":0  
> > > },  
> > > "hits":{  
> > > "total":41526,  
> > > "max\_score":1.0,  
> > > "hits":[
> > 
> > > ]  
> > > },  
> > > "facets":{  
> > > "location.location\_name":{  
> > > "\_type":"terms",  
> > > "missing":8,  
> > > "terms":[  
> > > {  
> > > "term":"49296",  
> > > "count":17  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > }
> > 
> > > > curl -XPOSThttp://127.0.0.1:9200/\_search-d '  
> > > > {  
> > > > "query": {  
> > > > "term": {  
> > > > "location.location\_name.orig": "49296"  
> > > > }  
> > > > },  
> > > > "facets": {  
> > > > "location.location\_name": {  
> > > > "terms": {  
> > > > "field": "location.location\_name.orig",  
> > > > "size": 1  
> > > > }  
> > > > }  
> > > > },  
> > > > "size": 0  
> > > > }'
> > 
> > > {  
> > > "took":12,  
> > > "timed\_out":false,  
> > > "\_shards":{  
> > > "total":20,  
> > > "successful":20,  
> > > "failed":0  
> > > },  
> > > "hits":{  
> > > "total":24,  
> > > "max\_score":8.651358,  
> > > "hits":[
> > 
> > > ]  
> > > },  
> > > "facets":{  
> > > "location.location\_name":{  
> > > "\_type":"terms",  
> > > "missing":0,  
> > > "terms":[  
> > > {  
> > > "term":"49296",  
> > > "count":24  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > }

---

<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:05am UTC](https://discuss.elastic.co/t/inconsistent-facet-count/4463/5 "2017-07-06T04:05:28Z")

</div>


