Elasticsearch局部更新(数组追加),elasticsearch数组
Elasticsearch局部更新(数组追加)
现在需要实现这样一个功能:(本人使用ES版本为5.4.0)
比如:我这需要操作的某个字段tags:[“tag1”,”tag2”,”tag3’] , 现在发现了一个新标签”tag4”,需要加入到tags中。
第1步:创建一个新文档 - 确定(index = test_index; type = test_type; id = 1)
POST test_index/test_type/1
{
"tags":["tag1", "tag2", "tag3"]
}
第2步:使用脚本将附加值附加到tags数组
- Elasticsearchrch 教程中,是这样实现的
POST test_index/test_type/1/_update
{
"script" : "ctx._source.tags+=new_tag",
"params" : {
"new_tag" : "tag4"
}
}
然后出现这个错误
{
"error": {
"root_cause": [
{
"type": "remote_transport_exception",
"reason": "[**_test_01][**.**.**.**: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
}
- 经过我上网找了一些资料后,应该是这样写:
POST test_index/test_type/1/_update
{
"script" : {
"inline": "ctx._source.tags.add(params.new_tag)",
"params" : {
"new_tag" : "tag4"
}
}
}
查看一下:
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_version": 32,
"found": true,
"_source": {
"tags": [
"tag1",
"tag2",
"tag3",
"tag4"
]
}
}
成功添加
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。