参考《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,因此这里需要使用ArrayList
的add
方法,不能使用+
进行操作。