# Scripted field in kibana (count)

**URL:** <https://discuss.elastic.co/t/scripted-field-in-kibana-count/140647>\
**Category:** Kibana\
**Created:** [July 19, 2018, 3:00am UTC](https://discuss.elastic.co/t/scripted-field-in-kibana-count/140647 "2018-07-19T03:00:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gokul\_15](https://avatars.discourse-cdn.com/v4/letter/g/5f9b8f/32.png) [@Gokul\_15](https://discuss.elastic.co/u/Gokul_15)\
**Post date:** [July 19, 2018, 3:00am UTC](https://discuss.elastic.co/t/scripted-field-in-kibana-count/140647/1 "2018-07-19T03:00:30Z")

</div>

how can i find the count of the string with some condition in scripted field ???  
eg:doc['gender'].count.value=="male"  
i know this is wrong i need something concept like this

---

<div class="post-metadata">

**Author:** ![Gokul\_15](https://avatars.discourse-cdn.com/v4/letter/g/5f9b8f/32.png) [@Gokul\_15](https://discuss.elastic.co/u/Gokul_15)\
**Post date:** [July 19, 2018, 5:21am UTC](https://discuss.elastic.co/t/scripted-field-in-kibana-count/140647/2 "2018-07-19T05:21:27Z")

</div>

anybody??

---

<div class="post-metadata">

**Author:** ![Gokul\_15](https://avatars.discourse-cdn.com/v4/letter/g/5f9b8f/32.png) [@Gokul\_15](https://discuss.elastic.co/u/Gokul_15)\
**Post date:** [July 19, 2018, 7:21am UTC](https://discuss.elastic.co/t/scripted-field-in-kibana-count/140647/3 "2018-07-19T07:21:57Z")

</div>

(doc['gender'].value=="male"){ int count=0; count=count+1; return count; } return false;  
am new to kibana,,,t shows error like this  
No results match your search criteria

---

<div class="post-metadata">

**Author:** ![jbudz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jbudz/32/45922_2.png) [@jbudz](https://discuss.elastic.co/u/jbudz)\
**Post date:** [July 19, 2018, 9:50pm UTC](https://discuss.elastic.co/t/scripted-field-in-kibana-count/140647/4 "2018-07-19T21:50:14Z")

</div>

Scripted fields only have access to the one document at a time, so you can provide a conditional check for certain fields but can't aggregate across documents.

If you want the total count of males you can do a data table visualization with a term or filter aggregation on gender. Does that help?

---

<div class="post-metadata">

**Author:** ![Gokul\_15](https://avatars.discourse-cdn.com/v4/letter/g/5f9b8f/32.png) [@Gokul\_15](https://discuss.elastic.co/u/Gokul_15)\
**Post date:** [July 20, 2018, 4:28am UTC](https://discuss.elastic.co/t/scripted-field-in-kibana-count/140647/5 "2018-07-20T04:28:31Z")

</div>

actually am looking to do percentage between count of male and female...  
eg:male 50% and female 50%

---

<div class="post-metadata">

**Author:** ![jbudz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jbudz/32/45922_2.png) [@jbudz](https://discuss.elastic.co/u/jbudz)\
**Post date:** [July 20, 2018, 8:20pm UTC](https://discuss.elastic.co/t/scripted-field-in-kibana-count/140647/6 "2018-07-20T20:20:33Z")

</div>

I see. Percentages in the data table aren't supported, but this does work with a pie chart.

If you want it in the data table you can workaround this by creating two scripted field and then using the average metric aggregation.

percent\_male

```auto
doc["gender"] == "male" ? 1 : 0

```

percent\_female

```auto
doc["gender"] == "female" ? 1 : 0

```

Make sure to select the percent field formatter too.

---

<div class="post-metadata">

**Author:** ![robert.j.foreman](https://avatars.discourse-cdn.com/v4/letter/r/9fc348/32.png) [@robert.j.foreman](https://discuss.elastic.co/u/robert.j.foreman)\
**Post date:** [July 21, 2018, 1:36pm UTC](https://discuss.elastic.co/t/scripted-field-in-kibana-count/140647/7 "2018-07-21T13:36:51Z")

</div>

Depending on your situation, you may want to look at a plugin like datasweet, then you can do term query aggregations, and only do math on the client, rather than burden the server with a script for each doc. If that helps

---

<div class="post-metadata">

**Author:** ![Gokul\_15](https://avatars.discourse-cdn.com/v4/letter/g/5f9b8f/32.png) [@Gokul\_15](https://discuss.elastic.co/u/Gokul_15)\
**Post date:** [July 23, 2018, 4:37am UTC](https://discuss.elastic.co/t/scripted-field-in-kibana-count/140647/8 "2018-07-23T04:37:14Z")

</div>

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:** [August 20, 2018, 4:37am UTC](https://discuss.elastic.co/t/scripted-field-in-kibana-count/140647/9 "2018-08-20T04:37:15Z")

</div>

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