es(elasticsearch)5.4下params参数的使用

参考《ES权威指南》时,用到类似于下面的参数进行修改文档的属性时,遇到了问题。

POST /website/blog/1/_update
{
   "script" : "ctx._source.tags.new_tag",
   "params" : {
      "new_tag" : "mylove"
   }
}

遇到的问题如下:

{
   "error": {
      "root_cause": [
         {
            "type": "remote_transport_exception",
            "reason": "[uDBlsNL][127.0.0.1:9300][indices:data/write/update[s]]"
         }
      ],
      "type": "illegal_argument_exception",
      "reason": "failed to execute script",
      "caused_by": {
         "type": "script_exception",
         "reason": "compile error",
         "script_stack": [
            "ctx._source.tags+=new_tag",
            "                  ^---- HERE"
         ],
         "script": "ctx._source.tags+=new_tag",
         "lang": "painless",
         "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Variable [new_tag] is not defined."
         }
      }
   },
   "status": 400
}

从问题的提示来看,应该是新版的es在语法上做了调整,原来可以直接用new_tag这个参数来直接修改原来文档的属性,现在这种方式应该是过时了,正确的打开方式如下:

POST /website/blog/1/_update
{
  "script":{
    "inline":"ctx._source.tags.add(params.new_tag)",
    "params":{
         "new_tag":"mylove"
    }
  }
}

大家应该可以看出来,原来的tags属性时一个List,因此这里需要使用ArrayListadd方法,不能使用+进行操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值