无论才能、知识多么卓著,如果缺乏热情,则无异纸上画饼充饥,无补于事。

ES如何更新数组中某一个字段的值

Elasticsearch | 作者 richardhej | 发布于2017年10月20日 | 阅读数:21614

我想更将下面这个数组中 "during":" 888888" 更新成"during":"1" 
{
"_index": "log",
"_type": "parse_log",
"_id": "AV80ztgqx-sSsZV5yS9j",
"_version": 2,
"_score": 1,
"_source": {
"ip": "1.1.1.1",
"tag": "主任",
"location": "北京市,北京市,",
"url": [{
"web": "/412/2014/01/16/2@13030.htm",
"access_time": "2017-08-23T03:24:09",
"during": "99999999",
"url_id": "1"
},
{
"web": "/412/2016/06/07/110@24757.htm",
[b]"during": "888888",[/b]
"url_id": "2",
"access_time": "2017-09-11T03:47:03"
}],
"@timestamp": "2017-10-19 21:13:17"
}
}
我用的script 
{
"script": {
"lang": "painless",
"source": "ctx._source.url.url_id=params.new_url_id;ctx._source.url.during=params.new_during",
"params": {
"new_url_id": "2",
"new_during": "0"
}
}
但是报错
{
"caused_by": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": ["ctx._source.url.url_id=params.new_url_id;",
" ^---- HERE"],
"script": "ctx._source.url.url_id=params.new_url_id;ctx._source.url.during=params.new_during",
"lang": "painless",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Illegal list shortcut value [url_id]."
}
}
}
已邀请:

exceptions - 。。。

赞同来自: richardhej

你好,你用的是高版本的es吧,我用的是5.1版本的,我猜测是跟数组下标有关,所以我试了一下:
 {
"script": {
"lang": "painless",
"inline": "ctx._source.url[1].url_id=params.new_url_id;ctx._source.url[1].during=params.new _during",
"params": {
"new_url_id": "2",
"new_during": "0"
}
}
}
然后curl -XPOST 'http://ip:9200/log/parse_log/AV83qqH8U7winznYGHpK/_update?pretty' -d @script.json
确实更新了:
 {
"_index" : "log",
"_type" : "parse_log",
"_id" : "AV83qqH8U7winznYGHpK",
"_score" : 1.0,
"_source" : {
"ip" : "1.1.1.1",
"tag" : "主任",
"location" : "北京市,北京市,",
"url" : [
{
"web" : "/412/2014/01/16/2@13030.htm",
"access_time" : "2017-08-23T03:24:09",
"during" : "99999999",
"url_id" : "1"
},
{
"web" : "/412/2016/06/07/110@24757.htm",
"during" : "0",
"url_id" : "2",
"access_time" : "2017-09-11T03:47:03"
}
],
"@timestamp" : "2017-10-19 21:13:17"
}
}
由于是低版本的,我把source改成了inline,你的直接用source就行了吧

richardhej

赞同来自:

谢谢。非常感谢。

flytogo

赞同来自:

请问,怎么获取最后一个数组元素的值呢?
 

要回复问题请先登录注册