# Sort before pagination

**URL:** <https://discuss.elastic.co/t/sort-before-pagination/300617>\
**Category:** Elasticsearch\
**Created:** [March 24, 2022, 6:13pm UTC](https://discuss.elastic.co/t/sort-before-pagination/300617 "2022-03-24T18:13:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rushil\_Patel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rushil_patel/32/103495_2.png) [@Rushil\_Patel](https://discuss.elastic.co/u/Rushil_Patel)\
**Post date:** [March 24, 2022, 6:13pm UTC](https://discuss.elastic.co/t/sort-before-pagination/300617/1 "2022-03-24T18:13:12Z")

</div>

Hey I am trying to sort and paginate the result simultaneously, but I am not getting the expected result.

NOTE: we are using 6.1 version  
Example:

```auto
data = [
{
level : 0
time: some date
... other fields
} ,
{
level: 1
time: some date
}.... and so on
]

// There are multiple data for each level

```

Here is my Elasticsearch sort query

```auto
sort = [
     {level: {order: 'asc'}},
    {time: {order: 'asc'}}
]

```

I have set the `limit: 20` and `from: 0`  
**Result:**  
I get the sort result with some `level: 0` and some `level:1` for the first page  
Move to next page same result as above some `level: 0` and some `level:1`

**Expected Result:**  
Should get all the `level:0` data first and then other levels, even through the pages  
-\> Example  
Let say I have 40 `level:0` data, 40 level:1 data and so on.  
So the expected result should be:  
As we have set the limit to 20 so First two pages should be level:0 data then next two pages should level: 1 data and so on.

---

<div class="post-metadata">

**Author:** ![DineshNaik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dineshnaik/32/89308_2.png) [@DineshNaik](https://discuss.elastic.co/u/DineshNaik)\
**Post date:** [March 25, 2022, 4:17am UTC](https://discuss.elastic.co/t/sort-before-pagination/300617/2 "2022-03-25T04:17:14Z")

</div>

Welcome to community Rushil,  
Have you looked at PIT (point in time) feature in es.

Kindly see if this helps:

> **[Paginate search results | Elasticsearch Guide \[8.1\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html)**

---

<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:** [April 22, 2022, 4:17am UTC](https://discuss.elastic.co/t/sort-before-pagination/300617/3 "2022-04-22T04:17:44Z")

</div>

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