Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a field copying data from another fields

I have an index with documents like the following:

[
    {
        "field1": "foo",
        "field2": "bar"
    },
    ...
]

Basically what I need is to add a field field3 to every document with field1 and field2 concatenated and separated by a semicolon, like this:

[
    {
        "field1": "foo",
        "field2": "bar",
        "field3": "foo;bar"
    },
    ...
]

Is there any way to add the new column to all my documents copying values from the other fields automatically?

like image 860
stefanobaldo Avatar asked Mar 12 '23 21:03

stefanobaldo


1 Answers

  • For 1.x versions up vote, Use Update by query plugin and
  • For versions greater than 2.3,Use this provided by elasticsearch.

Use following query then:

POST /my_index/my_type/_update_by_query
{
"query": {
  "match_all": {}
},
"script": "ctx._source.field3 = ctx._source.field1 + ';' + ctx._source.field2"
}

Make sure to enable scripting in elasticsearch.yml and restart ES.

This requires no reindexing

Hope this works for you.

like image 77
Richa Avatar answered Mar 20 '23 04:03

Richa