stefanobaldo
stefanobaldo

Reputation: 2063

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?

Upvotes: 4

Views: 6751

Answers (3)

Also, we can use curl to create a new field in json.

curl -X POST "localhost:9200/your-index/_update_by_query" -H 'Content-Type: application/json' -d'
{
"query": {"match_all": {}
         },
"script": "ctx._source.newfield = ctx._source.data_field1 + ';' + ctx._source.data_field2"
}
'

Upvotes: 0

avp
avp

Reputation: 3340

The latest (6.7) ElasticSearch supports copy_to in mapping. It doesn't say anything about adding delimiters but you can try if it allows that. If it does, that will be neat. https://www.elastic.co/guide/en/elasticsearch/reference/current/copy-to.html

Upvotes: 0

Richa
Richa

Reputation: 7649

  • 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.

Upvotes: 7

Related Questions