Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elasticsearch add field to all documents

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

like image 372
Nico Avatar asked Dec 18 '22 02:12

Nico


2 Answers

POST index_name/_update_by_query
{
  "script": {
    "inline": "ctx._source.field_name = \"value\"",
    "lang": "painless"
  }
}
like image 196
Krishna Kumar Avatar answered Jan 11 '23 17:01

Krishna Kumar


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.

like image 37
Juan-Kabbali Avatar answered Jan 11 '23 16:01

Juan-Kabbali