# Wrong count for terms facet

**URL:** <https://discuss.elastic.co/t/wrong-count-for-terms-facet/12597>\
**Category:** Elasticsearch\
**Created:** [July 1, 2013, 10:30am UTC](https://discuss.elastic.co/t/wrong-count-for-terms-facet/12597 "2013-07-01T10:30:41Z")\
**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:30am UTC](https://discuss.elastic.co/t/wrong-count-for-terms-facet/12597/1 "2013-07-01T10:30:41Z")

</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](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

--  
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:** ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)\
**Post date:** [July 1, 2013, 11:01am UTC](https://discuss.elastic.co/t/wrong-count-for-terms-facet/12597/2 "2013-07-01T11:01:27Z")

</div>

Hi,

it seems you might be hitting this issue

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

There are some workarounds proposed in the discussion in that ticket.

Regards,  
Lukas

On Mon, Jul 1, 2013 at 12:30 PM, Hridayesh Gupta [hridayeshiitd@gmail.com](mailto:hridayeshiitd@gmail.com)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
> 
> 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](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
> 
> --  
> 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).

--  
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:28am UTC](https://discuss.elastic.co/t/wrong-count-for-terms-facet/12597/3 "2017-07-06T02:28:49Z")

</div>


