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

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..

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

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

doc['error'].value == 'NONE' ? 'typea' : doc['error'].value == 'ERRX' ? 'typeb' : 'typec'
4 Likes

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