# How to count the number of occurrence of fields with data in a given index of douments

**URL:** <https://discuss.elastic.co/t/how-to-count-the-number-of-occurrence-of-fields-with-data-in-a-given-index-of-douments/82570>\
**Category:** Elasticsearch\
**Created:** [April 17, 2017, 11:31am UTC](https://discuss.elastic.co/t/how-to-count-the-number-of-occurrence-of-fields-with-data-in-a-given-index-of-douments/82570 "2017-04-17T11:31:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandesh\_patil](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@sandesh\_patil](https://discuss.elastic.co/u/sandesh_patil)\
**Post date:** [April 17, 2017, 11:31am UTC](https://discuss.elastic.co/t/how-to-count-the-number-of-occurrence-of-fields-with-data-in-a-given-index-of-douments/82570/1 "2017-04-17T11:31:03Z")

</div>

I am trying to count the number of occurrence of fields with data in search bar so that after finding the count i can use in metric visualization.

These fields are with in array object (data) for ex Below is JSON for one document

{  
"\_index": "test",  
"\_type": "extracted",  
"\_id": "123",  
"\_source": {  
"document\_id": 12345,  
"document\_number": "56789",  
"data": [  
{  
"field1": null,  
"name": "field1",  
"value": null,  
"page\_number": null,  
"rect": null,  
"user\_id": null  
},  
{  
"field2": null,  
"name": "field2",  
"value": null,  
"page\_number": null,  
"rect": null,  
"user\_id": null  
}, {  
"field3": null,  
"name": "field3",  
"value": null,  
"page\_number": null,  
"rect": null,  
"user\_id": null  
}]  
}

These array object (data) present in all document ids. Suppose i have 10 documents and with in that 10 document if "field1" appearing 3 times and "field2" appearing 5 times and "field3" appearing 6 times then i want result Total fields count = 14 for metric visualization. Please note i want to use only metric visualization. How to achieve this. Tried query but getting single field count in search bar of kibana like below

{  
"query": {  
"term": {  
"[data.name](http://data.name)": "field1"  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![jkuang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jkuang/32/72637_2.png) [@jkuang](https://discuss.elastic.co/u/jkuang)\
**Post date:** [April 18, 2017, 11:14pm UTC](https://discuss.elastic.co/t/how-to-count-the-number-of-occurrence-of-fields-with-data-in-a-given-index-of-douments/82570/2 "2017-04-18T23:14:11Z")

</div>

Try using a field term aggregation.

```auto
{ 
"aggs": { 
"field": { 
"terms": { "field": "field" }
} 
}, 
"size": 0 
}

```

---

<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:** [May 16, 2017, 11:26pm UTC](https://discuss.elastic.co/t/how-to-count-the-number-of-occurrence-of-fields-with-data-in-a-given-index-of-douments/82570/3 "2017-05-16T23:26:42Z")

</div>

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