为什么elasticsearch报告的点击数因查询方法而异?

2024-05-17 02:53:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个elasticsearch索引,有60k个元素。我知道通过检查head插件,我通过Sense得到相同的信息(结果在右下角)

enter image description here

然后,我想用两种不同的方式从Python查询相同的索引:通过直接的requests调用和使用elasticsearch模块:

import elasticsearch
import json
import requests

# the requests version
data = {"query": {"match_all": {}}}
r = requests.get('http://elk.example.com:9200/nessus_current/_search', data=json.dumps(data))
print(len(r.json()['hits']['hits']))

# the elasticsearch module version
es = elasticsearch.Elasticsearch(hosts='elk.example.com')
res = es.search(index="nessus_current", body={"query": {"match_all": {}}})
print(len(res['hits']['hits']))

在这两种情况下,结果都是10-与预期的60k相差甚远,查询的结果是有意义的(内容是我所期望的),只是其中只有几个。在

我取了这10次中的一次,用Sense查询它的_id来关闭循环。正如预期的那样,我们发现:

enter image description here

所以看起来这10个命中是整个索引的一个子集,为什么Python版本的调用中没有报告所有的元素?在


Tags: theimportjsonelk元素dataversionexample
1条回答
网友
1楼 · 发布于 2024-05-17 02:53:01

10 is the default size of the results returned by Elasticsearch。如果需要更多,请指定"size": 100。但是,请注意,不建议使用size返回所有文档,因为这样会导致集群崩溃。要返回所有结果,请使用scan&scroll。在

我认为应该是res['hits']['total']而不是{}来获得总点击数。在

相关问题 更多 >