Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 696
duality_ Avatar asked Mar 18 '15 11:03

duality_


People also ask

How does pagination work in Elasticsearch?

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.

What is Elasticsearch DSL?

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 elastic search in Python?

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.


1 Answers

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

like image 147
Cari Avatar answered Sep 18 '22 04:09

Cari