Nico
Nico

Reputation: 893

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

Upvotes: 13

Views: 10147

Answers (2)

Juan-Kabbali
Juan-Kabbali

Reputation: 2414

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.

Upvotes: 9

Krishna Kumar
Krishna Kumar

Reputation: 449

POST index_name/_update_by_query
{
  "script": {
    "inline": "ctx._source.field_name = \"value\"",
    "lang": "painless"
  }
}

Upvotes: 16

Related Questions