Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch Update Doc String Replacement

I have some documents on my Elasticsearch. I want to update my document contents by using String Regexp.

For example, I would like to replace all http words into https words, is it possible ?

Thank You

like image 928
Adityo Setyonugroho Avatar asked Mar 07 '23 20:03

Adityo Setyonugroho


1 Answers

This should get you off to a start. Check out the "Update by Query" API here. The API allows you to include the update script and search query in the same request body.

Regarding your case, an example might look like this...

POST addresses/_update_by_query

{
    "script":
    {
        "lang": "painless",
        "inline": "ctx._source.data.url = ctx._source.data.url.replace('http', 'https')"
    },
    "query":
    {
        "query_string":
        {
            "query": "http://*",
            "analyze_wildcard": true
        }
    }
}

Pretty self explanatory, but script is where we do the update, and query returns the documents to update.

Painless supports regex so you're in luck, look here for some examples, and update the inline value accordingly.

like image 69
Joey Avatar answered Mar 10 '23 09:03

Joey