3

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

Madhu Akula
  • 33
  • 1
  • 4
  • What are you trying to do? Index the data provided? Search? There are different URLs/HTTP method combinations for each. `-XPUT` for a particular type/index will index a document (replacing it if you specify an existing ID) https://www.elastic.co/guide/en/elasticsearch/guide/current/index-doc.html `-XGET` with particular type/index to get a specific record https://www.elastic.co/guide/en/elasticsearch/guide/current/get-doc.html etc. – Berin Loritsch Jul 28 '16 at 12:36
  • I know how to do update using for one document by taking index ID. But the index id is generated automatically. I wanted to replace one string with another in all the documents. – Madhu Akula Jul 28 '16 at 12:39

1 Answers1

11

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.

Sergey
  • 3,253
  • 2
  • 33
  • 55
Val
  • 207,596
  • 13
  • 358
  • 360
  • Thank you so much. Works like charm. – Madhu Akula Jul 28 '16 at 12:46
  • Do we need to enable groovy and inline script for this? If you know exact syntax could you please comment here? script.inline: true script.indexed: true – Madhu Akula Jul 28 '16 at 12:49
  • Good point, yes, you do, I will update my answer to mention it. – Val Jul 28 '16 at 12:50
  • For 2.3.4 still not working, after enabling in elasticsearch.yml and after restart the service. Here is the error I am getting {"error":{"root_cause":[{"type":"script_exception","reason":"failed to run inline script [ctx._source.user = alice] using lang [groovy]"}],"type":"script_exception","reason":"failed to run inline script [ctx._source.user = alice] using lang [groovy]","caused_by":{"type":"missing_property_exception","reason":"No such property: alice for class: 11d6dc9400ad10c4b07xxxxxxxxxxxxx}},"status":500} – Madhu Akula Jul 28 '16 at 13:00
  • You need single quotes (or escaped double quotes) around alice, i.e. `ctx._source.user = 'alice'` – Val Jul 28 '16 at 13:06
  • Fixed it. Working. Do you know any way to export data into json ? – Madhu Akula Jul 28 '16 at 13:30
  • Good. Yes, elasticdump is a good candidate tool for that. See http://stackoverflow.com/questions/38517663/tools-to-migrate-index-from-elasticsearch-1-x-to-elasticsearch-2-x/38517865#38517865 – Val Jul 28 '16 at 13:31