Remove multiple spaces with script

Hello,

In street fields, the number of spaces has constantly changing values as follows.

"street": "YALIM MAH.                    last/                 şajdsj            sdkdşfd"

I can remove the spaces as follows, but the number of spaces can be different.

POST /my-index/_update_by_query
{
 "script": {
   "source": "ctx._source.street = ctx._source.street.replace('                    ', ' ')",
   "lang": "painless"
 },
 "query": {
   "match_all": {}
 }
}

How can I make a single space in values with multiple spaces? Thanks

Hi @Ruwi

Try this script:

{
 "script": {
   "source": """
   ctx._source.street = /\s+/.matcher(ctx._source.street).replaceAll(' ')
   """,
   "lang": "painless"
 },
 "query": {
   "match_all": {}
 }
}

Yes, it worked! Thanks a lot. :slight_smile:

1 Like

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