# Scripted field to find percent of documents to be used in a metric

**URL:** <https://discuss.elastic.co/t/scripted-field-to-find-percent-of-documents-to-be-used-in-a-metric/246338>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [August 25, 2020, 5:55pm UTC](https://discuss.elastic.co/t/scripted-field-to-find-percent-of-documents-to-be-used-in-a-metric/246338 "2020-08-25T17:55:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jc034240](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@jc034240](https://discuss.elastic.co/u/jc034240)\
**Post date:** [August 25, 2020, 5:55pm UTC](https://discuss.elastic.co/t/scripted-field-to-find-percent-of-documents-to-be-used-in-a-metric/246338/1 "2020-08-25T17:55:01Z")

</div>

I made a scripted field in Kibana using the below which syntactically is fine but doesn't function properly.  
I want to take the amount of documents with "Execution", divide that hit count by 10, then multiply that number by 100 to get a percent.

```auto
if (doc.containsKey('Execution') ) {
    return Integer.parseInt(doc['tactic.keyword'].sum / 10 * 100)
}

```

---

<div class="post-metadata">

**Author:** ![mattkime](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattkime/32/43522_2.png) [@mattkime](https://discuss.elastic.co/u/mattkime)\
**Post date:** [August 25, 2020, 9:56pm UTC](https://discuss.elastic.co/t/scripted-field-to-find-percent-of-documents-to-be-used-in-a-metric/246338/2 "2020-08-25T21:56:34Z")

</div>

When using scripted fields from kibana, the scripts only have access to document level data.

However, you might find this helpful - [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html)

---

<div class="post-metadata">

**Author:** ![jc034240](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@jc034240](https://discuss.elastic.co/u/jc034240)\
**Post date:** [August 26, 2020, 4:25pm UTC](https://discuss.elastic.co/t/scripted-field-to-find-percent-of-documents-to-be-used-in-a-metric/246338/3 "2020-08-26T16:25:45Z")

</div>

Excellent, thanks Matthew

---

<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:** [September 23, 2020, 4:25pm UTC](https://discuss.elastic.co/t/scripted-field-to-find-percent-of-documents-to-be-used-in-a-metric/246338/4 "2020-09-23T16:25:52Z")

</div>

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