I'm using elasticsearch and kendo-ui grid. Now I'm switching from endless scroll to virtual scroll. At the moment I use the Scroll Api in elasticsearch, but can't control the range for my request.
Is there a way to get a session like in scroll, where I could send the from
and size
? Or is there a better way to handle this?
Use the search_after parameter in your search query.
search_after
can be used when you sort your documents on one or multiple fields. In the search_after
parameter you set the values for the fields that should be higher for all retrieved documents.
If you have for example a numeric field document_position
with values from 0 to 1000. To retrieve the documents with document_position
values higher than 200 you use the following query :
{
"query": { // your query },
"sort": "document_position",
"search_after": [200]
}
You can use the score
value in the search after parameter, but be aware that by default the score is not necessary consistent beetween search request. The documentation presents a solution to this problem. If you use the score
(or any other field which is not unique for all documents), you will also have to sort on a second field that must be unique for all documents.