# Elasticsearch limit count result

**URL:** <https://discuss.elastic.co/t/elasticsearch-limit-count-result/127157>\
**Category:** Elasticsearch\
**Created:** [April 7, 2018, 9:00am UTC](https://discuss.elastic.co/t/elasticsearch-limit-count-result/127157 "2018-04-07T09:00:07Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![rrader](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@rrader](https://discuss.elastic.co/u/rrader)\
**Post date:** [April 7, 2018, 9:00am UTC](https://discuss.elastic.co/t/elasticsearch-limit-count-result/127157/1 "2018-04-07T09:00:07Z")

</div>

How in Elasticsearch to limit count result to maximum of 1000?

So, If count have 400 results, then it respond with 400, if count have 1200, it repond with 1000.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 7, 2018, 10:06am UTC](https://discuss.elastic.co/t/elasticsearch-limit-count-result/127157/2 "2018-04-07T10:06:54Z")

</div>

Why do you want to do this?

I mean, can you describe exactly what you are after? Is that for computation purpose?  
If so the sampler `aggregation` might be what you want.

---

<div class="post-metadata">

**Author:** ![rrader](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@rrader](https://discuss.elastic.co/u/rrader)\
**Post date:** [April 7, 2018, 10:13am UTC](https://discuss.elastic.co/t/elasticsearch-limit-count-result/127157/3 "2018-04-07T10:13:08Z")

</div>

I need this for pagination, so in our application we only show 5 page indexes (1 2 3 4 5; 4 5 6 7 8), so I need to count from current page how many page are after, but I need to know if there are maximum 2-4 pages, I don't need to know if there are 100 pages after current

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 7, 2018, 10:38am UTC](https://discuss.elastic.co/t/elasticsearch-limit-count-result/127157/4 "2018-04-07T10:38:49Z")

</div>

According that you have the total number of hits, the `size` and `from` parameters, I think that you can do all that on client side IMO, no?

---

<div class="post-metadata">

**Author:** ![rrader](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@rrader](https://discuss.elastic.co/u/rrader)\
**Post date:** [April 7, 2018, 10:49am UTC](https://discuss.elastic.co/t/elasticsearch-limit-count-result/127157/5 "2018-04-07T10:49:30Z")

</div>

Yes I can do this with total number of hits, but total number can 1000000, but I only need to know if after page with index "n" are 3 more pages or not, so I want to limit the count

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 7, 2018, 11:06am UTC](https://discuss.elastic.co/t/elasticsearch-limit-count-result/127157/6 "2018-04-07T11:06:11Z")

</div>

I'm afraid I don't understand.

May be someone else does.

---

<div class="post-metadata">

**Author:** ![rrader](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@rrader](https://discuss.elastic.co/u/rrader)\
**Post date:** [April 7, 2018, 11:18am UTC](https://discuss.elastic.co/t/elasticsearch-limit-count-result/127157/7 "2018-04-07T11:18:55Z")

</div>

We need to show a pagination, pagination only show 5 index on page, examples  
1 2 3 4 5  
3 4 5 6 7  
4 5 6 7 8

so when we show page with index 5, we need to show pagination  
3 4 5 6 7  
2 pages before and 2 pages after  
so, when we show page with index 5 we need to know only if page with index 6 and 7 exist

I need to count/check, from offset 50, if there are more 20 elements or less

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 7, 2018, 11:36am UTC](https://discuss.elastic.co/t/elasticsearch-limit-count-result/127157/8 "2018-04-07T11:36:47Z")

</div>

Knowing the total number of documents that match the query should be enough to compute that, no?

---

<div class="post-metadata">

**Author:** ![rrader](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@rrader](https://discuss.elastic.co/u/rrader)\
**Post date:** [April 7, 2018, 11:45am UTC](https://discuss.elastic.co/t/elasticsearch-limit-count-result/127157/9 "2018-04-07T11:45:50Z")

</div>

Yes it is enough, but in this case the engine will do unneeded extra work, no?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 7, 2018, 1:00pm UTC](https://discuss.elastic.co/t/elasticsearch-limit-count-result/127157/10 "2018-04-07T13:00:11Z")

</div>

No. It won't.

---

<div class="post-metadata">

**Author:** ![rrader](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@rrader](https://discuss.elastic.co/u/rrader)\
**Post date:** [April 7, 2018, 1:14pm UTC](https://discuss.elastic.co/t/elasticsearch-limit-count-result/127157/11 "2018-04-07T13:14:17Z")

</div>

Hmm, but it should analyze all documents, even if it will find 20, it will continue to search all documents in order to calculate the count, how it will know that at 20 it must stop?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 7, 2018, 2:44pm UTC](https://discuss.elastic.co/t/elasticsearch-limit-count-result/127157/12 "2018-04-07T14:44:10Z")

</div>

Is it an issue? What is the response time you have?

---

<div class="post-metadata">

**Author:** ![rrader](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@rrader](https://discuss.elastic.co/u/rrader)\
**Post date:** [April 7, 2018, 5:50pm UTC](https://discuss.elastic.co/t/elasticsearch-limit-count-result/127157/13 "2018-04-07T17:50:01Z")

</div>

At the moment it is not an issue, but I want to prevent it to be an issue.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 7, 2018, 7:25pm UTC](https://discuss.elastic.co/t/elasticsearch-limit-count-result/127157/14 "2018-04-07T19:25:32Z")

</div>

My advice: Don't try to fix problems that do not exist.

---

<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:** [May 5, 2018, 7:25pm UTC](https://discuss.elastic.co/t/elasticsearch-limit-count-result/127157/15 "2018-05-05T19:25:44Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
