7

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()

1 Answer 1

10

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.