Search code examples
elasticsearchelasticsearch-pluginelastic-stack

Find and replace in elasticsearch all documents


I wanted to replace the single username in all my elasticsearch index documents. Is there any API query ?

I tried searching multiple but couldn't find. Any one has idea?

My scenario:

curl -XPOST 'http://localhost:9200/test/movies/' -d '{"user":"mad", "role":"tester"}'
curl -XPOST 'http://localhost:9200/test/movies/' -d '{"user":"bob", "role":"engineer"}'
curl -XPOST 'http://localhost:9200/test/movies/' -d '{"user":"cat", "role":"engineer"}'
curl -XPOST 'http://localhost:9200/test/movies/' -d '{"user":"bob", "role":"doctor"}'

I have the above data in the index called "test" and type "movies". Here I wanted to replace all the "bob" name with "alice".

Thanks


Solution

  • update-by-query is the way to go.

    POST /test/movies/_update_by_query
    {
      "script": {
        "source": "ctx._source.user = 'alice'"
      },
      "query": {
        "term": {
          "user": "bob"
        }
      }
    }
    

    Note: make sure to enable dynamic scripting in order for this to work.