Painless script not working as expected

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!

Please somebody help with this!

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:

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";
}

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