Search_after giving wrong result with backward pagination in elasticsearch

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.

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

1 Like

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.

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