# Elasticsearch Facets + limit results

**URL:** <https://discuss.elastic.co/t/elasticsearch-facets-limit-results/17669>\
**Category:** Elasticsearch\
**Created:** [May 22, 2014, 8:47am UTC](https://discuss.elastic.co/t/elasticsearch-facets-limit-results/17669 "2014-05-22T08:47:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martijn\_Dwars](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martijn_dwars/32/1537_2.png) [@Martijn\_Dwars](https://discuss.elastic.co/u/Martijn_Dwars)\
**Post date:** [May 22, 2014, 8:47am UTC](https://discuss.elastic.co/t/elasticsearch-facets-limit-results/17669/1 "2014-05-22T08:47:08Z")

</div>

I'm trying to construct the following SQL query in Elasticsearch:

SELECT companyId, COUNT(\*) c FROM visits GROUP BY companyId ORDER BY c DESC  
LIMIT 2

I came up with the following JSON body for the query:

{  
"facets": {  
"company": {  
"filter": {  
"term": {  
"entityType": "companypage"  
}  
},  
"terms": {  
"field": "entityId",  
"size": 2  
}  
}  
}  
}

When I use "size": 2, I get the following result:

facets: {  
company: {  
\_type: terms  
missing: 0  
total: 4  
other: 0  
terms: [{  
term: 2  
count: 3  
},  
{  
term: 20  
count: 1  
}]  
}  
}

When I use "size": 1, I get the following result:

facets: {  
company: {  
\_type: terms  
missing: 0  
total: 4  
other: 2  
terms: [{  
term: 2  
count: 2  
}]  
}  
}

How is it possible that the count for term 2 is 3 in the first response,  
but 2 in the second response?

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/45ccf14f-81f2-4a9e-b598-6eb120f46197%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/45ccf14f-81f2-4a9e-b598-6eb120f46197%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![emeschitc](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@emeschitc](https://discuss.elastic.co/u/emeschitc)\
**Post date:** [May 22, 2014, 1:44pm UTC](https://discuss.elastic.co/t/elasticsearch-facets-limit-results/17669/2 "2014-05-22T13:44:50Z")

</div>

from the documentation:  
The size parameter defines how many top terms should be returned out of the  
overall terms list. By default, the node coordinating the search process  
will ask each shard to provide its own top size terms and once all shards  
respond, it will reduce the results to the final list that will then be  
sent back to the client. This means that if the number of unique terms is  
greater than size, the returned list is slightly off and not accurate (it  
could be that the term counts are slightly off and it could even be that a  
term that should have been in the top size entries was not returned).

On Thu, May 22, 2014 at 10:47 AM, Martijn Dwars [via Elasticsearch Users] \<  
[ml-node+s115913n4056258h90@n3.nabble.com](mailto:ml-node+s115913n4056258h90@n3.nabble.com)\> wrote:

> I'm trying to construct the following SQL query in Elasticsearch:
> 
> SELECT companyId, COUNT(\*) c FROM visits GROUP BY companyId ORDER BY c  
> DESC LIMIT 2
> 
> I came up with the following JSON body for the query:
> 
> {  
> "facets": {  
> "company": {  
> "filter": {  
> "term": {  
> "entityType": "companypage"  
> }  
> },  
> "terms": {  
> "field": "entityId",  
> "size": 2  
> }  
> }  
> }  
> }
> 
> When I use "size": 2, I get the following result:
> 
> facets: {  
> company: {  
> \_type: terms  
> missing: 0  
> total: 4  
> other: 0  
> terms: [{  
> term: 2  
> count: 3  
> },  
> {  
> term: 20  
> count: 1  
> }]  
> }  
> }
> 
> When I use "size": 1, I get the following result:
> 
> facets: {  
> company: {  
> \_type: terms  
> missing: 0  
> total: 4  
> other: 2  
> terms: [{  
> term: 2  
> count: 2  
> }]  
> }  
> }
> 
> How is it possible that the count for term 2 is 3 in the first response,  
> but 2 in the second response?
> 
> --  
> 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 [hidden email][http://user/SendEmail.jtp?type=node&node=4056258&i=0](http://user/SendEmail.jtp?type=node&node=4056258&i=0)  
> .  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/45ccf14f-81f2-4a9e-b598-6eb120f46197%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/45ccf14f-81f2-4a9e-b598-6eb120f46197%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/45ccf14f-81f2-4a9e-b598-6eb120f46197%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/45ccf14f-81f2-4a9e-b598-6eb120f46197%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> * * *
> 
> If you reply to this email, your message will be added to the discussion  
> below:
> 
> [http://elasticsearch-users.115913.n3.nabble.com/Elasticsearch-Facets-limit-results-tp4056258.html](http://elasticsearch-users.115913.n3.nabble.com/Elasticsearch-Facets-limit-results-tp4056258.html)  
> To unsubscribe from Elasticsearch Users, click here[http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe\_by\_code&node=115913&code=ZW1lc2NoaXRjQGdtYWlsLmNvbXwxMTU5MTN8LTExODcwOTk0NDI=](http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=115913&code=ZW1lc2NoaXRjQGdtYWlsLmNvbXwxMTU5MTN8LTExODcwOTk0NDI=)  
> .  
> NAML[http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=macro\_viewer&id=instant\_html!nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify\_subscribers!nabble%3Aemail.naml-instant\_emails!nabble%3Aemail.naml-send\_instant\_email!nabble%3Aemail.naml](http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml)

---

<div class="post-metadata">

**Author:** ![emeschitc](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@emeschitc](https://discuss.elastic.co/u/emeschitc)\
**Post date:** [May 22, 2014, 1:55pm UTC](https://discuss.elastic.co/t/elasticsearch-facets-limit-results/17669/3 "2014-05-22T13:55:08Z")

</div>

How is it possible that the count for term 2 is 3 in the first response,

> but 2 in the second response?

From the docs:

The size parameter defines how many top terms should be returned out of the  
overall terms list. By default, the node coordinating the search process  
will ask each shard to provide its own top size terms and once all shards  
respond, it will reduce the results to the final list that will then be  
sent back to the client. This means that if the number of unique terms is  
greater than size, the returned list is slightly off and not accurate (it  
could be that the term counts are slightly off and it could even be that a  
term that should have been in the top size entries was not returned).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/9a37d6e2-6a8b-47dd-9180-8ff6f720a41e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9a37d6e2-6a8b-47dd-9180-8ff6f720a41e%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Martijn\_Dwars](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martijn_dwars/32/1537_2.png) [@Martijn\_Dwars](https://discuss.elastic.co/u/Martijn_Dwars)\
**Post date:** [May 24, 2014, 1:21pm UTC](https://discuss.elastic.co/t/elasticsearch-facets-limit-results/17669/4 "2014-05-24T13:21:25Z")

</div>

Is there any way to accomplish the same goal and get an accurate result?

Op donderdag 22 mei 2014 15:55:08 UTC+2 schreef [emes...@gmail.com](mailto:emes...@gmail.com):

> How is it possible that the count for term 2 is 3 in the first response,
> 
> > but 2 in the second response?
> 
> From the docs:
> 
> The size parameter defines how many top terms should be returned out of  
> the overall terms list. By default, the node coordinating the search  
> process will ask each shard to provide its own top size terms and once  
> all shards respond, it will reduce the results to the final list that will  
> then be sent back to the client. This means that if the number of unique  
> terms is greater than size, the returned list is slightly off and not  
> accurate (it could be that the term counts are slightly off and it could  
> even be that a term that should have been in the top size entries was not  
> returned).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/196ee17f-7355-4fea-a74b-81f49ca9805f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/196ee17f-7355-4fea-a74b-81f49ca9805f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [May 24, 2014, 5:12pm UTC](https://discuss.elastic.co/t/elasticsearch-facets-limit-results/17669/5 "2014-05-24T17:12:02Z")

</div>

One way to have improve accuracy would be to increase shard\_size[1]. In  
particular if shard\_size is greater than the number of unique vaues of your  
entityId field, results will be accurate. Please however beware that this  
can be resource intensive.

Another option would be to route your indexing requests so that all  
documents having the same entityId will end up on the same shard.

[1]

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

On Sat, May 24, 2014 at 3:21 PM, Martijn [ikben@martijndwars.nl](mailto:ikben@martijndwars.nl) wrote:

> Is there any way to accomplish the same goal and get an accurate result?
> 
> Op donderdag 22 mei 2014 15:55:08 UTC+2 schreef [emes...@gmail.com](mailto:emes...@gmail.com):
> 
> > How is it possible that the count for term 2 is 3 in the first response,
> > 
> > > but 2 in the second response?
> > 
> > From the docs:
> > 
> > The size parameter defines how many top terms should be returned out of  
> > the overall terms list. By default, the node coordinating the search  
> > process will ask each shard to provide its own top size terms and once  
> > all shards respond, it will reduce the results to the final list that will  
> > then be sent back to the client. This means that if the number of unique  
> > terms is greater than size, the returned list is slightly off and not  
> > accurate (it could be that the term counts are slightly off and it could  
> > even be that a term that should have been in the top size entries was  
> > not returned).
> > 
> > --  
> > 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).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/196ee17f-7355-4fea-a74b-81f49ca9805f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/196ee17f-7355-4fea-a74b-81f49ca9805f%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/196ee17f-7355-4fea-a74b-81f49ca9805f%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/196ee17f-7355-4fea-a74b-81f49ca9805f%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j66iRhMX0wKi\_66LSy8e%3D%3DAAoU%2BjzJgRzOekBqrSmyBLQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j66iRhMX0wKi_66LSy8e%3D%3DAAoU%2BjzJgRzOekBqrSmyBLQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:27am UTC](https://discuss.elastic.co/t/elasticsearch-facets-limit-results/17669/6 "2017-07-06T01:27:12Z")

</div>


