Search code examples
hadoopelasticsearchelasticsearch-plugin

Elastic search update multiple fields in single query request


working on ES 5.x version, and need to update multiple fields update using script.Also share , have any better solution .

POST ../100/_update
    {
    "script" : {
        "inline": "ctx._source.student.hobbies.add(params.tag)",
        "lang": "painless",
        "params" : {
            "tag" : "cricket"
        }
    }
}

also the same student i need to update phone no. now i am calling 2 api for update hobbies and phone number. looking for better solution to update both in single query


Solution

  • Just add a semicolon between both statements, like this:

    {
      "script": {
        "inline": "ctx._source.student.hobbies.add(params.hobby); ctx._source.student.phone.add(params.phone)",
        "lang": "painless",
        "params": {
          "hobby": "cricket",
          "phone" : "122-33-4567"
        }
      }
    }
    

    If you have more than one value to add to an array, you can do it like this

    {
      "script": {
        "inline": "ctx._source.student.hobbies.addAll(params.hobbies); ctx._source.student.phone.add(params.phone)",
        "lang": "painless",
        "params": {
          "hobbies": ["football", "cricket"],
          "phone" : "122-33-4567"
        }
      }
    }