# Count number of elements in an array

**URL:** <https://discuss.elastic.co/t/count-number-of-elements-in-an-array/134568>\
**Category:** Elasticsearch\
**Created:** [June 5, 2018, 8:49am UTC](https://discuss.elastic.co/t/count-number-of-elements-in-an-array/134568 "2018-06-05T08:49:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![javidr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javidr/32/136982_2.png) [@javidr](https://discuss.elastic.co/u/javidr)\
**Post date:** [June 5, 2018, 8:49am UTC](https://discuss.elastic.co/t/count-number-of-elements-in-an-array/134568/1 "2018-06-05T08:49:04Z")

</div>

Hi

I have a json that contains an array on it. The structure is something like this:

{date:xxx, stock: [{name: xxx : status:OK}, {name: yyy : status:OK}, {name: zzz : status:KO}, {name: xxz : status:OK},]}

I would like to visualise a bar chart that contains in the Y axis the number of status:OK i have in the array

How can i count the number of occurrences in an array based on a condition? In that example, If i filter by stock.status=OK, it just display a bar with the size 1 instead of 3.

Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 5, 2018, 9:30am UTC](https://discuss.elastic.co/t/count-number-of-elements-in-an-array/134568/2 "2018-06-05T09:30:57Z")

</div>

GThe best thing is to solve that at index time.  
Otherwise you can use a scripted field with a painless script that computed this at search time.

---

<div class="post-metadata">

**Author:** ![javidr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javidr/32/136982_2.png) [@javidr](https://discuss.elastic.co/u/javidr)\
**Post date:** [June 5, 2018, 2:50pm UTC](https://discuss.elastic.co/t/count-number-of-elements-in-an-array/134568/3 "2018-06-05T14:50:33Z")

</div>

Thanks David. I have it now

The problem im facing now is that it doesnt display a bar when the count is 0. I assume there is an option somewhere, so working on it

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:** [July 3, 2018, 2:50pm UTC](https://discuss.elastic.co/t/count-number-of-elements-in-an-array/134568/4 "2018-07-03T14:50:45Z")

</div>

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