Calling groovy script from Kibana

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

Guys any suggestions on this?

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:

{ "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.

    You can see the results of your efforts by clicking on the arrow directly below the visualization. From there, you can select the "Request" button/tab and see the request that is sent to Elasticsearch. You can use that to understand how your request is actually being sent. Note: I actually tested it with a dynamic script, which I have enabled locally for testing; using dynamic scripts is absolutely not recommended for production.

Hope that helps.

5 Likes

Hey Chris,

Thanks a lot... It definitely helped a lot.. :grinning:

Cheers!!

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. :smiley:

You might be looking for a scripted metric aggregation 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, which will be able to do arithmetic on results of aggregations.
1 Like

Thanks Tanya for your inputs. :grinning:

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.

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.. :smiley:

Regards,
Darpan.

1 Like

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

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

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

script.engine.groovy.indexed.aggs: true