# How to get the count of total non-zero documents from an index?

**URL:** <https://discuss.elastic.co/t/how-to-get-the-count-of-total-non-zero-documents-from-an-index/132523>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [May 18, 2018, 5:37pm UTC](https://discuss.elastic.co/t/how-to-get-the-count-of-total-non-zero-documents-from-an-index/132523 "2018-05-18T17:37:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![saikrishnagaddipati](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@saikrishnagaddipati](https://discuss.elastic.co/u/saikrishnagaddipati)\
**Post date:** [May 18, 2018, 5:37pm UTC](https://discuss.elastic.co/t/how-to-get-the-count-of-total-non-zero-documents-from-an-index/132523/1 "2018-05-18T17:37:24Z")

</div>

I am retrieving data from an index "dummyindex" and send the total count of non-zero documents in the field downtime\_depth to another index. I am not able to get the value using value\_count using below script. any suggestions?

```
POST _xpack/watcher/watch/downtimeexample
{
 "trigger": {
   "schedule": {
     "interval": "1m"
   }
 },
 "input": {
   "search": {
     "request": {
       "indices": [
         "dummyindex"
       ],
       "body": {
         "size": 0,
           "query": {
             "range": {
               "@timestamp": {
                 "gte": "now-14d/d"
               }
             }
           },
           "aggs": {
             "hostname_agg": {
               "terms": {
                 "field": "hostname.keyword",
                 "size": 10
               },
               "aggs": {
                 "state_agg": {
                   "terms": {
                     "field": "server_state.keyword",
                     "size": 10
                   }
                 },
                 "downtime_agg": {
                   "terms": {
                     "field": "downtime_depth.keyword",
                     "include": "0", 
                     "size": 10
                   }
                 },
                 "downtime_nonzeroagg": {
                   "terms": {
                     "field": "downtime_depth.keyword",
                     "exclude": "0", 
                     "size": 10
                   }
                 },
                 "total_terms": {
                   "value_count": {
                     "field": "downtime_depth.keyword"
                   }
                 }
               }
             }
           }
       }
     }
   }
 },
 "condition": {
  "compare": {
   "ctx.payload.hits.total": {
    "gte": 1
   }
  }
 },
 "transform": {
  "script": {
   "source": " return ['nonzero_doc_count' : ctx.payload.aggregations.total_terms.value, '@timestamp': ctx.trigger.scheduled_time]"
  }
 },   
 "actions": {
  "my_index_action": {
   "index": {
    "index": "summary-index-host-availability",
    "doc_type": "mytype"
   }
  }
 }
}
```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [June 4, 2018, 12:46pm UTC](https://discuss.elastic.co/t/how-to-get-the-count-of-total-non-zero-documents-from-an-index/132523/2 "2018-06-04T12:46:32Z")

</div>

please provide the output of the [Execute Watch API](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/watcher-api-execute-watch.html) in order to make debugging easier. Otherwise everything else is just guesswork!

Thank you!

---

<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:** [July 2, 2018, 12:46pm UTC](https://discuss.elastic.co/t/how-to-get-the-count-of-total-non-zero-documents-from-an-index/132523/3 "2018-07-02T12:46:36Z")

</div>

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