Query for counting docs in all indices


#1

I need a query that counts all docs in all indices.
I have found and read the _count API, but GET /_all/_count for example only gives the number of documents in all indices together. If I substitude _all with an index name I get the numbers of docs for that index, but I need all numbers of all indices in one query.


#2

You could use this query instead :

GET _cat/indices?v


#3

Thanks, that solution would probably work, but is there any way I can get that result as JSON?


#4

If you need a JSON object, you could also use :

GET _stats

Then parse the object to get, for all your indices, the doc count.


#5

Much thanks, thats perfect.
I also found that GET _cat/indices?v&h=docs.count would also work.


#6

After some digging I found an even better solution.

GET /_search
{
    "query" : {
      "bool": {
        "should": [{
          "match_phrase": {
            "_index": "index-*"
          }
        }]
      }
    },
    "size": 0,
    "aggs" : {
        "indices" : {
            "terms" : { "field" : "_index" }
        }
    }
}

Which returns the doc_count for all indices which match the boolquery.


#7

All right, great,

Just be aware that this solution works only for your indices (assuming the indices name starts with index-*).
One more thing, you're querying all the indices of your cluster, depends on the number of indices and their sizes, this query can be expensive.

So you can just query the indices with this pattern (the query clause is not needed) :

GET index-*/_search
{
  "size": 0,
  "aggs": {
    "indices": {
      "terms": {
        "field": "_index"
      }
    }
  }
}

(Hari Prasad) #8

@kumpelblase: try GET _count ( without _all or index name). This will give you output like

{
"count": 234,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
}
}


#9

This only gives to count of all documents in all indices combined.
The accepted answer which @klof gave is already perfect.


(system) #10

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.