# Calling groovy script from Kibana

**URL:** <https://discuss.elastic.co/t/calling-groovy-script-from-kibana/2542>\
**Category:** Kibana\
**Created:** [June 12, 2015, 6:43am UTC](https://discuss.elastic.co/t/calling-groovy-script-from-kibana/2542 "2015-06-12T06:43:23Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darpan205](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/darpan205/32/3548_2.png) [@Darpan205](https://discuss.elastic.co/u/Darpan205)\
**Post date:** [June 12, 2015, 6:43am UTC](https://discuss.elastic.co/t/calling-groovy-script-from-kibana/2542/1 "2015-06-12T06:43:23Z")

</div>

Hi,

I have a groovy script at $ES/config/scripts location. I wantto invoke that script while doing a search from Kibana & pass the parameters in the groovy script.  
Could you please guide me the set of steps needed in Kibana side to invoke this groovy script?

Regards,  
Darpan

---

<div class="post-metadata">

**Author:** ![Darpan205](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/darpan205/32/3548_2.png) [@Darpan205](https://discuss.elastic.co/u/Darpan205)\
**Post date:** [June 15, 2015, 9:39am UTC](https://discuss.elastic.co/t/calling-groovy-script-from-kibana/2542/2 "2015-06-15T09:39:48Z")

</div>

Guys any suggestions on this?

---

<div class="post-metadata">

**Author:** ![pickypg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pickypg/32/62409_2.png) [@pickypg](https://discuss.elastic.co/u/pickypg)\
**Post date:** [June 17, 2015, 5:04am UTC](https://discuss.elastic.co/t/calling-groovy-script-from-kibana/2542/3 "2015-06-17T05:04:01Z")

</div>

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:

![](https://us1.discourse-cdn.com/elastic/original/2X/7/7394716d0752381c32adb905bbbcca15e875707b.png)

```
{ "filtered" : { "filter" : { "script" : { "script" : "your-script-name" } } } }

```

* * *

If you want to use your script to customize an aggregation value, then you can do that too, but it takes a few extra steps.

1. Create a custom script field. The value of it is irrelevant, but this is limited because Kibana's script fields are currently only able to use Lucene Expressions, which can only work with numeric fields (not dates, nor strings). For this example, I created one named `custom.count`.

2. In the Visualization, select the script field.

3. Once selected, expand the "Advanced" link below the field.

4. Under the advanced link, enter your script into the JSON Input field as shown. Be sure to add the `"lang" : "groovy"` portion in order to override the script field's `"lang" : "expression"`.

5. From there, the `custom.count` field (as shown) is replaced with the JSON Input that you supply. Any field(s) that you supply, such as `"script"` or `"lang"` will override those supplied by Kibana. If you don't override it, then it gets combined with your input. This is why it's necessary to create a script field.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![Darpan205](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/darpan205/32/3548_2.png) [@Darpan205](https://discuss.elastic.co/u/Darpan205)\
**Post date:** [June 18, 2015, 6:57am UTC](https://discuss.elastic.co/t/calling-groovy-script-from-kibana/2542/4 "2015-06-18T06:57:20Z")

</div>

Hey Chris,

Thanks a lot... It definitely helped a lot.. 😀

Cheers!!

---

<div class="post-metadata">

**Author:** ![Darpan205](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/darpan205/32/3548_2.png) [@Darpan205](https://discuss.elastic.co/u/Darpan205)\
**Post date:** [June 30, 2015, 6:58am UTC](https://discuss.elastic.co/t/calling-groovy-script-from-kibana/2542/5 "2015-06-30T06:58:52Z")

</div>

Hi Chris,

I have one question, how can I use script to customize an aggregation value (In my case its count)??

Here's my scenario,  
I am having records for search engine, i.e. search queries and corresponding click queries if any, now I want to have (num of clicks/ num of searches) over the period of time. So I have written and stored groovy script in ES which does this.

So now in Kibana I have created a scripted field and calling my groovy script from JSON input as suggested by you. But Ii doesn't seems to be working on count aggregation.

What I want is Count should be calculated based on some script and not on the default total count.

Any ideas?

Thanks in advance. 😃

---

<div class="post-metadata">

**Author:** ![tbragin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tbragin/32/45166_2.png) [@tbragin](https://discuss.elastic.co/u/tbragin)\
**Post date:** [July 1, 2015, 4:47am UTC](https://discuss.elastic.co/t/calling-groovy-script-from-kibana/2542/6 "2015-07-01T04:47:27Z")

</div>

You might be looking for a [scripted metric aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html) here, which is not supported in Kibana at this time.

To operate on ratios (which is a common use case), I'd suggest a couple of approaches:

1. At the current time, see if you can index the ratio directly as a field, either at data ingest time using Logstash or after the fact using Watcher
2. Wait until Elasticsearch 2.0 and a subsequent Kibana release that adds support for a [bucket script aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/master/search-aggregations-pipeline-bucket-script-aggregation.html), which will be able to do arithmetic on results of aggregations.

---

<div class="post-metadata">

**Author:** ![Darpan205](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/darpan205/32/3548_2.png) [@Darpan205](https://discuss.elastic.co/u/Darpan205)\
**Post date:** [July 2, 2015, 6:38am UTC](https://discuss.elastic.co/t/calling-groovy-script-from-kibana/2542/7 "2015-07-02T06:38:48Z")

</div>

Thanks Tanya for your inputs. 😀

---

<div class="post-metadata">

**Author:** ![pickypg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pickypg/32/62409_2.png) [@pickypg](https://discuss.elastic.co/u/pickypg)\
**Post date:** [July 6, 2015, 10:45pm UTC](https://discuss.elastic.co/t/calling-groovy-script-from-kibana/2542/8 "2015-07-06T22:45:26Z")

</div>

If you want to "count" based on some script, then you inherently want to sum based on some script.

You can use the sum aggregation with your custom script and simply return `0` when you do not want a document to match, and whatever other value to represent the actual "count" that you're interested in finding.

---

<div class="post-metadata">

**Author:** ![Darpan205](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/darpan205/32/3548_2.png) [@Darpan205](https://discuss.elastic.co/u/Darpan205)\
**Post date:** [July 7, 2015, 4:40am UTC](https://discuss.elastic.co/t/calling-groovy-script-from-kibana/2542/9 "2015-07-07T04:40:33Z")

</div>

Hi Chris,

Can we write Aggregations in Groovy script and later call that script from Kibana to generate the response on dashboard?  
Can you provide me some tutorial links for Groovy script in ES/Kibana!?

Thanks for all your help.. 😃

Regards,  
Darpan.

---

<div class="post-metadata">

**Author:** ![msen](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@msen](https://discuss.elastic.co/u/msen)\
**Post date:** [October 29, 2015, 11:19pm UTC](https://discuss.elastic.co/t/calling-groovy-script-from-kibana/2542/10 "2015-10-29T23:19:46Z")

</div>

Any update on this, is there any concrete way of having calculations such as rates on total numbers based on the aggregations with or without scripts

---

<div class="post-metadata">

**Author:** ![EricK](https://avatars.discourse-cdn.com/v4/letter/e/bbce88/32.png) [@EricK](https://discuss.elastic.co/u/EricK)\
**Post date:** [June 5, 2016, 10:47pm UTC](https://discuss.elastic.co/t/calling-groovy-script-from-kibana/2542/11 "2016-06-05T22:47:54Z")

</div>

i have created a groovy scripted field (geo\_point) and indexed it. Works well and shows up in a search, but struggling to use the field to create a tile map.

The field does not list on available Geo coordinates as it is calculated on the fly. Any thoughts would help?

Alternatively, i can create the field in Kibana scripted field but this seems to still only support Lucene expressions (4.4.1). Can i enable groovy here?

Regards,

Eric

---

<div class="post-metadata">

**Author:** ![mrvincnezo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mrvincnezo/32/10829_2.png) [@mrvincnezo](https://discuss.elastic.co/u/mrvincnezo)\
**Post date:** [July 25, 2016, 1:34pm UTC](https://discuss.elastic.co/t/calling-groovy-script-from-kibana/2542/12 "2016-07-25T13:34:11Z")

</div>

You need to enable groovy in elasticsearch. You need to add the following to `elasticsearch.yml`

`script.engine.groovy.indexed.aggs: true`

---

<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:44pm UTC](https://discuss.elastic.co/t/calling-groovy-script-from-kibana/2542/13 "2017-07-06T13:44:22Z")

</div>


