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()
The simplest method of pagination uses the from and size parameters available in Elasticsearch's search API. By default, from is 0 and size is 10, meaning if you don't specify otherwise, Elasticsearch will return only the first ten results from your index.
Query DSLedit. Elasticsearch provides a full Query DSL (Domain Specific Language) based on JSON to define queries. Think of the Query DSL as an AST (Abstract Syntax Tree) of queries, consisting of two types of clauses: Leaf query clauses.
What is ElasticSearch? ElasticSearch (ES) is a distributed and highly available open-source search engine that is built on top of Apache Lucene. It's an open-source which is built in Java thus available for many platforms. You store unstructured data in JSON format which also makes it a NoSQL database.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With