繁体   English   中英

Python:如何获取弹性搜索查询的总命中数

[英]Python: How to get the total number of hits of a elasticsearch query

我很高兴使用ElasticsearchEnron电子邮件数据集做一些实验。 我做了一个查询以获得对我的实际问题不重要的东西。 我总共获得了 4 次点击,我想将此数字打印为:

The total number of hits is: 4

我的问题是:如何获得总点击次数?

这是我的查询:

s = Search(using=client, index="enron_test").query('range', date={'gte': query_date_1, 'lte': query_date_2, "format": "dd/MM/yyyy||dd/MM/yyyy"})

这是从Sense获取的查询结果:

{
   "took": 6,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 4,
       ...

您可以尝试以下操作:

es=Elasticsearch([{'host':'url','port':'9200','timeout':60}])
result=es.search(index='your index',doc_type='your doc_type',body={'query':your query})
print(result['hits']['total'])

这对我有用

es=Elasticsearch([{'host':'url','port':'9200','timeout':60}])
res = es.count(index='your index', doc_type='your doc_type', body={'query': your query })["count"]

如果它对您有帮助,以下是使用 Python 进行计数的好例子:

https://python.hotexamples.com/examples/elasticsearch/Elasticsearch/count/python-elasticsearch-count-method-examples.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM