# Metric visual for number occurrences of a field and apply arithmetic

**URL:** <https://discuss.elastic.co/t/metric-visual-for-number-occurrences-of-a-field-and-apply-arithmetic/50699>\
**Category:** Kibana\
**Created:** [May 23, 2016, 11:19am UTC](https://discuss.elastic.co/t/metric-visual-for-number-occurrences-of-a-field-and-apply-arithmetic/50699 "2016-05-23T11:19:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremy\_Colton](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jeremy\_Colton](https://discuss.elastic.co/u/Jeremy_Colton)\
**Post date:** [May 23, 2016, 11:19am UTC](https://discuss.elastic.co/t/metric-visual-for-number-occurrences-of-a-field-and-apply-arithmetic/50699/1 "2016-05-23T11:19:26Z")

</div>

Hi,

I have read [https://www.elastic.co/blog/kibana-4-beta-3-now-more-filtery](https://www.elastic.co/blog/kibana-4-beta-3-now-more-filtery) and [https://qbox.io/blog/elasticsearch-scripting-aggregations-2](https://qbox.io/blog/elasticsearch-scripting-aggregations-2) but I cannot see how to use Kibana to fulfill my use-case:

I log an "event" field that contains various String values. I want to find the number of occurrences of a specific value "button clicked" and then multiply it by a monetary amount to calculate revenue and show this as a Metric visual.

Eg number of occurrences of event:"button clicked" \* 0.25

How can i do this?

Many thanks

---

<div class="post-metadata">

**Author:** ![spalger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spalger/32/14092_2.png) [@spalger](https://discuss.elastic.co/u/spalger)\
**Post date:** [May 23, 2016, 4:48pm UTC](https://discuss.elastic.co/t/metric-visual-for-number-occurrences-of-a-field-and-apply-arithmetic/50699/2 "2016-05-23T16:48:34Z")

</div>

One way you could do this is with a scripted field. That field might be `clickValueInUSD`, which has a field formatter for currency and a script value like `doc['event'].values.includes('button clicked') ? 0.25 : 0` and then create a metric aggregation on the sum of that field.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Colton](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jeremy\_Colton](https://discuss.elastic.co/u/Jeremy_Colton)\
**Post date:** [May 24, 2016, 9:14am UTC](https://discuss.elastic.co/t/metric-visual-for-number-occurrences-of-a-field-and-apply-arithmetic/50699/3 "2016-05-24T09:14:05Z")

</div>

Great!

I am running Kibana 4.5

I tried adding this scripted field exactly as you provided. Please see the attached screenshot.

![](https://us1.discourse-cdn.com/elastic/original/2X/e/e9a86acab2d0e2aaf1098095956f9e137155b28f.JPG)

When I run a metric 'sum' visual on this scripted field, Kibana complains with:

`Error: Request to Elasticsearch failed: {"error":{"root_cause":[{"type":"script_exception","reason":"Failed to parse expression: doc['event'].values.includes('button clicked') ? (0.25) : 0"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"logstash-2016.05.21","node":"y35NyWVvSMy_bDDwfUVj7w","reason":{"type":"script_exception","reason":"Failed to compile inline script [doc['event'].values.includes('button clicked') ? (0.25) : 0] using lang [expression]","caused_by":{"type":"script_exception","reason":"Failed to parse expression: doc['event'].values.includes('button clicked') ? (0.25) : 0","caused_by":{"type":"parse_exception","reason":"unexpected character ''' on line (1) position (29)","caused_by":{"type":"lexer_no_viable_alt_exception","reason":null}}}}}]}}`

Googling this hasn't revealed any solution other than to ask what scripting language is this? Do I need to enable something in Kibana to allow this script to run?

Many thanks.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [May 24, 2016, 9:09pm UTC](https://discuss.elastic.co/t/metric-visual-for-number-occurrences-of-a-field-and-apply-arithmetic/50699/4 "2016-05-24T21:09:16Z")

</div>

You will need to enable it in ES - [https://www.elastic.co/guide/en/elasticsearch/reference/2.3/modules-scripting.html#enable-dynamic-scripting](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/modules-scripting.html#enable-dynamic-scripting)

---

<div class="post-metadata">

**Author:** ![Jeremy\_Colton](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jeremy\_Colton](https://discuss.elastic.co/u/Jeremy_Colton)\
**Post date:** [May 26, 2016, 12:57pm UTC](https://discuss.elastic.co/t/metric-visual-for-number-occurrences-of-a-field-and-apply-arithmetic/50699/5 "2016-05-26T12:57:50Z")

</div>

Hi @warkolm, I read the link you provided and allegedly enabled scripting by adding to my /etc/elasticsearch/elasticsearch.yml:

```
script.inline: true
script.indexed: true

```

I restarted my ES server. I deleted/re-added my script but I still get the same parse error as I reported above when adding my Metric visual. How can I know that scripting is enabled in the elasticsearch logs?

---

<div class="post-metadata">

**Author:** ![Jeremy\_Colton](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jeremy\_Colton](https://discuss.elastic.co/u/Jeremy_Colton)\
**Post date:** [May 31, 2016, 8:18am UTC](https://discuss.elastic.co/t/metric-visual-for-number-occurrences-of-a-field-and-apply-arithmetic/50699/6 "2016-05-31T08:18:44Z")

</div>

Hi @warkolm, can you be more specific about how to enable scripting since the link you provided didn't help. Is there a specific language I need to enable as well? When I restart ES, should the logs say that scripting for a given language is enabled?

Many thanks

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [May 31, 2016, 10:37pm UTC](https://discuss.elastic.co/t/metric-visual-for-number-occurrences-of-a-field-and-apply-arithmetic/50699/7 "2016-05-31T22:37:53Z")

</div>

Are you still getting that same error?

---

<div class="post-metadata">

**Author:** ![Jeremy\_Colton](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jeremy\_Colton](https://discuss.elastic.co/u/Jeremy_Colton)\
**Post date:** [June 1, 2016, 7:52am UTC](https://discuss.elastic.co/t/metric-visual-for-number-occurrences-of-a-field-and-apply-arithmetic/50699/8 "2016-06-01T07:52:40Z")

</div>

Yes...the link that you provided didn't help.

Ok, I discovered the problem. Your syntax was wrong. You can't use a String in a Scripted Field, only numbers! I found this link that explains it:

> [@Calling groovy script from Kibana](https://discuss.elastic.co/t/calling-groovy-script-from-kibana/2542/3):
>
> Hi Darpan, You can use Groovy scripts within Kibana, but you have to use some of its advanced features to effectively make it do what you want. However, the way to do it depends on where the script needs to go. Can you write the query that you want to be processed? { "query" : { "filtered" : { "script" : { "script" : "your-script-name" } } } } If that's the case, then you can actually supply the inner-query portion in the Kibana search box: [image] { "filtere…

It's not simple to set up but the steps are there that explain how to do maths on a metric field.

Many 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 6, 2017, 1:51pm UTC](https://discuss.elastic.co/t/metric-visual-for-number-occurrences-of-a-field-and-apply-arithmetic/50699/9 "2017-07-06T13:51:49Z")

</div>


