Monday, 12 October 2015

Elasticsearch: Pagination

By default Elasticsearch returns top 10 results, that match given query. What if you want to get next 10 results (results from 11 to 20). This is very easy, Elasticsearch takes two query parameters ‘from’, ‘size’ to support pagination.

from
Starting document

size
Specifies the number of results to be returned.

GET /_search?from=10&size=2
GET /_search?from=10
GET /_search?size=20

How pagination works
Suppose we are searching in a single index, with 5 primary shards. When you request for first page of results (By default elastic search returns 10 results), each shard returns top 10 results to the requesting node. The resulting node sorts all 50 results and return top 10 of them.

Don’t go for Deep pagination
Deep pagination effects performance. For example, Suppose you ask for documents from 20000 to 20010. Each shard produce top 20010 results and returns them to requesting node. Requesting node sorts 100050 (5 * 20010) documents and return results from 20000 to 20010. Whenever you are going for deep pagination, you need to sort more documents, which obviously impacts performance.




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment