Update part of a url

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

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

The url looks like:

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

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?

Hey Carlos,

This is absolutely possible within Elasticsearch. What you want to do is use the update_by_query API.
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.

Here is a sample query using the update_by_query API and a script using Painless to modify the URL:

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.

3 Likes