Elasticsearch:在嵌套对象中移除/更新字段 [英] Elasticsearch: remove/update field inside nested object
问题描述
$ _ $$$$$$$$$$$ b $ b_version:5,
found:true,
_source:{
count:42,
list_data:[{
list_id:11,
timestamp:1469125397
},{
list_id:122,
timestamp:1469050965
}]
}
}
这是我的文档模式。 list_data
是嵌套对象。我要求更新/删除 list_data
之内的特定文件。我可以使用groovy脚本更新 count
字段。
$ curl -XPOST'localhost:9200 / index / type / 1212 / _update?pretty'-d'
{
script:ctx._source.count = 41
}'
但不知道如何更新嵌套对象。
例如,我想将它添加到 list_data
。
{
list_id:121,
timestamp:1469050965
}
,我的文档应该更改为:
{
_index:test,
_type:test,
_id:1212,
_version:6,
found:true,
_source {
count:41,
list_data:[{
list_id:11,
timestamp:1469125397
},{
list_id:121,
timestamp:1469050965
},{
list_id:122,
timestamp:1469050965
}]
}
}
如果我根据<$ c $执行删除c> list_id = 122我的记录应该像
{
/ pre>
_index: test,
_type:test,
_id:1212,
_version:7,
found:true,
_source:{
count:41,
list_data:[{
list_id:11,
timestamp:1469125397
},{
list_id:121,
timestamp:1469050965
}]
}
}
解决方案要在您的嵌套字段中添加一个新元素,可以这样进行:
$ curl -XPOST'localhost:9200 / index / type / 1212 / _update?pretty'-d'
{
script :ctx._source.list_data + = newElement,
params:{
newElement:{
list_id:121,
timestamp:1469050965
}
}
}'
要从嵌套字段列表中删除现有元素,可以这样进行:
$ curl -XPOST'localhost:9200 / index / type / 1212 / _update?pretty'-d'
{
script:ctx._source。 list_data.removeAll {it.list_id == remove_id},
params:{
remove_id:122
}
}'
{ "_index" : "test", "_type" : "test", "_id" : "1212", "_version" : 5, "found" : true, "_source" : { "count" : 42, "list_data" : [ { "list_id" : 11, "timestamp" : 1469125397 }, { "list_id" : 122, "timestamp" : 1469050965 } ] } }
This is my document schema.
list_data
is nested object. I have requirement to update/delete particular filed insidelist_data
. I am able to updatecount
field using groovy script.$ curl -XPOST 'localhost:9200/index/type/1212/_update?pretty' -d ' { "script" : "ctx._source.count = 41" }'
But don't know how to update nested object.
For example i want to add this into
list_data
.{ "list_id" : 121, "timestamp" : 1469050965 }
and my document should change to:
{ "_index" : "test", "_type" : "test", "_id" : "1212", "_version" : 6, "found" : true, "_source" : { "count" : 41, "list_data" : [ { "list_id" : 11, "timestamp" : 1469125397 }, { "list_id" : 121, "timestamp" : 1469050965 }, { "list_id" : 122, "timestamp" : 1469050965 } ] } }
and if i perform delete based on
list_id
= 122 my record should look like{ "_index" : "test", "_type" : "test", "_id" : "1212", "_version" : 7, "found" : true, "_source" : { "count" : 41, "list_data" : [ { "list_id" : 11, "timestamp" : 1469125397 }, { "list_id" : 121, "timestamp" : 1469050965 }] } }
解决方案To add a new element to your nested field you can proceed like this:
$ curl -XPOST 'localhost:9200/index/type/1212/_update?pretty' -d ' { "script" : "ctx._source.list_data += newElement", "params": { "newElement": { "list_id" : 121, "timestamp" : 1469050965 } } }'
To remove an existing element from your nested field list, you can proceed like this:
$ curl -XPOST 'localhost:9200/index/type/1212/_update?pretty' -d ' { "script" : "ctx._source.list_data.removeAll{it.list_id == remove_id}", "params": { "remove_id" : 122 } }'
这篇关于Elasticsearch:在嵌套对象中移除/更新字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!