Painless scripted fields for truncating multiple values of a field

Hello All,
I have my field as below.
tags: targetgeo:russia, targetgeo:china, targetgeo:iran

I wanted to truncate the string "targetgeo:" and need only values like russia,china,iran.

My painless script is like below,

if (doc.containsKey('tags.keyword') && doc['tags.keyword'].length>0){
  for (int i = 0; i < doc['tags.keyword'].length; i++) {
    if (doc['tags.keyword'][i].length() > 10) {
      if (doc['tags.keyword'][i].substring(0,10) == 'targetgeo:') {
              return doc['tags.keyword'][i].substring(10,doc['tags.keyword'][i].length());
      }
    }
  }
}
return "";

And I am getting only Iran as the result since that was the last field.
I think I need one more loop to add that field. Can you give me some suggestions.
Thanks.

You can use REPLACE?

REPLACE(
    source,      
    pattern,     
    replacement)

Something like below

return REPLACE(doc['tags.keyword'], "targetgeo:", "")

A little POC -->

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