from elasticsearch import Elasticsearch
from elasticsearch import helpers
# 假设已经有了一个Elasticsearch客户端实例
es = Elasticsearch()
# 使用search_after进行分页
def search_after_pagination(scroll_id, size):
# 获取上次查询的最后一条记录的sort值
last_sort_value = scroll_id
body = {
"size": size,
"query": {
"function_score": {
"query": {
"match_all": {}
},
"functions": [
{
"random_score": {
"seed": 123456,
"field": "_seq_no"
}
}
],
"boost_mode": "replace"
}
},
"sort": [
{
"_id": {
"order": "asc"
}
}
]
}
if last_sort_value:
body['search_after'] = last_sort_value
response = es.search(index='your_index', body=body)
# 返回结果和新的sort值
return response['hits']['hits'], response['hits']['sort'][0]
# 使用scroll进行深度分页
def scroll_pagination(size):
# 初始查询
scroll_id = 'initial'
s = helpers.scan(
client=es,
query={
"function_score": {
"query": {
"match_all": {}
},
"functions": [
{
"random_score": {
"seed": 123456,
"field": "_seq_no"
}
}
],
"boost_mode": "replace"
}
},
index='your_index',
size=size,
scroll='5m', # 设置scroll超时时间
)
for i, response in enumerate(s):
# 处理返回的结果
print(response)
if i == 10: # 假设我们只需要获取10个结果
评论已关闭