If one wants to count the number of documents in an index (of Elasticsearch) then there are (at least?) two possibilities:
Direct count
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