# Count Key Occurrences in Index

**URL:** <https://discuss.elastic.co/t/count-key-occurrences-in-index/112477>\
**Category:** Elasticsearch\
**Created:** [December 19, 2017, 4:49pm UTC](https://discuss.elastic.co/t/count-key-occurrences-in-index/112477 "2017-12-19T16:49:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jong99](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jong99](https://discuss.elastic.co/u/jong99)\
**Post date:** [December 19, 2017, 4:49pm UTC](https://discuss.elastic.co/t/count-key-occurrences-in-index/112477/1 "2017-12-19T16:49:47Z")

</div>

Is there a simple way to count the number of occurrences of keys in documents in an index?

For example:

**Documents:**  
`{"a":"blah","b":2}`  
`{"a":"foo","c":4,"d":"baz"}`  
`{"a":"bar","b":2,"c":1}`

**Result:**  
`{"a":3, "b":2, "c":2, "d":1}`

Or do I need to run an Exists query for each key?

Thanks!

---

<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:** [January 16, 2018, 4:49pm UTC](https://discuss.elastic.co/t/count-key-occurrences-in-index/112477/2 "2018-01-16T16:49:52Z")

</div>

Closed on January 16, 2018, 4:49pm UTC

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