Paginate in elasticsearch

时间:2016-05-29 11:14:12

标签: elasticsearch flask pagination

我是弹性搜索的新手,至少对于烧瓶扩展(flask-elasticsearch),文档非常糟糕。任何帮助都是很有用的。

我有一个弹性搜索查询,看起来像这样

res = es.search(index="user-index", from_=(page-1)*10, size=10, body={"query": {"match_all": {}}})

我查询用户表,只想要10个结果。现在我仍然需要获得实际的表行。所以我像这样查询它们

list_of_ids = []
for hit in res['hits']['hits']:
    list_of_ids.append(hit["_id"])

search_result = models.User.query.filter(models.User.id.in_(list_of_ids)).paginate(page, 10, False)

你可以看到我正在使用paginate()函数。但是,由于我将10个ID列表传递给查询,因此paginate函数不知道搜索查询中的结果总数。所以paginate函数不能这样工作......

我可以自己完成所有的分页功能,但我想知道是否有一种很好的方法来保持分页功能并以某种方式将其与弹性搜索联系起来? 谢谢 卡尔

1 个答案:

答案 0 :(得分:2)

我找到了一个解决方案...可以只分配相关的分页值,如

search_result.total = res['hits']['total']
search_result.page = page

欢呼声

相关问题