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. 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.