# Remove multiple spaces with script

**URL:** <https://discuss.elastic.co/t/remove-multiple-spaces-with-script/335638>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [June 9, 2023, 1:43pm UTC](https://discuss.elastic.co/t/remove-multiple-spaces-with-script/335638 "2023-06-09T13:43:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruwi](https://avatars.discourse-cdn.com/v4/letter/r/e19b73/32.png) [@Ruwi](https://discuss.elastic.co/u/Ruwi)\
**Post date:** [June 9, 2023, 1:43pm UTC](https://discuss.elastic.co/t/remove-multiple-spaces-with-script/335638/1 "2023-06-09T13:43:02Z")

</div>

Hello,

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

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

```

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

```auto
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

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [June 9, 2023, 7:12pm UTC](https://discuss.elastic.co/t/remove-multiple-spaces-with-script/335638/2 "2023-06-09T19:12:16Z")

</div>

Hi @Ruwi

Try this script:

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

```

---

<div class="post-metadata">

**Author:** ![Ruwi](https://avatars.discourse-cdn.com/v4/letter/r/e19b73/32.png) [@Ruwi](https://discuss.elastic.co/u/Ruwi)\
**Post date:** [June 14, 2023, 11:56am UTC](https://discuss.elastic.co/t/remove-multiple-spaces-with-script/335638/3 "2023-06-14T11:56:26Z")

</div>

Yes, it worked! Thanks a lot. 🙂

---

<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 12, 2023, 11:56am UTC](https://discuss.elastic.co/t/remove-multiple-spaces-with-script/335638/4 "2023-07-12T11:56:33Z")

</div>

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