Ronak Vora
Ronak Vora

Reputation: 310

Pagination with multi match query

I'm trying to figure out how to accomplish pagination with a multi match query using elasticsearch.

The scroll and search_after APIs seem like they won't work. scroll isn't meant for real time user requests as per documentation. search_after requires some unique field per id and requires you to sort on that field as per documentation but when using a multi-match query you're basically sorting by the score.

So, the only thing I've thought of so far is to do the following:

Send back last document id + score and use the score as the sort field. But, this could potentially return duplicate documents if other documents were added in between two queries.

Upvotes: 2

Views: 982

Answers (1)

Pierre Mallet
Pierre Mallet

Reputation: 7221

If you want to paginate the first option is to use from and size parameter in your query. The documentation here

Pagination of results can be done by using the from and size parameters. The from parameter defines the offset from the first result you want to fetch. The size parameter allows you to configure the maximum amount of hits to be returned.

Though from and size can be set as request parameters, they can also be set within the search body. from defaults to 0, and size defaults to 10.

Note that from + size can not be more than the index.max_result_window index setting which defaults to 10,000. See the Scroll or Search After API for more efficient ways to do deep scrolling.

If you don't need to paginate over 10k results it's your best choice. The max_result_window can be modified, but the performance will decrease as the selected page number will increase.

But of course if some documents are added during your user pagination they will be added and your pagination can be slightly inaccurate.

Upvotes: 1

Related Questions