Counting number of documents using Elasticsearch

后端 未结 5 1659
一生所求
一生所求 2021-01-31 07:13

If one wants to count the number of documents in an index (of Elasticsearch) then there are (at least?) two possibilities:

  • Direct count

5条回答
  •  孤独总比滥情好
    2021-01-31 07:55

    If _search must be used instead of _count, and you're on Elasticsearch 7.0+, setting size: 0 and track_total_hits: true will provide the same info as _count

    GET my-index/_search
    {
      "query": { "term": { "field": { "value": "xyz" } } },
      "size": 0,
      "track_total_hits": true
    }
    
    
    {
      "took" : 612,
      "timed_out" : false,
      "_shards" : {
        "total" : 629,
        "successful" : 629,
        "skipped" : 524,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 29349466,
          "relation" : "eq"
        },
        "max_score" : null,
        "hits" : [ ]
      }
    }
    

    See Elasticsearch 7.0 Breaking changes

提交回复
热议问题