# Query for counting docs in all indices

**URL:** <https://discuss.elastic.co/t/query-for-counting-docs-in-all-indices/149236>\
**Category:** Elasticsearch\
**Created:** [September 20, 2018, 6:38am UTC](https://discuss.elastic.co/t/query-for-counting-docs-in-all-indices/149236 "2018-09-20T06:38:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![kumpelblase](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kumpelblase/32/35709_2.png) [@kumpelblase](https://discuss.elastic.co/u/kumpelblase)\
**Post date:** [September 20, 2018, 6:38am UTC](https://discuss.elastic.co/t/query-for-counting-docs-in-all-indices/149236/1 "2018-09-20T06:38:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![klof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klof/32/31590_2.png) [@klof](https://discuss.elastic.co/u/klof)\
**Post date:** [September 20, 2018, 6:52am UTC](https://discuss.elastic.co/t/query-for-counting-docs-in-all-indices/149236/2 "2018-09-20T06:52:18Z")

</div>

You could use this query instead :

`GET _cat/indices?v`

---

<div class="post-metadata">

**Author:** ![kumpelblase](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kumpelblase/32/35709_2.png) [@kumpelblase](https://discuss.elastic.co/u/kumpelblase)\
**Post date:** [September 20, 2018, 7:02am UTC](https://discuss.elastic.co/t/query-for-counting-docs-in-all-indices/149236/3 "2018-09-20T07:02:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![klof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klof/32/31590_2.png) [@klof](https://discuss.elastic.co/u/klof)\
**Post date:** [September 20, 2018, 7:08am UTC](https://discuss.elastic.co/t/query-for-counting-docs-in-all-indices/149236/4 "2018-09-20T07:08:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kumpelblase](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kumpelblase/32/35709_2.png) [@kumpelblase](https://discuss.elastic.co/u/kumpelblase)\
**Post date:** [September 20, 2018, 7:10am UTC](https://discuss.elastic.co/t/query-for-counting-docs-in-all-indices/149236/5 "2018-09-20T07:10:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kumpelblase](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kumpelblase/32/35709_2.png) [@kumpelblase](https://discuss.elastic.co/u/kumpelblase)\
**Post date:** [September 20, 2018, 7:35am UTC](https://discuss.elastic.co/t/query-for-counting-docs-in-all-indices/149236/6 "2018-09-20T07:35:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![klof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klof/32/31590_2.png) [@klof](https://discuss.elastic.co/u/klof)\
**Post date:** [September 20, 2018, 7:48am UTC](https://discuss.elastic.co/t/query-for-counting-docs-in-all-indices/149236/7 "2018-09-20T07:48:03Z")

</div>

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"
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![Hari\_Prasad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hari_prasad/32/56784_2.png) [@Hari\_Prasad](https://discuss.elastic.co/u/Hari_Prasad)\
**Post date:** [September 20, 2018, 9:56am UTC](https://discuss.elastic.co/t/query-for-counting-docs-in-all-indices/149236/8 "2018-09-20T09:56:23Z")

</div>

@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  
}  
}

---

<div class="post-metadata">

**Author:** ![kumpelblase](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kumpelblase/32/35709_2.png) [@kumpelblase](https://discuss.elastic.co/u/kumpelblase)\
**Post date:** [September 20, 2018, 10:59am UTC](https://discuss.elastic.co/t/query-for-counting-docs-in-all-indices/149236/9 "2018-09-20T10:59:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 18, 2018, 10:59am UTC](https://discuss.elastic.co/t/query-for-counting-docs-in-all-indices/149236/10 "2018-10-18T10:59:27Z")

</div>

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