Search code examples
elasticsearch

Counting number of documents using Elasticsearch


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

  • Direct count

    POST my_index/_count

    should return the number of documents in my_index.

  • Using search

    Here one can use the count as the search_type or some other type. In either of the cases the total count can be extracted from the field ['hits']['total']

My questions are:

  • what is the difference between the different approaches? Which one should I prefer?

  • I raise this question because I'm experiencing different results depending on the chosen method. I'm now in the process of debugging the issue, and this question popped up.


Solution

  • Probably _count is a bit faster since it doesn't have to execute a full query with ranking and result fetching and can simply return the size.

    It would be interesting to know a bit more about how you manage to get different results though. For that I need more information like what exact queries you are sending and if any indexing is going on on the index.

    But suppose that you do the following

    1. index some documents
    2. refresh the index

    _search and _count (with a match all query) should return the same total. If not, that'd be very weird.