【问题标题】:Python: How to get the total number of hits of a elasticsearch queryPython:如何获取弹性搜索查询的总命中数
【发布时间】:2017-12-02 05:01:39
【问题描述】:

我在使用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,
       ...

【问题讨论】:

  • s["hits"]["total"]
  • 如果我写下print(["hits"]["total"]),它将无法返回TypeError: list indices must be integers, not str。如果我用s 写下print(s["hits"]["total"]),它会返回<elasticsearch_dsl.search.Search object at 0x7f87f6698d50>

标签: python json elasticsearch


【解决方案1】:

您可以尝试以下方法:

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'])

【讨论】:

    【解决方案2】:

    这对我有用

    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

    【讨论】:

      猜你喜欢
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      相关资源
      最近更新 更多