7

我正在使用Elasticsearch DSL,我想对结果进行分页。为此,我需要知道搜索结果的总数。我应该如何最好地做到这一点?

我是否进行一次搜索,然后执行两次,一次通常用于 ,.hits.total另一次切片用于项目?像这样的东西:

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

1 回答 1

9

尝试这个:

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

... 只点击 ElasticSearch 一次。

官方文档:https ://elasticsearch-dsl.readthedocs.io/en/latest/search_dsl.html#pagination

于 2015-03-18T14:30:34.900 回答