# How to create a string scripted field with else if condition?

**URL:** <https://discuss.elastic.co/t/how-to-create-a-string-scripted-field-with-else-if-condition/79845>\
**Category:** Kibana\
**Created:** [March 24, 2017, 6:29am UTC](https://discuss.elastic.co/t/how-to-create-a-string-scripted-field-with-else-if-condition/79845 "2017-03-24T06:29:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elasticheart](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elasticheart/32/65189_2.png) [@elasticheart](https://discuss.elastic.co/u/elasticheart)\
**Post date:** [March 24, 2017, 6:29am UTC](https://discuss.elastic.co/t/how-to-create-a-string-scripted-field-with-else-if-condition/79845/1 "2017-03-24T06:29:26Z")

</div>

Hi,

I am using ELK GA 5.0.0 . In Kibana, I have created a string type scripted field 'err\_type` like below;

```
if (doc['error'].value == 'NONE') { 
  return 'typea';
}else if (doc['error'].value == 'ERRX') { 
  return 'typeb';
}
return 'typec';

```

When I try to + or - filter using it in Discover, I am getting `Discover: compile error`. I was able to see the generated fields in the left sidebar. I then clicked `+` filter corresponding to `typec`. Then I got the error. When I click the edit button, I have seen the query constructed like below;

```
{
  "script": {
    "script": {
      "inline": "(if (doc['error'].value == 'NONE') { \n return 'typea';\n}else if (doc['error'].value == 'ERRX') { \n return 'typeb';\n}\nreturn 'typec';) == params.value",
      "lang": "painless",
      "params": {
        "value": "typec"
      }
    }
  }
}

```

How can I fix this?

Thanks in advance..

---

<div class="post-metadata">

**Author:** ![Stacey\_Gammon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stacey_gammon/32/14025_2.png) [@Stacey\_Gammon](https://discuss.elastic.co/u/Stacey_Gammon)\
**Post date:** [March 24, 2017, 1:10pm UTC](https://discuss.elastic.co/t/how-to-create-a-string-scripted-field-with-else-if-condition/79845/2 "2017-03-24T13:10:14Z")

</div>

This is a known issue with 5.0 that was fixed in 5.1: [https://github.com/elastic/kibana/issues/9024](https://github.com/elastic/kibana/issues/9024)

You can upgrade, or adjust your scripted filter so it doesn't use multiple statements, like so:

```auto
doc['error'].value == 'NONE' ? 'typea' : doc['error'].value == 'ERRX' ? 'typeb' : 'typec'

```

---

<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:** [April 21, 2017, 1:10pm UTC](https://discuss.elastic.co/t/how-to-create-a-string-scripted-field-with-else-if-condition/79845/3 "2017-04-21T13:10:18Z")

</div>

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