# Scripted field in kibana (painless)

**URL:** <https://discuss.elastic.co/t/scripted-field-in-kibana-painless/237358>\
**Category:** Kibana\
**Created:** [June 16, 2020, 8:05pm UTC](https://discuss.elastic.co/t/scripted-field-in-kibana-painless/237358 "2020-06-16T20:05:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nb03briceno](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nb03briceno/32/45777_2.png) [@nb03briceno](https://discuss.elastic.co/u/nb03briceno)\
**Post date:** [June 16, 2020, 8:05pm UTC](https://discuss.elastic.co/t/scripted-field-in-kibana-painless/237358/1 "2020-06-16T20:05:22Z")

</div>

Hello everyone,

I'm trying to create a simple scripted field in my index of ES. Basically, I have a field "attempResult" in my index that could be null when the grok(logstash pattern) does not match, or "Successful", "Failed" in the cases in which the pattern match.

So, what I would like is to create a field (number) with the value of 1 when the field 'attemptResult' is "failed", and 0 in the case it is "Successful". I'm trying to create that on the script section of the scripted field definition by using 🙂

if(doc['attemptResult.keyword'] == ("Sucessful")){  
return 0;  
}  
if(doc['attemptResult.keyword'] == ("Failed")){  
return 1;  
}

but when i run it, it always adds null value to the scripted field,

any suggestion?

Thanks su much,

JUAN DAVID BRICENO GUERRERO  
MASTER STUDENT IN SUSTAINABLE INDUSTRIAL ENGINEERING

---

<div class="post-metadata">

**Author:** ![tylersmalley](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tylersmalley/32/8833_2.png) [@tylersmalley](https://discuss.elastic.co/u/tylersmalley)\
**Post date:** [June 17, 2020, 9:32pm UTC](https://discuss.elastic.co/t/scripted-field-in-kibana-painless/237358/2 "2020-06-17T21:32:30Z")

</div>

When the value is null, what would you like it to return, null, 1 or 0?

I assume you want it always to return 1 or 0. In that case, you could modify the scripted field to return 0 when successful, and 1 otherwise:

```auto
if(doc['attemptResult.keyword'] == ("Sucessful")){
return 0;
}

return 1;

```

---

<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 15, 2020, 9:32pm UTC](https://discuss.elastic.co/t/scripted-field-in-kibana-painless/237358/3 "2020-07-15T21:32:47Z")

</div>

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