# Search\_after giving wrong result with backward pagination in elasticsearch

**URL:** https://discuss.elastic.co/t/search-after-giving-wrong-result-with-backward-pagination-in-elasticsearch/190983
**Category:** Elasticsearch
**Created:** [July 17, 2019, 12:02pm UTC](https://discuss.elastic.co/t/search-after-giving-wrong-result-with-backward-pagination-in-elasticsearch/190983 "2019-07-17T12:02:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![akhilmathew001](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akhilmathew001/32/77941_2.png) [@akhilmathew001](https://discuss.elastic.co/u/akhilmathew001)
#### Post date: [July 17, 2019, 12:02pm UTC](https://discuss.elastic.co/t/search-after-giving-wrong-result-with-backward-pagination-in-elasticsearch/190983/1 "2019-07-17T12:02:00Z")

</div>

With elasticsearch, I am trying to implement pagination with the help of search\_after. I am getting an issue when I am doing backward pagination, ie. wrong result (Differs from actual results from the previous page).

For forward pagination, I am using elastic query as follows..

```
{
"sort": [{
    "threshold": "desc"
}, {
    "last_login": {
        "order": "desc",
        "mode": "avg"
    }
}, {
    "id": {
        "order": "asc"
    }
}],
"search_after": ["0", 1563037185, "3591152"],
"from": 0,
"_source": [],
"track_total_hits": true,
"track_scores": false,
"aggs": {},
"query": {
    "bool": {
        "filter": {
            "bool": {
                "should": [],
                "must_not": [],
                "must": []
            }
        },
        "should": [],
        "must_not": [],
        "must": []
    }
},
"size": 25

}

```

Note: Here search\_after value is the sort array of the last user from the current pagination.

For doing reverse pagination,

I am just reversing the sort order of the sorting options as mentioned in the thread [elastic discussion](https://discuss.elastic.co/t/how-to-get-a-previous-page-from-elasticsearch-using-search-after/105028/2). My elastic query is now as follows..

```
{
"sort": [{
    "threshold": "asc"
}, {
    "last_login": {
        "order": "asc",
        "mode": "avg"
    }
}, {
    "id": {
        "order": "desc"
    }
}],
"search_after": ["0", 1561460886, "3275906"],
"from": 0,
"_source": [],
"track_total_hits": true,
"track_scores": false,
"aggs": {},
"query": {
    "bool": {
        "filter": {
            "bool": {
                "should": [],
                "must_not": [],
                "must": []
            }
        },
        "should": [],
        "must_not": [],
        "must": []
    }
},
"size": 25

}

```

Here search\_after is the value from the sort array of the last user in the current pagination. This time instead of showing previous page results, elastic gives some other results.

Note: Elasticsearch version 7.2  
**NB: There is no realtime update or indexing happening to the elasticsearch.**

---

<div class="post-metadata">

### Author: ![xeraa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xeraa/32/48181_2.png) [@xeraa](https://discuss.elastic.co/u/xeraa)
#### Post date: [July 30, 2019, 2:51am UTC](https://discuss.elastic.co/t/search-after-giving-wrong-result-with-backward-pagination-in-elasticsearch/190983/2 "2019-07-30T02:51:41Z")

</div>

> [@akhilmathew001](#):
>
> elastic gives some other results

Trying to understand what is happening there: The sort order is wrong or the start position / offset is not what you expect?

---

<div class="post-metadata">

### Author: ![akhilmathew001](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akhilmathew001/32/77941_2.png) [@akhilmathew001](https://discuss.elastic.co/u/akhilmathew001)
#### Post date: [July 31, 2019, 7:27am UTC](https://discuss.elastic.co/t/search-after-giving-wrong-result-with-backward-pagination-in-elasticsearch/190983/3 "2019-07-31T07:27:11Z")

</div>

I am getting the previous page in a reversed order. Previous page first user is coming in last with search\_after backward pagination. If I am using sorting based on score then entirly different page is coming.

---

<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: [August 28, 2019, 7:27am UTC](https://discuss.elastic.co/t/search-after-giving-wrong-result-with-backward-pagination-in-elasticsearch/190983/4 "2019-08-28T07:27:18Z")

</div>

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