Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 635
Learn Hadoop Avatar asked Sep 05 '25 21:09

Learn Hadoop


1 Answers

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"
    }
  }
}
like image 97
Val Avatar answered Sep 09 '25 20:09

Val