# Update part of a url

**URL:** <https://discuss.elastic.co/t/update-part-of-a-url/377354>\
**Category:** Elasticsearch\
**Created:** [April 21, 2025, 9:49pm UTC](https://discuss.elastic.co/t/update-part-of-a-url/377354 "2025-04-21T21:49:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carlos\_Fernando\_Palm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carlos_fernando_palm/32/77804_2.png) [@Carlos\_Fernando\_Palm](https://discuss.elastic.co/u/Carlos_Fernando_Palm)\
**Post date:** [April 21, 2025, 9:49pm UTC](https://discuss.elastic.co/t/update-part-of-a-url/377354/1 "2025-04-21T21:49:51Z")

</div>

Hello! my team and I were trying to perform an update operation  
for several records that match having a certain url:

```auto
GET myindex/_search
{
  "query": {
    "wildcard": {
      "url": {
        "value": "*myteam.com*"
      }
    }
  }
}

```

The url looks like:

[https://myteam.com/home/gatherings/gathering/52f8f7ab-632a-45c5-9d40-39eed70ac4cc](https://myteam.com/home/gatherings/gathering/52f8f7ab-632a-45c5-9d40-39eed70ac4cc)

and we would like to update it to look like:

[https://myteam.com/discover/gatherings/gathering/52f8f7ab-632a-45c5-9d40-39eed70ac4cc](https://myteam.com/discover/gatherings/gathering/52f8f7ab-632a-45c5-9d40-39eed70ac4cc)

where we have changed the /home part of the url with a /discover, for all the records that have that.

So far we have not been able to find an example of this kind of update, this begs the question:  
is it possible to do something like this?

---

<div class="post-metadata">

**Author:** ![JD\_Armada](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jd_armada/32/142652_2.png) [@JD\_Armada](https://discuss.elastic.co/u/JD_Armada)\
**Post date:** [April 21, 2025, 11:34pm UTC](https://discuss.elastic.co/t/update-part-of-a-url/377354/2 "2025-04-21T23:34:17Z")

</div>

Hey Carlos,

This is absolutely possible within Elasticsearch. What you want to do is use the [update\_by\_query API](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-update-by-query).  
You are on the right track with with wildcard search, all you have to do is add a script that will modify the URL in the way you desire. You can write that script using [Painless](https://www.elastic.co/docs/reference/scripting-languages/painless/brief-painless-walkthrough).

Here is a sample query using the update\_by\_query API and a script using Painless to modify the URL:

```auto
POST your-index-name/_update_by_query
{
  "query": {
    "wildcard": {
      "url": {
        "value": "*myteam.com*"
      }
    }
  },
  "script": {
    "source": """
      if (ctx._source.url != null && ctx._source.url.contains('/home/')) {
        ctx._source.url = ctx._source.url.replace('/home/', '/discover/');
      }
    """,
    "lang": "painless"
  }
}

```

Please remember to test on a single document or two before bulk updating.
