Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch - Copy one field value to other field for all documents

We have a field "name" in the index. We recently added a new field "alias". I want to copy name field value to the new field alias for all documents.

Is there any Update query that will do this? If that is not possible , Help me to achieve this. Thanks in advance

I am trying this query http://URL/index/profile/_update_by_query

{
  "query": {

        "constant_score" : {
            "filter" : {
                "exists" : { "field" : "name" }
            }
        }

  },
  "script" : "ctx._source.alias = name;"
}

In the script , I am not sure how to give name field. I getting error

{
  "error": {
    "root_cause": [
      {
        "type": "class_cast_exception",
        "reason": "java.lang.String cannot be cast to java.util.Map"
      }
    ],
    "type": "class_cast_exception",
    "reason": "java.lang.String cannot be cast to java.util.Map"
  },
  "status": 500
}
like image 327
Ramesh Avatar asked May 15 '17 07:05

Ramesh


1 Answers

Indeed, the syntax has changed a tiny little bit since. You need to modify your query to this:

{
  "query": {

        "constant_score" : {
            "filter" : {
                "exists" : { "field" : "name" }
            }
        }

  },
  "script" : {
      "inline": "ctx._source.alias = ctx._source.name;"
  }
}

UPDATE for ES 6

Use source instead of inline

like image 148
Val Avatar answered Oct 30 '22 14:10

Val