3

Im new to elasticasearch and trying to apply the cursor paginatoin seen in facebook, twitter for my api.

Is there a way to apply that throw filters, aggregations i already tried a couple queries but the issue that i can reserve the order of the documents, any help will be appreciated ?

AboElzooz
  • 309
  • 4
  • 16
  • 1
    Not sure about what you exactly want, but take a look at the `scroll` [api documentation](http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html) – ThomasC Mar 16 '15 at 12:57
  • This question is quite old, but you probably want to look at https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html - note the 'pit' tokens which seem to offer stable results. – David Goodwin Dec 07 '21 at 12:37

2 Answers2

2

The idea behind the cursor pagination is to work as a pointer.

In the ElasticSearch, you have the _search method where you can use the scroll propriety. This method will group the documents based on the size.

GET /<index>/_search?scroll=<ttl, example: 1m>
{
  "query": {
    <parameters>
  },
  "size": <integer, example: 1000>,
}

For the next slices of documents, you will use _search method but only with the scroll and scroll_id propriety. Internally, ES will return only the next slice of documents without recalculate again the documents.

GET /_search/scroll
{
  "scroll": <ttl, example: 1m>,
  "scroll_id": <scroll_id hash>
}

You should avoid to use size and from proprieties due to performance constraints.

Have a look on: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-scroll

Tiago Mateus
  • 356
  • 4
  • 17
-1

You can paginate through the from and size parameters:

{
    "from" : 0, "size" : 10,
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}
Zach
  • 9,591
  • 1
  • 38
  • 33