无法更新elasticsearch

时间:2017-08-04 11:03:30

标签: elasticsearch elasticsearch-2.0

我正在努力转储和更新Elasticsearch数据,我能够正确转储数据,但我无法更新记录,例如

当我做的时候

GET test/users/3985882

我会得到以下回复

{
  "_index": "test",
  "_type": "users",
  "_id": "3985882",
  "_version": 1,
  "found": true,
  "_source": {
    "gender": null,
    "user_mixpanel_data": {
      "event_name": "Detail",
      "time": "2017-07-01 01:37:43 IST",
      "email": "abc@gmail.com"
    },
    "email": "abc@gmail.com",
    "user_invoices": [
      {
        "number": "54539",
        "orders": [
          {
            "order_id": "54539C1"
          }
        ]
      }
    ]
  }
}

现在,当我尝试更新嵌套对象user_invoices并需要更新时,我正在尝试从SO上询问的以下问题的脚本,即 elasticsearch:use script to update nested field?

我根据我的问题修改了脚本。

POST test/users/_update 
{
    "script": "def updated = false; ctx._source.user_invoices?.each { obj -> if (obj.number == item.number) { obj.number = item.new_number; updated = true;}; if (!updated) { ctx._source.user_invoices = ((ctx._source.user_invoices ?: []) + item)}",
    "lang": "painless",
    "params": {
        "item": {
            "number": "54539",
            "new_number": "54540",
            "order_id": "54539C1",
            "new_order_id": "54539C2"
        }
    }
}

上面的脚本给了我以下回复

{
  "_index": "test",
  "_type": "users",
  "_id": "_update",
  "_version": 28,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": false
}

这里的响应显示查询成功,但是当我再次看到相同的记录时,我没有得到更新的响应。

1 个答案:

答案 0 :(得分:0)

你没有传递id 这里

POST test/users/id/_update