# Problem in script field

**URL:** <https://discuss.elastic.co/t/problem-in-script-field/121100>\
**Category:** Kibana\
**Created:** [February 22, 2018, 5:25pm UTC](https://discuss.elastic.co/t/problem-in-script-field/121100 "2018-02-22T17:25:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nabilberd](https://avatars.discourse-cdn.com/v4/letter/n/5daacb/32.png) [@nabilberd](https://discuss.elastic.co/u/nabilberd)\
**Post date:** [February 22, 2018, 5:25pm UTC](https://discuss.elastic.co/t/problem-in-script-field/121100/1 "2018-02-22T17:25:35Z")

</div>

Hello,

I have a problem with script field in kibana , by the way i want count number of attribute but i must test it before calculate then do another count for another attribute and finally sum them.

Thank you.

---

<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:** [February 22, 2018, 6:13pm UTC](https://discuss.elastic.co/t/problem-in-script-field/121100/2 "2018-02-22T18:13:06Z")

</div>

You haven't shared a lot of details but I can start the conversation off by telling you that scripted fields can only work on individual documents, not groups of documents.

So if you have documents that have `price` and `tax` fields, you can create a scripted field for `total_sale` that adds those 2 fields together on each document. But you can't get the sum of `price` over multiple documents in a scripted field.

---

<div class="post-metadata">

**Author:** ![nabilberd](https://avatars.discourse-cdn.com/v4/letter/n/5daacb/32.png) [@nabilberd](https://discuss.elastic.co/u/nabilberd)\
**Post date:** [February 22, 2018, 6:32pm UTC](https://discuss.elastic.co/t/problem-in-script-field/121100/3 "2018-02-22T18:32:01Z")

</div>

Thank you for your reply.

First i have in Elasticsearch lot of object and i want parse this object to count field "price" then "tax", and sum this fields .

I tried this script but dont work :

int m=0;  
int n=0;  
float T;

for(el in doc['\_source'].value){  
if( doc('\_source.currentStatusName').value == 'AVOIDABLE'){ m++; }  
else if (doc('\_source.currentStatusName').value == 'NON\_AVOIDABLE'){ n++; }  
else { return 0;}  
}

T= m/(m+n);  
return T;

---

<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:** [February 22, 2018, 7:01pm UTC](https://discuss.elastic.co/t/problem-in-script-field/121100/4 "2018-02-22T19:01:30Z")

</div>

Like I said in my previous comment, that won't work because scripted fields only operate on each individual document. The scope of your m, n, and T variables are each individual document.

You can create a scripted field that has either 1 or 0 for each document based on that currentStatusName field. And then you can do some aggregation in a visualization based on that where you could have the sum of some other field.

For example, I create a scripted field with this script;

```auto
if (doc['machine.os.raw'].value == "win xp") {
  return 1
} else {
return 0
}

```

(if you set your scripted field to be a String instead of a Number you could return 'AVOIDABLE' or 'NON\_AVOIDABLE' instead)

I can see it works in the Discover tab;  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/1/b/1b184de755576c559165bcbc98a19d1bceb241f5.png)

And now I could create a Data Table visualization that splits the data on that field like this;

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/6/a/6abff8a88076364c9e5a9b0e92dadfbaeae7747d.png)

Or I can change the metric from `Count` to `Sum` of some fields such as `bytes`;

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/3/f/3f73aa3e024d30cdd12b28997195fb64b2e34ce0.png)

---

<div class="post-metadata">

**Author:** ![nabilberd](https://avatars.discourse-cdn.com/v4/letter/n/5daacb/32.png) [@nabilberd](https://discuss.elastic.co/u/nabilberd)\
**Post date:** [February 22, 2018, 7:37pm UTC](https://discuss.elastic.co/t/problem-in-script-field/121100/5 "2018-02-22T19:37:43Z")

</div>

I used KIBANA6.2.1 what you said but it show compiler error :

Error: Request to Elasticsearch failed: {"error":{"root\_cause":[{"type":"script\_exception","reason":"compile error","script\_stack":["if( doc['currentStatusName'].value == "AVOIDABLE"){\n return "AVOIDABLE"\n}\nelse if (doc['currentStatusName'].value == "NON\_AVOIDABLE"){ return "NON\_AVOIDABLE" }\nelse {return "ok"}"," ^---- HERE"],"script":"if( doc['currentStatusName'].value == "AVOIDABLE"){\n return "AVOIDABLE"\n}\nelse if (doc['currentStatusName'].value == "NON\_AVOIDABLE"){ return "NON\_AVOIDABLE" }\nelse {return "ok"}","lang":"expression"}],"type":"search\_phase\_execution\_exception","reason":"all shards failed","phase":"query","grouped":true,"failed\_shards":[{"shard":0,"index":"test","node":"aOfYSOCNRuu\_LuhyA-NnFw","reason":{"type":"script\_exception","reason":"compile error","script\_stack":["if( doc['currentStatusName'].value == "AVOIDABLE"){\n return "AVOIDABLE"\n}\nelse if (doc['currentStatusName'].value == "NON\_AVOIDABLE"){ return "NON\_AVOIDABLE" }\nelse {return "ok"}"," ^---- HERE"],"script":"if( doc['currentStatusName'].value == "AVOIDABLE"){\n return "AVOIDABLE"\n}\nelse if (doc['currentStatusName'].value == "NON\_AVOIDABLE"){ return "NON\_AVOIDABLE" }\nelse {return "ok"}","lang":"expression","caused\_by":{"type":"parse\_exception","reason":"unexpected character '"' on line (1) position (38)","caused\_by":{"type":"lexer\_no\_viable\_alt\_exception","reason":null}}}}]},"status":500}

---

<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:** [February 22, 2018, 9:02pm UTC](https://discuss.elastic.co/t/problem-in-script-field/121100/6 "2018-02-22T21:02:03Z")

</div>

Please post your scripted field script

---

<div class="post-metadata">

**Author:** ![nabilberd](https://avatars.discourse-cdn.com/v4/letter/n/5daacb/32.png) [@nabilberd](https://discuss.elastic.co/u/nabilberd)\
**Post date:** [February 27, 2018, 4:26pm UTC](https://discuss.elastic.co/t/problem-in-script-field/121100/7 "2018-02-27T16:26:45Z")

</div>

Sorry for the late response  
it worked when i did this :

if ( params.\_source.machine.os.raw == "win xp") {  
return 1  
} else {  
return 0  
}

---

<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:** [March 27, 2018, 4:27pm UTC](https://discuss.elastic.co/t/problem-in-script-field/121100/8 "2018-03-27T16:27:05Z")

</div>

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