# Limit query results while paginating them

**URL:** <https://discuss.elastic.co/t/limit-query-results-while-paginating-them/219739>\
**Category:** Elasticsearch\
**Created:** [February 18, 2020, 9:13am UTC](https://discuss.elastic.co/t/limit-query-results-while-paginating-them/219739 "2020-02-18T09:13:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ovinz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ovinz/32/59207_2.png) [@ovinz](https://discuss.elastic.co/u/ovinz)\
**Post date:** [February 18, 2020, 9:13am UTC](https://discuss.elastic.co/t/limit-query-results-while-paginating-them/219739/1 "2020-02-18T09:13:56Z")

</div>

Hello, I'd like to limit my query results to the first 500 documents and use the pagination (for instance from=0&size=30).

My use case :

- I want to hide the total of documents
- I don't use yet any relevancy aggs (scoring )
- The results are handle by an API (api-platform with a Paginator).

I thought `terminate_after` did the trick but it's not the right to do that apparently. I have some wrong results when I sort.

Do you have an advice to handle that ?  
Do I have to do it with a size of 500 doc and handle the pagination in my app (php in my case) ?

Thanks

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 18, 2020, 9:59am UTC](https://discuss.elastic.co/t/limit-query-results-while-paginating-them/219739/2 "2020-02-18T09:59:35Z")

</div>

Hey,

can you maybe explain what did not work as expected with using from/size even without terminate after? What did not work as expected, and what did you expect?

--Alex

---

<div class="post-metadata">

**Author:** ![ovinz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ovinz/32/59207_2.png) [@ovinz](https://discuss.elastic.co/u/ovinz)\
**Post date:** [February 18, 2020, 10:15am UTC](https://discuss.elastic.co/t/limit-query-results-while-paginating-them/219739/3 "2020-02-18T10:15:20Z")

</div>

Hi Alexander,

Thanks for your help

Using `from/size` feature allows me to divid results into small parts (30 documents in my case) but I'm not able, in the same time, to limit the whole query to a number (ie : 500).  
My query looks like this :

```
"sort": [
  {
	"createdAt": {
		"order": "desc"
	}
}],
"query": {
	"..."
},
"size": 30,
"from": 0
```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 18, 2020, 10:26am UTC](https://discuss.elastic.co/t/limit-query-results-while-paginating-them/219739/4 "2020-02-18T10:26:21Z")

</div>

I am not sure I understand.

What do you think you gain when limiting the query to 500? Speed? You still need to sort every hit properly by the sort criteria.

BUT: I'd highly encourage you to upgrade to Elasticsearch 7.6.0, as there is a huge speed up optimization when sorting by dates and numbers. See the release blog post at [https://www.elastic.co/blog/elasticsearch-7-6-0-released](https://www.elastic.co/blog/elasticsearch-7-6-0-released)

---

<div class="post-metadata">

**Author:** ![ovinz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ovinz/32/59207_2.png) [@ovinz](https://discuss.elastic.co/u/ovinz)\
**Post date:** [February 18, 2020, 10:47am UTC](https://discuss.elastic.co/t/limit-query-results-while-paginating-them/219739/5 "2020-02-18T10:47:22Z")

</div>

Yes I'll do the upgrade 😉

I'm not looking for performance at all but handle pagination with one query.  
In a natural form, I would have said  
"can you give me the first 500 results that match these criteria by cutting them out from 0 to 29 then from 30 to 60"...

But I must thinking wrong way.  
I should rather use a size of 500 and then slice the results within my php application

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 19, 2020, 9:17am UTC](https://discuss.elastic.co/t/limit-query-results-while-paginating-them/219739/6 "2020-02-19T09:17:19Z")

</div>

I do not understand, why you are not using from/size parameters for this.

```auto
from: 0, size: 30
from: 30, size: 30
from: 60, size: 30
...
from: 480, size: 20

```

in order to retrieve 500 results.

Why does that not work for you? What did I miss in your question? 🙂

--Alex

---

<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:** [March 18, 2020, 9:17am UTC](https://discuss.elastic.co/t/limit-query-results-while-paginating-them/219739/7 "2020-03-18T09:17:21Z")

</div>

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