首页 > 解决方案 > 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)

标签: pythonjsonapielasticsearchscroll

解决方案


是不是因为您的电话是“return_es_results(res, 100)”<--- 请注意电话中的 100。

所以它会循环直到 100 ......你只问 100 个结果!

你可能想分页?如果您使用 django,这里有一些关于分页的文档: https ://docs.djangoproject.com/en/2.2/topics/pagination/


推荐阅读