最近在看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"
]
}
}