# Scripted Field with parameters

**URL:** <https://discuss.elastic.co/t/scripted-field-with-parameters/58686>\
**Category:** Kibana\
**Created:** [August 23, 2016, 1:27pm UTC](https://discuss.elastic.co/t/scripted-field-with-parameters/58686 "2016-08-23T13:27:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shubham\_Agrawal](https://avatars.discourse-cdn.com/v4/letter/s/9de0a6/32.png) [@Shubham\_Agrawal](https://discuss.elastic.co/u/Shubham_Agrawal)\
**Post date:** [August 23, 2016, 1:27pm UTC](https://discuss.elastic.co/t/scripted-field-with-parameters/58686/1 "2016-08-23T13:27:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [August 24, 2016, 7:02pm UTC](https://discuss.elastic.co/t/scripted-field-with-parameters/58686/2 "2016-08-24T19:02:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Shubham\_Agrawal](https://avatars.discourse-cdn.com/v4/letter/s/9de0a6/32.png) [@Shubham\_Agrawal](https://discuss.elastic.co/u/Shubham_Agrawal)\
**Post date:** [August 25, 2016, 10:14am UTC](https://discuss.elastic.co/t/scripted-field-with-parameters/58686/3 "2016-08-25T10:14:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [August 25, 2016, 2:20pm UTC](https://discuss.elastic.co/t/scripted-field-with-parameters/58686/4 "2016-08-25T14:20:57Z")

</div>

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](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;

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

 ![](https://us1.discourse-cdn.com/elastic/original/2X/f/feb59af6f2eb671cdc2f7c543ef04d208f090900.png)

Thanks,  
Lee

---

<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:40pm UTC](https://discuss.elastic.co/t/scripted-field-with-parameters/58686/5 "2017-07-06T13:40:18Z")

</div>


