python - ElasticSearch Scroll api 问题
问题描述
我正在尝试在 Python 中使用滚动 API,但我遇到了一个问题,因为它在我的整个数据集中循环。
当应该有超过 150k 个结果时,我得到了大约 100 个返回结果(我可以在 kibana 中看到它们)
附件是我的代码
res = helpers.scan(client = es, scroll = '2m', query = {
"size": 10000,
"query": {
"match": {
"type": {
"query": "IP_Address"
}}}},
index = "logstash-*")
# function to return hits from the elasticsearch query in res
def get_es_json(es_scan):
for hits in es_scan:
return hits
# iterate through results with defined number of results
def return_es_results(es_json_data, num_results):
for i in range(num_results):
data = get_es_json(es_json_data)
print(data['_source']['geoip']['asn'])
return_es_results(res, 100)
解决方案
是不是因为您的电话是“return_es_results(res, 100)”<--- 请注意电话中的 100。
所以它会循环直到 100 ......你只问 100 个结果!
你可能想分页?如果您使用 django,这里有一些关于分页的文档: https ://docs.djangoproject.com/en/2.2/topics/pagination/
推荐阅读
- sql - Invalid expression in the select list when using group by
- java - 在 Intents 中传递 FirebaseFirestore 引用
- apache - Tomcat 是 HTTP 服务器还是 Servlet 容器?
- reactjs - 从快速服务器发出渲染反应应用程序
- git - VS2017:构建子文件夹下的 git
- java - Java Vector3D method priorities
- php - 如何引用另一个参数作为函数中参数的默认值?
- laravel - 将 Laravel 与 Netsuite 集成
- css - Fixed positioning not at the top of current page view
- c++ - 如何减少 constexpr 函数的编译时间?