Search code examples
elasticsearchelasticsearch-dsl

How to paginate results from Elasticsearch DSL in Python


I'm using Elasticsearch DSL and I would like to paginate through the results. To do that, I need to know the total number of results in the search. How should I best do that?

Do I do one search, then execute twice, one generally for the .hits.total and the other sliced for items? Something like this:

response = Link.search().filter("term", run_id=run_id)
total = response.execute().hits.total
links = response[start:end].execute()

Solution

  • Try this:

    dsl = Link.search().filter("term", run_id=run_id)
    response = dsl[start:end].execute()
    links = response.hits.hits
    total = response.hits.total
    

    ... only hits ElasticSearch once.

    official docs: https://elasticsearch-dsl.readthedocs.io/en/latest/search_dsl.html#pagination