# Facet counts change when setting size from 12 to 13

**URL:** <https://discuss.elastic.co/t/facet-counts-change-when-setting-size-from-12-to-13/12857>\
**Category:** Elasticsearch\
**Created:** [July 19, 2013, 10:30am UTC](https://discuss.elastic.co/t/facet-counts-change-when-setting-size-from-12-to-13/12857 "2013-07-19T10:30:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![svandenborne](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/svandenborne/32/2217_2.png) [@svandenborne](https://discuss.elastic.co/u/svandenborne)\
**Post date:** [July 19, 2013, 10:30am UTC](https://discuss.elastic.co/t/facet-counts-change-when-setting-size-from-12-to-13/12857/1 "2013-07-19T10:30:27Z")

</div>

In ES 0.90.0:

I have the following query:

{ "fields": ["company\_city.untouched"],  
"query":{"query\_string":{"query":"antwerpen","default\_operator":"AND"}},  
"facets":{  
"cities" : { "terms" : {"field" : "company\_city.untouched"  
,"size": "12"} },  
"test":  
{"filter": {"term":{"company\_city.untouched": "Brasschaat"}}}  
}  
}

Which gives me:

"facets": {  
"cities": {  
"\_type": "terms",  
"missing": 0,  
"total": 122,  
"other": 45,  
"terms": [  
...  
{  
"term": "Brasschaat",  
"count": 3  
},  
...  
]  
},  
"test": {  
"\_type": "filter",  
"count": 4  
}

I did not understand why the count was not 4 for "Brasschaat" for both  
facets (By the way I get the same result when not specifying a size).

Now as soon as I set the size to a value bigger than 12, the count of the  
cities facet for "Brasschaat" changes to 4.

So when I run:  
{ "fields": ["company\_city.untouched"],  
"query":{"query\_string":{"query":"antwerpen","default\_operator":"AND"}},  
"facets":{  
"cities" : { "terms" : {"field" : "company\_city.untouched"  
,"size": "13"} },  
"test":  
{"filter": {"term":{"company\_city.untouched": "Brasschaat"}}}  
}  
}

I get:  
"facets": {  
"cities": {  
"\_type": "terms",  
"missing": 0,  
"total": 122,  
"other": 45,  
"terms": [  
...  
{  
"term": "Brasschaat",  
"count": 4  
},  
...  
]  
},  
"test": {  
"\_type": "filter",  
"count": 4  
}

The same thing happens for one of the other cities. The counts of all other  
cities do not change..

This is on my local machine, the data is not changing and there is only one  
shard.

Any idea what is happening here?  
Am I doing something wrong?

Thanks!

Sandy

--  
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:** ![DH1](https://avatars.discourse-cdn.com/v4/letter/d/65b543/32.png) [@DH1](https://discuss.elastic.co/u/DH1)\
**Post date:** [July 19, 2013, 12:33pm UTC](https://discuss.elastic.co/t/facet-counts-change-when-setting-size-from-12-to-13/12857/2 "2013-07-19T12:33:51Z")

</div>

This is a normal behaviour.

When you do a facet, the top x is calculated on each shards, and then,  
those results a merged to make the total count.  
The list of x terms is not necessary the same on each shards, so, the  
result may end up with counts inferior to reality, and even with a false  
top x.  
here, we "solved" the problem by facetting with a size of x+20, and then  
just taking into account the x first counts.  
It will increase accuracy, but will not, however, make for 100% accurates  
counts. It was enough for us anyway.  
If you need 100 % accuracy, you'll have to facet with a size equal to the  
total number of different terms of that field.

Hope it helps.

Le vendredi 19 juillet 2013 12:30:27 UTC+2, Sandy Van den Borne a écrit :

> In ES 0.90.0:
> 
> I have the following query:
> 
> { "fields": ["company\_city.untouched"],  
> "query":{"query\_string":{"query":"antwerpen","default\_operator":"AND"}},  
> "facets":{  
> "cities" : { "terms" : {"field" : "company\_city.untouched"  
> ,"size": "12"} },  
> "test":  
> {"filter": {"term":{"company\_city.untouched": "Brasschaat"}}}  
> }  
> }
> 
> Which gives me:
> 
> "facets": {  
> "cities": {  
> "\_type": "terms",  
> "missing": 0,  
> "total": 122,  
> "other": 45,  
> "terms": [  
> ...  
> {  
> "term": "Brasschaat",  
> "count": 3  
> },  
> ...  
> ]  
> },  
> "test": {  
> "\_type": "filter",  
> "count": 4  
> }
> 
> I did not understand why the count was not 4 for "Brasschaat" for both  
> facets (By the way I get the same result when not specifying a size).
> 
> Now as soon as I set the size to a value bigger than 12, the count of the  
> cities facet for "Brasschaat" changes to 4.
> 
> So when I run:  
> { "fields": ["company\_city.untouched"],  
> "query":{"query\_string":{"query":"antwerpen","default\_operator":"AND"}},  
> "facets":{  
> "cities" : { "terms" : {"field" : "company\_city.untouched"  
> ,"size": "13"} },  
> "test":  
> {"filter": {"term":{"company\_city.untouched": "Brasschaat"}}}  
> }  
> }
> 
> I get:  
> "facets": {  
> "cities": {  
> "\_type": "terms",  
> "missing": 0,  
> "total": 122,  
> "other": 45,  
> "terms": [  
> ...  
> {  
> "term": "Brasschaat",  
> "count": 4  
> },  
> ...  
> ]  
> },  
> "test": {  
> "\_type": "filter",  
> "count": 4  
> }
> 
> The same thing happens for one of the other cities. The counts of all  
> other cities do not change..
> 
> This is on my local machine, the data is not changing and there is only  
> one shard.
> 
> Any idea what is happening here?  
> Am I doing something wrong?
> 
> Thanks!
> 
> Sandy

--  
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:** ![Radim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radim/32/2213_2.png) [@Radim](https://discuss.elastic.co/u/Radim)\
**Post date:** [July 22, 2013, 11:53am UTC](https://discuss.elastic.co/t/facet-counts-change-when-setting-size-from-12-to-13/12857/3 "2013-07-22T11:53:22Z")

</div>

This is a long-standing issue with ES, see "Terms facet gives wrong count  
with n\_shards \> 1"

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

-rr

On Friday, July 19, 2013 12:30:27 PM UTC+2, Sandy Van den Borne wrote:

> In ES 0.90.0:
> 
> I have the following query:
> 
> { "fields": ["company\_city.untouched"],  
> "query":{"query\_string":{"query":"antwerpen","default\_operator":"AND"}},  
> "facets":{  
> "cities" : { "terms" : {"field" : "company\_city.untouched"  
> ,"size": "12"} },  
> "test":  
> {"filter": {"term":{"company\_city.untouched": "Brasschaat"}}}  
> }  
> }
> 
> Which gives me:
> 
> "facets": {  
> "cities": {  
> "\_type": "terms",  
> "missing": 0,  
> "total": 122,  
> "other": 45,  
> "terms": [  
> ...  
> {  
> "term": "Brasschaat",  
> "count": 3  
> },  
> ...  
> ]  
> },  
> "test": {  
> "\_type": "filter",  
> "count": 4  
> }
> 
> I did not understand why the count was not 4 for "Brasschaat" for both  
> facets (By the way I get the same result when not specifying a size).
> 
> Now as soon as I set the size to a value bigger than 12, the count of the  
> cities facet for "Brasschaat" changes to 4.
> 
> So when I run:  
> { "fields": ["company\_city.untouched"],  
> "query":{"query\_string":{"query":"antwerpen","default\_operator":"AND"}},  
> "facets":{  
> "cities" : { "terms" : {"field" : "company\_city.untouched"  
> ,"size": "13"} },  
> "test":  
> {"filter": {"term":{"company\_city.untouched": "Brasschaat"}}}  
> }  
> }
> 
> I get:  
> "facets": {  
> "cities": {  
> "\_type": "terms",  
> "missing": 0,  
> "total": 122,  
> "other": 45,  
> "terms": [  
> ...  
> {  
> "term": "Brasschaat",  
> "count": 4  
> },  
> ...  
> ]  
> },  
> "test": {  
> "\_type": "filter",  
> "count": 4  
> }
> 
> The same thing happens for one of the other cities. The counts of all  
> other cities do not change..
> 
> This is on my local machine, the data is not changing and there is only  
> one shard.
> 
> Any idea what is happening here?  
> Am I doing something wrong?
> 
> Thanks!
> 
> Sandy

--  
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:25am UTC](https://discuss.elastic.co/t/facet-counts-change-when-setting-size-from-12-to-13/12857/4 "2017-07-06T02:25:13Z")

</div>


