# Facet count unexpected result

**URL:** https://discuss.elastic.co/t/facet-count-unexpected-result/5713
**Category:** Elasticsearch
**Created:** [October 28, 2011, 12:53am UTC](https://discuss.elastic.co/t/facet-count-unexpected-result/5713 "2011-10-28T00:53:33Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![John1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/john1/32/3092_2.png) [@John1](https://discuss.elastic.co/u/John1)
#### Post date: [October 28, 2011, 12:53am UTC](https://discuss.elastic.co/t/facet-count-unexpected-result/5713/1 "2011-10-28T00:53:33Z")

</div>

To illustrate the problem, I create 16 records. 14 are in Phoenix and  
2 are in Scottsdale:

curl -XPUT '[http://localhost:9200/subscribers/subscriber/1](http://localhost:9200/subscribers/subscriber/1)' -d  
' { "name": "John", "location": "Phoenix" }'  
curl -XPUT '[http://localhost:9200/subscribers/subscriber/2](http://localhost:9200/subscribers/subscriber/2)' -d  
' { "name": "Joe", "location": "Phoenix" }'  
curl -XPUT '[http://localhost:9200/subscribers/subscriber/3](http://localhost:9200/subscribers/subscriber/3)' -d  
' { "name": "Jack", "location": "Phoenix" }'  
curl -XPUT '[http://localhost:9200/subscribers/subscriber/4](http://localhost:9200/subscribers/subscriber/4)' -d  
' { "name": "Janet", "location": "Scottsdale" }'  
curl -XPUT '[http://localhost:9200/subscribers/subscriber/5](http://localhost:9200/subscribers/subscriber/5)' -d  
' { "name": "Jane", "location": "Scottsdale" }'  
curl -XPUT '[http://localhost:9200/subscribers/subscriber/6](http://localhost:9200/subscribers/subscriber/6)' -d  
' { "name": "Bill", "location": "Phoenix" }'  
curl -XPUT '[http://localhost:9200/subscribers/subscriber/7](http://localhost:9200/subscribers/subscriber/7)' -d  
' { "name": "Steve", "location": "Phoenix" }'  
curl -XPUT '[http://localhost:9200/subscribers/subscriber/8](http://localhost:9200/subscribers/subscriber/8)' -d  
' { "name": "Lucy", "location": "Phoenix" }'  
curl -XPUT '[http://localhost:9200/subscribers/subscriber/9](http://localhost:9200/subscribers/subscriber/9)' -d  
' { "name": "Zane", "location": "Phoenix" }'  
curl -XPUT '[http://localhost:9200/subscribers/subscriber/10](http://localhost:9200/subscribers/subscriber/10)' -d  
' { "name": "George", "location": "Phoenix" }'  
curl -XPUT '[http://localhost:9200/subscribers/subscriber/11](http://localhost:9200/subscribers/subscriber/11)' -d  
' { "name": "Adam", "location": "Phoenix" }'  
curl -XPUT '[http://localhost:9200/subscribers/subscriber/12](http://localhost:9200/subscribers/subscriber/12)' -d  
' { "name": "Eve", "location": "Phoenix" }'  
curl -XPUT '[http://localhost:9200/subscribers/subscriber/13](http://localhost:9200/subscribers/subscriber/13)' -d  
' { "name": "William", "location": "Phoenix" }'  
curl -XPUT '[http://localhost:9200/subscribers/subscriber/14](http://localhost:9200/subscribers/subscriber/14)' -d  
' { "name": "Alan", "location": "Phoenix" }'  
curl -XPUT '[http://localhost:9200/subscribers/subscriber/15](http://localhost:9200/subscribers/subscriber/15)' -d  
' { "name": "Luke", "location": "Phoenix" }'  
curl -XPUT '[http://localhost:9200/subscribers/subscriber/16](http://localhost:9200/subscribers/subscriber/16)' -d  
' { "name": "Bo", "location": "Phoenix" }'

I want to find the 'most popular' locations, I use:

curl -XGET '[http://localhost:9200/subscribers/subscriber/\_search](http://localhost:9200/subscribers/subscriber/_search)?  
pretty=true' -d '  
{ "size" : 0,  
"query" : {  
"match\_all" : { }  
},  
"facets" : {  
"locations" : {  
"terms" : {  
"field" : "location",  
"size" : 2  
}  
}  
}  
}'

This works as expected:

"facets" : {  
"locations" : {  
"\_type" : "terms",  
"missing" : 0,  
"total" : 16,  
"other" : 0,  
"terms" : [ {  
"term" : "phoenix",  
"count" : 14  
}, {  
"term" : "scottsdale",  
"count" : 2  
} ]  
}  
}

Now I want to find the single most popular location (I changed the  
size field from 2 to 1):

curl -XGET '[http://localhost:9200/subscribers/subscriber/\_search](http://localhost:9200/subscribers/subscriber/_search)?  
pretty=true' -d '  
{ "size" : 0,  
"query" : {  
"match\_all" : { }  
},  
"facets" : {  
"locations" : {  
"terms" : {  
"field" : "location",  
"size" : 1  
}  
}  
}  
}'

Result:

"facets" : {  
"locations" : {  
"\_type" : "terms",  
"missing" : 0,  
"total" : 16,  
"other" : 3,  
"terms" : [ {  
"term" : "phoenix",  
"count" : 13  
} ]  
}  
}

This is not what I expect. I still expect: other '2' and count '14'.  
Am I missing something?  
My version is 0.17.6

---

<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: [October 28, 2011, 7:36am UTC](https://discuss.elastic.co/t/facet-count-unexpected-result/5713/2 "2011-10-28T07:36:54Z")

</div>

Do you have more than one shard? Have a look into this discussion:

> <https://github.com/elastic/elasticsearch/issues/1305>
>
> I'm working with nested documents and have noticed that my faceted search interf…ace is giving the wrong counts when I have more than one shard. To be more specific, I'm working with RDF triples (entity \> attribute \> value) and I'm nesting the attributes (called predicates in my example):
> 
> \`\`\`
> {
> "\_id" : "512a2c022f0b4e3daa341e6c8bcf6c2f",
> "url": "http://dbpedia.org/resource/Alan\_Shepard",
> "predicates": \[
> {
> "type": "type",
> "string\_value": \["thing", "person", "astronaut"\]
> }, {
> "type": "label",
> "string\_value": \["Alan Shepard"\]
> }, {
> "type": "time in space",
> "float\_value": \[216.950\]
> },
> ... lots more
> \]
> }
> \`\`\`
> 
> I've created a shell script (https://gist.github.com/1196986) that recreates the problem with a fresh index. The created data set has these totals:
> \- thing (30)
> \- creative work (20)
> \- video game (10)
> \- tv show (10)
> \- people (10)
> 
> With only \*\*one shard\*\* the following query gives the correct counts no matter what the size parameter is set to:
> 
> \`\`\`
> {
> "size": 0,
> "query": {
> "match\_all": {}
> },
> "facets": {
> "type\_counts": {
> "terms": {
> "field": "string\_value",
> "size": 5
> },
> "nested": "predicates",
> "facet\_filter": {
> "term": {
> "type": "type"
> }
> }
> }
> }
> }
> \`\`\`
> 
> However, with \*\*more than one shard\*\* the size parameter affects the accuracy of the counts. If it is equal to or greater than the number of terms returned by the facet query (5 in this case) then it works fine. However, the terms at the bottom of the list start to display low counts as you reduce the size parameter:
> 
> With "size" : 4
> \- thing (30)
> \- creative work (20)
> \- video game (10)
> \- \*\*tv show (9)\*\*
> 
> With "size" : 3
> \- thing (30)
> \- \*\*creative work (15)\*\*
> \- \*\*video game (9)\*\*
> 
> With "size" : 2
> \- thing (30)
> \- \*\*creative work (15)\*\*
> 
> So it looks like the sub-totals from some of the shards aren't being included for some reason. BTW I'm on ubuntu and the problem seems to affect all versions of ES I've tried (17.0, 17.1 and 17.6). Any ideas...?
> 
> P.S. absolutely loving ES - it's made my life a lot easier :)

Peter

On 28 Okt., 02:53, John [john.bo...@gmail.com](mailto:john.bo...@gmail.com) wrote:

> To illustrate the problem, I create 16 records. 14 are in Phoenix and  
> 2 are in Scottsdale:
> 
> curl -XPUT '[http://localhost:9200/subscribers/subscriber/1'-d](http://localhost:9200/subscribers/subscriber/1'-d)  
> ' { "name": "John", "location": "Phoenix" }'  
> curl -XPUT '[http://localhost:9200/subscribers/subscriber/2'-d](http://localhost:9200/subscribers/subscriber/2'-d)  
> ' { "name": "Joe", "location": "Phoenix" }'  
> curl -XPUT '[http://localhost:9200/subscribers/subscriber/3'-d](http://localhost:9200/subscribers/subscriber/3'-d)  
> ' { "name": "Jack", "location": "Phoenix" }'  
> curl -XPUT '[http://localhost:9200/subscribers/subscriber/4'-d](http://localhost:9200/subscribers/subscriber/4'-d)  
> ' { "name": "Janet", "location": "Scottsdale" }'  
> curl -XPUT '[http://localhost:9200/subscribers/subscriber/5'-d](http://localhost:9200/subscribers/subscriber/5'-d)  
> ' { "name": "Jane", "location": "Scottsdale" }'  
> curl -XPUT '[http://localhost:9200/subscribers/subscriber/6'-d](http://localhost:9200/subscribers/subscriber/6'-d)  
> ' { "name": "Bill", "location": "Phoenix" }'  
> curl -XPUT '[http://localhost:9200/subscribers/subscriber/7'-d](http://localhost:9200/subscribers/subscriber/7'-d)  
> ' { "name": "Steve", "location": "Phoenix" }'  
> curl -XPUT '[http://localhost:9200/subscribers/subscriber/8'-d](http://localhost:9200/subscribers/subscriber/8'-d)  
> ' { "name": "Lucy", "location": "Phoenix" }'  
> curl -XPUT '[http://localhost:9200/subscribers/subscriber/9'-d](http://localhost:9200/subscribers/subscriber/9'-d)  
> ' { "name": "Zane", "location": "Phoenix" }'  
> curl -XPUT '[http://localhost:9200/subscribers/subscriber/10'-d](http://localhost:9200/subscribers/subscriber/10'-d)  
> ' { "name": "George", "location": "Phoenix" }'  
> curl -XPUT '[http://localhost:9200/subscribers/subscriber/11'-d](http://localhost:9200/subscribers/subscriber/11'-d)  
> ' { "name": "Adam", "location": "Phoenix" }'  
> curl -XPUT '[http://localhost:9200/subscribers/subscriber/12'-d](http://localhost:9200/subscribers/subscriber/12'-d)  
> ' { "name": "Eve", "location": "Phoenix" }'  
> curl -XPUT '[http://localhost:9200/subscribers/subscriber/13'-d](http://localhost:9200/subscribers/subscriber/13'-d)  
> ' { "name": "William", "location": "Phoenix" }'  
> curl -XPUT '[http://localhost:9200/subscribers/subscriber/14'-d](http://localhost:9200/subscribers/subscriber/14'-d)  
> ' { "name": "Alan", "location": "Phoenix" }'  
> curl -XPUT '[http://localhost:9200/subscribers/subscriber/15'-d](http://localhost:9200/subscribers/subscriber/15'-d)  
> ' { "name": "Luke", "location": "Phoenix" }'  
> curl -XPUT '[http://localhost:9200/subscribers/subscriber/16'-d](http://localhost:9200/subscribers/subscriber/16'-d)  
> ' { "name": "Bo", "location": "Phoenix" }'
> 
> I want to find the 'most popular' locations, I use:
> 
> curl -XGET '[http://localhost:9200/subscribers/subscriber/\_search](http://localhost:9200/subscribers/subscriber/_search)?  
> pretty=true' -d '  
> { "size" : 0,  
> "query" : {  
> "match\_all" : { }  
> },  
> "facets" : {  
> "locations" : {  
> "terms" : {  
> "field" : "location",  
> "size" : 2  
> }  
> }  
> }
> 
> }'
> 
> This works as expected:
> 
> "facets" : {  
> "locations" : {  
> "\_type" : "terms",  
> "missing" : 0,  
> "total" : 16,  
> "other" : 0,  
> "terms" : [ {  
> "term" : "phoenix",  
> "count" : 14  
> }, {  
> "term" : "scottsdale",  
> "count" : 2  
> } ]  
> }  
> }
> 
> Now I want to find the single most popular location (I changed the  
> size field from 2 to 1):
> 
> curl -XGET '[http://localhost:9200/subscribers/subscriber/\_search](http://localhost:9200/subscribers/subscriber/_search)?  
> pretty=true' -d '  
> { "size" : 0,  
> "query" : {  
> "match\_all" : { }  
> },  
> "facets" : {  
> "locations" : {  
> "terms" : {  
> "field" : "location",  
> "size" : 1  
> }  
> }  
> }
> 
> }'
> 
> Result:
> 
> "facets" : {  
> "locations" : {  
> "\_type" : "terms",  
> "missing" : 0,  
> "total" : 16,  
> "other" : 3,  
> "terms" : [ {  
> "term" : "phoenix",  
> "count" : 13  
> } ]  
> }  
> }
> 
> This is not what I expect. I still expect: other '2' and count '14'.  
> Am I missing something?  
> My version is 0.17.6

---

<div class="post-metadata">

### Author: ![John1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/john1/32/3092_2.png) [@John1](https://discuss.elastic.co/u/John1)
#### Post date: [October 30, 2011, 3:17pm UTC](https://discuss.elastic.co/t/facet-count-unexpected-result/5713/3 "2011-10-30T15:17:27Z")

</div>

Yes, this is exactly the same issue.  
Thanks for pointing me to that.

On Oct 28, 12:36 am, Karussell [tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com) wrote:

> Do you have more than one shard? Have a look into this discussion:
> 
> [terms facet gives wrong count with n\_shards \> 1 · Issue #1305 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/1305)
> 
> Peter
> 
> On 28 Okt., 02:53, John [john.bo...@gmail.com](mailto:john.bo...@gmail.com) wrote:
> 
> > To illustrate the problem, I create 16 records. 14 are in Phoenix and  
> > 2 are in Scottsdale:
> 
> > curl -XPUT '[http://localhost:9200/subscribers/subscriber/1'-d](http://localhost:9200/subscribers/subscriber/1'-d)  
> > ' { "name": "John", "location": "Phoenix" }'  
> > curl -XPUT '[http://localhost:9200/subscribers/subscriber/2'-d](http://localhost:9200/subscribers/subscriber/2'-d)  
> > ' { "name": "Joe", "location": "Phoenix" }'  
> > curl -XPUT '[http://localhost:9200/subscribers/subscriber/3'-d](http://localhost:9200/subscribers/subscriber/3'-d)  
> > ' { "name": "Jack", "location": "Phoenix" }'  
> > curl -XPUT '[http://localhost:9200/subscribers/subscriber/4'-d](http://localhost:9200/subscribers/subscriber/4'-d)  
> > ' { "name": "Janet", "location": "Scottsdale" }'  
> > curl -XPUT '[http://localhost:9200/subscribers/subscriber/5'-d](http://localhost:9200/subscribers/subscriber/5'-d)  
> > ' { "name": "Jane", "location": "Scottsdale" }'  
> > curl -XPUT '[http://localhost:9200/subscribers/subscriber/6'-d](http://localhost:9200/subscribers/subscriber/6'-d)  
> > ' { "name": "Bill", "location": "Phoenix" }'  
> > curl -XPUT '[http://localhost:9200/subscribers/subscriber/7'-d](http://localhost:9200/subscribers/subscriber/7'-d)  
> > ' { "name": "Steve", "location": "Phoenix" }'  
> > curl -XPUT '[http://localhost:9200/subscribers/subscriber/8'-d](http://localhost:9200/subscribers/subscriber/8'-d)  
> > ' { "name": "Lucy", "location": "Phoenix" }'  
> > curl -XPUT '[http://localhost:9200/subscribers/subscriber/9'-d](http://localhost:9200/subscribers/subscriber/9'-d)  
> > ' { "name": "Zane", "location": "Phoenix" }'  
> > curl -XPUT '[http://localhost:9200/subscribers/subscriber/10'-d](http://localhost:9200/subscribers/subscriber/10'-d)  
> > ' { "name": "George", "location": "Phoenix" }'  
> > curl -XPUT '[http://localhost:9200/subscribers/subscriber/11'-d](http://localhost:9200/subscribers/subscriber/11'-d)  
> > ' { "name": "Adam", "location": "Phoenix" }'  
> > curl -XPUT '[http://localhost:9200/subscribers/subscriber/12'-d](http://localhost:9200/subscribers/subscriber/12'-d)  
> > ' { "name": "Eve", "location": "Phoenix" }'  
> > curl -XPUT '[http://localhost:9200/subscribers/subscriber/13'-d](http://localhost:9200/subscribers/subscriber/13'-d)  
> > ' { "name": "William", "location": "Phoenix" }'  
> > curl -XPUT '[http://localhost:9200/subscribers/subscriber/14'-d](http://localhost:9200/subscribers/subscriber/14'-d)  
> > ' { "name": "Alan", "location": "Phoenix" }'  
> > curl -XPUT '[http://localhost:9200/subscribers/subscriber/15'-d](http://localhost:9200/subscribers/subscriber/15'-d)  
> > ' { "name": "Luke", "location": "Phoenix" }'  
> > curl -XPUT '[http://localhost:9200/subscribers/subscriber/16'-d](http://localhost:9200/subscribers/subscriber/16'-d)  
> > ' { "name": "Bo", "location": "Phoenix" }'
> 
> > I want to find the 'most popular' locations, I use:
> 
> > curl -XGET '[http://localhost:9200/subscribers/subscriber/\_search](http://localhost:9200/subscribers/subscriber/_search)?  
> > pretty=true' -d '  
> > { "size" : 0,  
> > "query" : {  
> > "match\_all" : { }  
> > },  
> > "facets" : {  
> > "locations" : {  
> > "terms" : {  
> > "field" : "location",  
> > "size" : 2  
> > }  
> > }  
> > }
> 
> > }'
> 
> > This works as expected:
> 
> > "facets" : {  
> > "locations" : {  
> > "\_type" : "terms",  
> > "missing" : 0,  
> > "total" : 16,  
> > "other" : 0,  
> > "terms" : [ {  
> > "term" : "phoenix",  
> > "count" : 14  
> > }, {  
> > "term" : "scottsdale",  
> > "count" : 2  
> > } ]  
> > }  
> > }
> 
> > Now I want to find the single most popular location (I changed the  
> > size field from 2 to 1):
> 
> > curl -XGET '[http://localhost:9200/subscribers/subscriber/\_search](http://localhost:9200/subscribers/subscriber/_search)?  
> > pretty=true' -d '  
> > { "size" : 0,  
> > "query" : {  
> > "match\_all" : { }  
> > },  
> > "facets" : {  
> > "locations" : {  
> > "terms" : {  
> > "field" : "location",  
> > "size" : 1  
> > }  
> > }  
> > }
> 
> > }'
> 
> > Result:
> 
> > "facets" : {  
> > "locations" : {  
> > "\_type" : "terms",  
> > "missing" : 0,  
> > "total" : 16,  
> > "other" : 3,  
> > "terms" : [ {  
> > "term" : "phoenix",  
> > "count" : 13  
> > } ]  
> > }  
> > }
> 
> > This is not what I expect. I still expect: other '2' and count '14'.  
> > Am I missing something?  
> > My version is 0.17.6

---

<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:50am UTC](https://discuss.elastic.co/t/facet-count-unexpected-result/5713/4 "2017-07-06T03:50:26Z")

</div>


