0

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?

Meyra
  • 19
  • 5
  • 1
    this answer may help: https://stackoverflow.com/a/42107414/4604579 (hint: use `search_after`) – Val Jul 09 '19 at 15:11
  • @Val But you can use `search_after` only if you know the `id` of the feature before. But when I scroll down and release the scroll bar on index 8001, I want to get the features from index 8001 to 8500 without loading the previous features. – Meyra Jul 09 '19 at 16:13

1 Answers1

0

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.

  • You can use `search_after` only if you know the `id` of the feature before. But when I scroll down and release the scroll bar on index 8001, I want to get the features from index 8001 to 8500 without loading the previous features. – Meyra Jul 09 '19 at 18:29
  • You don't need the `id` of the feature before for `search_after`. I have updated the answer with a more complete example. – Pierre-Nicolas Mougel Jul 10 '19 at 05:25
  • Wouldn't I be better of just search normal with `from`+ `size`. My problem was, that the order of the features could change. That's why I wanted to use something like `scroll`. But when I add a tie-breaker, then I could just use the normal search. Or are there benefits of using `search_after` which I don't recognize? – Meyra Jul 10 '19 at 07:45
  • If you are using `from` + `size` you are likely to hit the `index.max_result_window` threshold. By default it is set to 10000. You can increase this threshold but it is not advised to do so. From the documentation, `search_after` is more efficient to get paginated results – Pierre-Nicolas Mougel Jul 10 '19 at 09:05
  • For your problem regarding the change in the order of the feature, I guess that for the same search request the order should be the same. If the user change the search query, then it is generally ok to show the first page. Or maybe I did not understand your problem. – Pierre-Nicolas Mougel Jul 10 '19 at 09:08