# Painless (Elasticsearch) can't use keyword - script error

**URL:** https://discuss.elastic.co/t/painless-elasticsearch-cant-use-keyword-script-error/213878
**Category:** Kibana
**Created:** [January 6, 2020, 11:06am UTC](https://discuss.elastic.co/t/painless-elasticsearch-cant-use-keyword-script-error/213878 "2020-01-06T11:06:17Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Matthias\_Seidl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matthias_seidl/32/60323_2.png) [@Matthias\_Seidl](https://discuss.elastic.co/u/Matthias_Seidl)
#### Post date: [January 6, 2020, 11:06am UTC](https://discuss.elastic.co/t/painless-elasticsearch-cant-use-keyword-script-error/213878/1 "2020-01-06T11:06:17Z")

</div>

I'm trying to create a scripted field in Kibana, which checks whether the field "Direction" is "I" or not.  
`if (doc['Direction'].value != "I") {return 1;} else {return 0;}`

But for some reason, it won't work. With all other fields .keyword, that aren't explicitly mentioned in the index mapping it works that way, but I had to mention Direction in the mapping because I also have an alias pointing to it. For Direction I put the following in the mapping file:

```
"Direction": {
  "type": "keyword"
}

```

And there is also an alias pointing to Direction:

```
"ISDN_Direction": {
      "path": "Direction",
      "type": "alias"
    }

```

but both fields can't be used in the painless script. I don't get an error, but the result preview, for the first 10 results, is just empty.

Can someone help me with this issue?

---

<div class="post-metadata">

### Author: ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)
#### Post date: [January 6, 2020, 11:42pm UTC](https://discuss.elastic.co/t/painless-elasticsearch-cant-use-keyword-script-error/213878/2 "2020-01-06T23:42:42Z")

</div>

Is your scripted field a number? Painless scripts can be hard to debug, but my suspicion here is that the result is 0 which is turning into the empty string.

---

<div class="post-metadata">

### Author: ![Matthias\_Seidl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matthias_seidl/32/60323_2.png) [@Matthias\_Seidl](https://discuss.elastic.co/u/Matthias_Seidl)
#### Post date: [January 7, 2020, 7:55am UTC](https://discuss.elastic.co/t/painless-elasticsearch-cant-use-keyword-script-error/213878/3 "2020-01-07T07:55:42Z")

</div>

Thanks for your response Wylie! The scripted field is a number, but this isn't the problem. When I tried the comparison with other fields, e.g. "Name.keyword" it worked perfectly fine.  
These fields aren't explicitly mentioned in my mapping and they work correctly. It's just this one field that I mapped as a keyword type in the mapping, which isn't working.

Do I have to add something else to the mapping?

---

<div class="post-metadata">

### Author: ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)
#### Post date: [January 7, 2020, 3:42pm UTC](https://discuss.elastic.co/t/painless-elasticsearch-cant-use-keyword-script-error/213878/4 "2020-01-07T15:42:18Z")

</div>

My understanding of painless is limited, but I will try to get feedback from an expert on this.

---

<div class="post-metadata">

### Author: ![Matthias\_Seidl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matthias_seidl/32/60323_2.png) [@Matthias\_Seidl](https://discuss.elastic.co/u/Matthias_Seidl)
#### Post date: [January 7, 2020, 3:54pm UTC](https://discuss.elastic.co/t/painless-elasticsearch-cant-use-keyword-script-error/213878/5 "2020-01-07T15:54:45Z")

</div>

I found the problem!  
I changed the data type mapping, but I still had indices in my ES DB that had an old mapping on "text". Kibana didn't show me a mapping conflict, since both, text and keywords, are strings.

I deleted the old indices which mapped the field to "text" and now the painless calculation works without any problem 🙂

---

<div class="post-metadata">

### Author: ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)
#### Post date: [January 7, 2020, 5:02pm UTC](https://discuss.elastic.co/t/painless-elasticsearch-cant-use-keyword-script-error/213878/6 "2020-01-07T17:02:09Z")

</div>

Sounds like that "could absolutely be an issue". The other potential issue is a null-pointer one, if not all documents contain the field you care about there could be a null error.

---

<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: [February 4, 2020, 5:02pm UTC](https://discuss.elastic.co/t/painless-elasticsearch-cant-use-keyword-script-error/213878/7 "2020-02-04T17:02:10Z")

</div>

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