# Scripting field in Kibana and use it in loop

**URL:** <https://discuss.elastic.co/t/scripting-field-in-kibana-and-use-it-in-loop/148043>\
**Category:** Kibana\
**Created:** [September 11, 2018, 5:07am UTC](https://discuss.elastic.co/t/scripting-field-in-kibana-and-use-it-in-loop/148043 "2018-09-11T05:07:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![shrikantgulia](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@shrikantgulia](https://discuss.elastic.co/u/shrikantgulia)\
**Post date:** [September 11, 2018, 5:07am UTC](https://discuss.elastic.co/t/scripting-field-in-kibana-and-use-it-in-loop/148043/1 "2018-09-11T05:07:55Z")

</div>

can anyone help me over the scripting field. I am getting an error on running this

def x = doc['index\_total'].value;  
def y = doc['index\_time\_in\_millis'].value;  
{  
if ( x \> 0) {  
def z = y / x;  
return z  
}  
else {  
def z = 5 \* 0;  
return z  
}  
}  
if ( z \> 0) {  
def l = 0;  
return l  
}  
else {  
def f = 1;  
return f  
}  
by creating the "indexing\_latency" field and using it in the in loop but this is also throwing an error.

A help would be really appreciated

Best Regards  
Shrikant

Best Regards  
Shrikant

---

<div class="post-metadata">

**Author:** ![shrikantgulia](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@shrikantgulia](https://discuss.elastic.co/u/shrikantgulia)\
**Post date:** [September 11, 2018, 6:03am UTC](https://discuss.elastic.co/t/scripting-field-in-kibana-and-use-it-in-loop/148043/2 "2018-09-11T06:03:00Z")

</div>

@magnusbaeck please guide me

Best Regards  
Shrikant

---

<div class="post-metadata">

**Author:** ![shrikantgulia](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@shrikantgulia](https://discuss.elastic.co/u/shrikantgulia)\
**Post date:** [September 11, 2018, 6:29am UTC](https://discuss.elastic.co/t/scripting-field-in-kibana-and-use-it-in-loop/148043/3 "2018-09-11T06:29:21Z")

</div>

@magnusbaeck Please help me I am stuck here

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [September 11, 2018, 6:44am UTC](https://discuss.elastic.co/t/scripting-field-in-kibana-and-use-it-in-loop/148043/4 "2018-09-11T06:44:20Z")

</div>

Please do not ping people not already active in the thread and also be patient. This forum is manned by volunteers, so there is no SLA. If you have not received any response within a few days it is generally fine to bump the issue.

When I read your issue, it is not clear exactly what you are trying to do. Can you please describe further? What is the loop you want too use it in? What does the error look like?

---

<div class="post-metadata">

**Author:** ![shrikantgulia](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@shrikantgulia](https://discuss.elastic.co/u/shrikantgulia)\
**Post date:** [September 11, 2018, 6:50am UTC](https://discuss.elastic.co/t/scripting-field-in-kibana-and-use-it-in-loop/148043/5 "2018-09-11T06:50:00Z")

</div>

i am sorry @Christian_Dahlqvist  
I am facing a issue  
i want to create a script and the script is  
def x = doc['index\_total'].value;  
def y = doc['index\_time\_in\_millis'].value;  
{  
if ( x \>; 0) {  
def z = y / x;  
return z  
}  
else {  
def z = 5 \* 0;  
return z  
}  
}  
if ( z \> 0) {  
def l = 0;  
return l  
}  
else {  
def f = 1;  
return f  
}  
first i want to extract the value of Z after that i want to apply condition on that value i.e (z)

like if (z\>0.3 && z\<0.7) {  
def l = 3;  
retunr l  
}  
else {  
def m = 0;  
return m  
}

please help me

Best Regards  
Shrikant

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [September 11, 2018, 6:58am UTC](https://discuss.elastic.co/t/scripting-field-in-kibana-and-use-it-in-loop/148043/6 "2018-09-11T06:58:16Z")

</div>

Please format your code sample properly as it is hard to read. It looks like you are trying to do something like this:

```auto
def x = doc['index_total'].value;
def y = doc['index_time_in_millis'].value;

if ( x >; 0) {
  def z = y / x;
  return z
} else {
  def z = 5 * 0;
  return z
}

if ( z > 0) {
  def l = 0;
  return l
} else {
  def f = 1;
  return f
}

```

As you are calling `return z` the following if-else block will never be reached. If you describe what you expect your script to do it may be easier to help.

---

<div class="post-metadata">

**Author:** ![shrikantgulia](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@shrikantgulia](https://discuss.elastic.co/u/shrikantgulia)\
**Post date:** [September 11, 2018, 7:04am UTC](https://discuss.elastic.co/t/scripting-field-in-kibana-and-use-it-in-loop/148043/7 "2018-09-11T07:04:18Z")

</div>

@Christian_Dahlqvist  
Thankyou so much for the response

I want to first find the indexing latency which is (index\_total / index\_time\_in\_millis) and define it as Z.  
Now I want that if Z is between (0.3 && 0.7) I want the final output (L) to be 1. and if Z is greater than (0.7 && less than 0.3) I want L to be 0.

Thanks & Regards  
Shrikant

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [September 11, 2018, 7:06am UTC](https://discuss.elastic.co/t/scripting-field-in-kibana-and-use-it-in-loop/148043/8 "2018-09-11T07:06:11Z")

</div>

Then remove the `return z` clauses as they cause the script to terminate.

---

<div class="post-metadata">

**Author:** ![shrikantgulia](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@shrikantgulia](https://discuss.elastic.co/u/shrikantgulia)\
**Post date:** [September 11, 2018, 7:11am UTC](https://discuss.elastic.co/t/scripting-field-in-kibana-and-use-it-in-loop/148043/9 "2018-09-11T07:11:40Z")

</div>

@Christian_Dahlqvist  
Still facing the same issue

def x = doc['index\_total'].value;  
def y = doc['index\_time\_in\_millis'].value;  
for ( x \> 0) {  
def z = y / x;  
}  
if (z \> 0.3);  
def t =1;  
return t  
}  
else {  
def m = 0;  
return m  
}

and i also tried this  
def x = doc['index\_total'].value;  
def y = doc['index\_time\_in\_millis'].value;  
if ( x \> 0) {  
def z = y / x  
}  
else {  
def z = 5\*0  
}  
if (z \> 0.3) {  
def m = 1;  
return m  
}  
else {  
def m = 0;  
return m  
}

Thanks & Regards  
Shrikant

---

<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:** [October 9, 2018, 7:11am UTC](https://discuss.elastic.co/t/scripting-field-in-kibana-and-use-it-in-loop/148043/10 "2018-10-09T07:11:48Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
