# Painless script not working as expected

**URL:** <https://discuss.elastic.co/t/painless-script-not-working-as-expected/185978>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [June 16, 2019, 11:25pm UTC](https://discuss.elastic.co/t/painless-script-not-working-as-expected/185978 "2019-06-16T23:25:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nitish\_Jain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nitish_jain/32/48163_2.png) [@Nitish\_Jain](https://discuss.elastic.co/u/Nitish_Jain)\
**Post date:** [June 16, 2019, 11:25pm UTC](https://discuss.elastic.co/t/painless-script-not-working-as-expected/185978/1 "2019-06-16T23:25:31Z")

</div>

Hi,

I am trinyg to run this script and it's not working as expected.

String messageValue = doc['message.keyword'];  
if ( messageValue != null) {  
String val = messageValue .substring(0,30);  
return val + "678"; }  
else { return "message is not there";}

In first return statement, if I simply return val, it's printing the correct value, but as soon as I do substring, I do not get anything back in the results.

Also, in the first line of the script, if I do doc['message.keyword'].value instead of doc['message.keyword'], it is returning null in some cases where as data is present in all the cases. I tried using def data type instead of String too, but even that worked the same way.

Please advise on what am I doing wrong.

Thanks!

---

<div class="post-metadata">

**Author:** ![Nitish\_Jain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nitish_jain/32/48163_2.png) [@Nitish\_Jain](https://discuss.elastic.co/u/Nitish_Jain)\
**Post date:** [June 18, 2019, 2:55pm UTC](https://discuss.elastic.co/t/painless-script-not-working-as-expected/185978/2 "2019-06-18T14:55:57Z")

</div>

Please somebody help with this!

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [June 20, 2019, 11:25pm UTC](https://discuss.elastic.co/t/painless-script-not-working-as-expected/185978/3 "2019-06-20T23:25:17Z")

</div>

> if I do doc['message.keyword'].value instead of doc['message.keyword'], it is returning null in some cases where as data is present in all the cases.

Are you sure? The `doc` map returns an accessor for a field, not the actual field value. That accessor is essentially a list over the values for that document, and it is empty if there are no values for that document. The accessor should not be null (it throws an error if the mapping does not exist). So I would rewrite your script as:

```auto
def messageValue = doc['message.keyword'];
if (messageValue.size() > 0) {
  String val = messageValue.get(0).substring(0,30);
  return val + "678";
} else {
  return "message is not there";
}

```

---

<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 18, 2019, 11:25pm UTC](https://discuss.elastic.co/t/painless-script-not-working-as-expected/185978/4 "2019-07-18T23:25:17Z")

</div>

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