elasticsearch Variable [new_tag] is not defined.错误

最近在看elasticsearch 5.2.2版本对应的信息 

https://es.xiaoleilu.com/030_Data/45_Partial_update.html

其中局部更新中,普通的更新可以,但是如果用的是脚本更新的时候

duanlshdeMacBook-Pro:bin duanlsh$ curl -i -XPOST 'http://localhost:9200/website/blog/2/_update?pretty' -d '{"script":"ctx._source.tags+=new_tag","params":{"new_tag":"search"}}'

报错信息为:

HTTP/1.1 400 Bad Request
content-type: application/json; charset=UTF-8
content-length: 705

{
  "error" : {
    "root_cause" : [
      {
        "type" : "remote_transport_exception",
        "reason" : "[dAG25k-][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",
      "caused_by" : {
        "type" : "illegal_argument_exception",
        "reason" : "Variable [new_tag] is not defined."
      },
      "script_stack" : [
        "ctx._source.tags+=new_tag",
        "                  ^---- HERE"
      ],
      "script" : "ctx._source.tags+=new_tag",
      "lang" : "painless"
    }
  },
  "status" : 400
}

后来搜索啦一些解决办法

http://stackoverflow.com/questions/43079189/elasticsearch-script-variable-not-defined?answertab=votes#tab-top

解决的方式为更改解决命令信息

curl -i -XPOST 'http://localhost:9200/website/blog/2/_update?pretty' -d '{
"script":{
    "inline":"ctx._source.tags.add(params.new_tag)",
    "params":{
         "new_tag":"tag3"
    }
  }
}'

添加成功
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 182

{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "2",
  "_version" : 23,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}


获取对应的数据:

duanlshdeMacBook-Pro:bin duanlsh$ curl -i -XGET 'http://localhost:9200/website/blog/2?pretty'
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 296

{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "2",
  "_version" : 23,
  "found" : true,
  "_source" : {
    "title" : "My first external blog entry",
    "text" : "Starting to get the hang of this...",
    "view" : 2,
    "tags" : [
      "text",
      "tag2",
      "tag3"
    ]
  }
}

 

转载于:https://my.oschina.net/u/1459447/blog/870721

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值