0

What is the purpose of _count in elastic search? I know that I can use endpoint _search to get the data in elastic search.

Thanks

Honddoe
  • 51
  • 11
  • When you are only interested in count of matching documents and not `hits` i.e. matching documents themselves, you can use _count. – Nishant Nov 29 '18 at 04:20

1 Answers1

1

The purpose of the _count API is simply to get the number of matches for a given query.

It takes the same input as the _search API but without returning any hits, just a count of matches.

The Search API with size=0 would return this:

POST index/_search?size=0
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 10931164,
    "max_score" : 0.0,
    "hits" : [ ]
  }
}

While the Count API would return this:

POST index/_count
{
  "count" : 10931164,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • I have another question, why does it use POST verb to get the data? From what I know I can use GET when searching data in elasticsearch. – Honddoe Nov 29 '18 at 04:46
  • The answer is here: https://stackoverflow.com/a/34796014/4604579 (both are supported but prefer POST when you're sending some payload) – Val Nov 29 '18 at 04:55