9

I'm new here.

I have about 200 thousand documents in one index, all have same type. I want to add one more field "category" (which is a keyword string) to every single document.

Is there a convenient way to achieve this? I know normally one query only gets 10000 documents

Thank you very much

Nico
  • 733
  • 1
  • 6
  • 7
  • what did you try? where did it fail? – Scransom Dec 22 '17 at 04:23
  • 2
    This answer might help: https://stackoverflow.com/questions/32931757/how-to-update-multiple-documents-that-match-a-query-in-elasticsearch/32941245#32941245 (hint: use update-by-query) – Val Dec 22 '17 at 04:29

2 Answers2

13
POST index_name/_update_by_query
{
  "script": {
    "inline": "ctx._source.field_name = \"value\"",
    "lang": "painless"
  }
}
Krishna Kumar
  • 369
  • 5
  • 10
  • 6
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – CertainPerformance Aug 01 '19 at 12:30
7

Late but hope this can help you.

You can use _update_by_query API.

Be aware that you can define conditions for your update with the query part of the request. If you just need to update all the documents in your index, you can simply delete the query part.

POST my-index-000001/_update_by_query
{
  "script": {
    "source": "ctx._source.count++",
    "lang": "painless"
  },
  "query": {
    "term": {
      "user.id": "kimchy"
    }
  }
}

Note: the inline option inside _update_by_query is depreciated since 6.x. You should use source instead.

Juan-Kabbali
  • 1,961
  • 15
  • 20