Scripted Field with parameters

Hi,
I want to create a new field for every document. the script takes two parameters. and I want to do visualization in kibana(4.x) using this newly created scripted field. I have tried Scripted fields in settings tab of kibana, but there i don't have the power to use parameters.

Any help would be appreciated.

Thanks

Hi Shubham,

Maybe I don't understand what you're asking, but I don't think it's a good idea to create a new scripted field for every document. Kibana has to list all those fields in Discover and after a few hundred or thousand fields I think things will get very slow, or worse. Do you mean just one scripted field with a different value for each document?

Can you explain your use case a little more?

Thanks,
Lee

1 Like

Hi Lee,

I have a few 100 fields in my document. Some of them are numerical value fields. At any time i want to calculate the diff between these numerical field. I can do that by creating a new scripted field in kibana settings. There i have to write script as

doc['field1'].value - doc['field2'].value

I was wondering if these field1 and field2 can be parameterized because i have so many permutations of fields and don't want to write scripted field every time i have to calculate a new difference.

Thanks
Shubham

Hi Shubham,

You could potentially write a script that does the difference and store that in Elasticsearch. Then when you created a scripted field you would call that function and pass it the two fields instead of writing that script. But that doesn't really save you any effort. You're still creating a scripted field for each permutation.
https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html

The other thing is that I think you could use the script like doc['field1'].value - doc['field2'].value directly in a visualization without having to create a scripted field by using the Advanced JSON Input.

In my example below I made a Data Table visualization. I Split Rows by Terms and picked the count field. Then I clicked on the Advanced link and put this in the JSON Input;

{
"script": "doc['count'].value + 100",
"lang": "expression"
}

And I gave it a CustomLabel of count + 100.
That by itself worked, but I Split Rows again and added the count field again so you could see it's original value.

So you can get your diffs without having to create a scripted field. But I'm not sure if this is really any easier.

Thanks,
Lee