# Wrong count in terms facet

**URL:** https://discuss.elastic.co/t/wrong-count-in-terms-facet/12596
**Category:** Elasticsearch
**Created:** [July 1, 2013, 10:05am UTC](https://discuss.elastic.co/t/wrong-count-in-terms-facet/12596 "2013-07-01T10:05:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![hridayeshgupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hridayeshgupta/32/2251_2.png) [@hridayeshgupta](https://discuss.elastic.co/u/hridayeshgupta)
#### Post date: [July 1, 2013, 10:05am UTC](https://discuss.elastic.co/t/wrong-count-in-terms-facet/12596/1 "2013-07-01T10:05:45Z")

</div>

Not sure if this is bug or I am missing something. But Terms facet is returning wrong count for number of terms.

I have a field which have str\_tag\_analyzer. I want to get Tag Cloud from from field. I want to get top 20 tags along with their count (How many times they appeared).

Terms facet looked solution for this case. I have an understanding that size parameter in Terms facet query controls how many tags will be returned.

When I run term facet query with different size, I get unexpected result. Here is my few queries and its result.

query 1  
  
curl -XGET '[http://localhost:9200/stage\_profiles/wrapper\_0/\_search?pretty=1](http://localhost:9200/stage_profiles/wrapper_0/_search?pretty=1)' -d '  
{  
query : {  
"nested" : {  
"query" : {  
"field" : {  
"gsid" : 222  
}  
},  
"path" : "medals"  
}  
}, from: 0, size: 0  
,  
facets: {  
"tags" : { "terms" : {"field" : "field\_val\_t", size: 1} }  
}  
}'

{  
"took" : 1,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 3,  
"successful" : 3,  
"failed" : 0  
},  
"hits" : {  
"total" : 189,  
"max\_score" : 1.0,  
"hits" : []  
},  
"facets" : {  
"tags" : {  
"\_type" : "terms",  
"missing" : 57,  
"total" : 331,  
"other" : 316,  
"terms" : [ {  
"term" : "hyderabad",  
"count" : 15  
} ]  
}  
}

Query 2 curl -XGET 'http://localhost:9200/stage\_profiles/wrapper\_0/\_search?pretty=1' -d ' { query : { "nested" : { "query" : { "field" : { "gsid" : 222 } }, "path" : "medals" } }, from: 0, size: 0 , facets: { "tags" : { "terms" : {"field" : "field\_val\_t", size: 3} } } }'

{  
"took" : 1,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 3,  
"successful" : 3,  
"failed" : 0  
},  
"hits" : {  
"total" : 189,  
"max\_score" : 1.0,  
"hits" : []  
},  
"facets" : {  
"tags" : {  
"\_type" : "terms",  
"missing" : 57,  
"total" : 331,  
"other" : 282,  
"terms" : [ {  
"term" : "playing",  
"count" : 20  
}, {  
"term" : "hyderabad",  
"count" : 15  
}, {  
"term" : "pune",  
"count" : 14  
} ]  
}  
}  
}  
  
Query 3  
  
curl -XGET '[http://localhost:9200/stage\_profiles/wrapper\_0/\_search?pretty=1](http://localhost:9200/stage_profiles/wrapper_0/_search?pretty=1)' -d '  
{  
query : {  
"nested" : {  
"query" : {  
"field" : {  
"gsid" : 222  
}  
},  
"path" : "medals"  
}  
}, from: 0, size: 0  
,  
facets: {  
"tags" : { "terms" : {"field" : "field\_val\_t", size: 10} }  
}  
}'  
{  
"took" : 1,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 3,  
"successful" : 3,  
"failed" : 0  
},  
"hits" : {  
"total" : 189,  
"max\_score" : 1.0,  
"hits" : []  
},  
"facets" : {  
"tags" : {  
"\_type" : "terms",  
"missing" : 57,  
"total" : 331,  
"other" : 198,  
"terms" : [ {  
"term" : "playing",  
"count" : 20  
}, {  
"term" : "hyderabad",  
"count" : 19  
}, {  
"term" : "bangalore",  
"count" : 18  
}, {  
"term" : "pune",  
"count" : 16  
}, {  
"term" : "chennai",  
"count" : 16  
}, {  
"term" : "games",  
"count" : 13  
}, {  
"term" : "testing",  
"count" : 11  
}, {  
"term" : "cricket",  
"count" : 9  
}, {  
"term" : "singing",  
"count" : 6  
}, {  
"term" : "movies",  
"count" : 5  
} ]  
}  
}  
}

I have following concerns

1. The first query is giving tag with count of 15, but there exists another tag with count 20 (that can be seen in query 2 and 3). So it must return "playing" tag with count 20.
2. 2nd query returns count of "hyderabad" tag as 15 but 3rd query returns count as 19 for the same tag.

Please let me know if you need any other info such as mapping, data present in ES.  
Thanks

---

<div class="post-metadata">

### Author: ![ppearcy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ppearcy/32/980_2.png) [@ppearcy](https://discuss.elastic.co/u/ppearcy)
#### Post date: [July 8, 2013, 4:49am UTC](https://discuss.elastic.co/t/wrong-count-in-terms-facet/12596/2 "2013-07-08T04:49:17Z")

</div>

You are running into a frequently hit problem, request a larger facet  
count, see here for more details:

> <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 :)

Best Regards,  
Paul

On Monday, July 1, 2013 4:05:45 AM UTC-6, Hridayesh Gupta wrote:

> Not sure if this is bug or I am missing something. But Terms facet is  
> returning wrong count for number of terms.
> 
> I have a field which have str\_tag\_analyzer. I want to get Tag Cloud from  
> from field. I want to get top 20 tags along with their count (How many  
> times  
> they appeared).
> 
> Terms facet looked solution for this case. I have an understanding that  
> size  
> parameter in Terms facet query controls how many tags will be returned.
> 
> When I run term facet query with different size, I get unexpected result.  
> Here is my few queries and its result.
> 
> query 1
> 
> Query 2
> 
> Query 3
> 
> I have following concerns
> 
> 1. The first query is giving tag with count of 15, but there exists  
> another  
> tag with count 20 (that can be seen in query 2 and 3). So it must return  
> "playing" tag with count 20.
> 2. 2nd query returns count of "hyderabad" tag as 15 but 3rd query returns  
> count as 19 for the same tag.
> 
> Please let me know if you need any other info such as mapping, data  
> present  
> in ES.  
> Thanks
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Wrong-count-in-terms-facet-tp4037300.html](http://elasticsearch-users.115913.n3.nabble.com/Wrong-count-in-terms-facet-tp4037300.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:27am UTC](https://discuss.elastic.co/t/wrong-count-in-terms-facet/12596/3 "2017-07-06T02:27:45Z")

</div>


