乐闻世界logo
搜索文章和话题

How does Elasticsearch handle pagination of search results?

1个答案

1

In Elasticsearch, pagination of search results is typically implemented using the from and size parameters.

  • The size parameter specifies the number of results to display per page.
  • The from parameter skips the initial number of results to achieve pagination.

For example, to retrieve data for the third page with 10 results per page, set size=10 and from=20 (as the third page skips the first 20 results).

Here is a specific example using Elasticsearch's query DSL (Domain-Specific Language):

json
GET /_search { "from": 20, "size": 10, "query": { "match": { "field_name": "search_text" } } }

In the above example, the first 20 search results (i.e., the content of the first and second pages) are skipped, and results starting from the 21st are retrieved for a total of 10 results, thus accessing the third page.

However, it is important to note that using from and size for pagination may encounter performance issues when dealing with large datasets. Elasticsearch needs to first retrieve the first from + size results before returning the size results starting from from. When from is very large, this can slow down query performance.

To optimize this, use the search_after parameter with a sort field for more efficient pagination. This method does not skip large amounts of data but directly continues from the last result of the previous page, significantly improving pagination efficiency, especially with large datasets.

A simple example of using search_after:

json
GET /_search { "size": 10, "query": { "match": { "field_name": "search_text" } }, "sort": [ { "timestamp": { "order": "asc" } } ], "search_after": [1609459200000] // This is the timestamp value of the last document from the previous page }

In this query, sort ensures results are ordered by a specific field (e.g., timestamp). The search_after parameter uses the sort field value of the last document from the previous page to directly start retrieving this page's data.

In summary, Elasticsearch provides flexible pagination capabilities, allowing both simple from and size methods and more efficient search_after methods for handling pagination of large datasets.

2024年8月13日 21:41 回复

你的答案