es用python增加字段_使用python附加到现有的elasticsearch数组字段

I have the following ES object:

{

"_index": "index_name",

"_type": "my_type",

"_id": "12345678-abcd-9012-efgh-3456ijkl7890"

"_source": {

"apps": [

{

"processName": "process_name_1",

"name": "app_name_1",

"VersionName": "version_1"

},

{

"processName": "process_name_2",

"name": "app_name_2",

"VersionName": "version_2"

}

]

}

}

I want to add another object to the "apps" array while keeping the existing data so it looks like the following:

{

"_index": "index_name",

"_type": "my_type",

"_id": "12345678-abcd-9012-efgh-3456ijkl7890"

"_source": {

"apps": [

{

"processName": "process_name_1",

"name": "app_name_1",

"VersionName": "version_1"

},

{

"processName": "process_name_2",

"name": "app_name_2",

"VersionName": "version_2"

},

{

"processName": "process_name_3",

"name": "app_name_3",

"VersionName": "version_3"

}

]

}

}

As you see, I only added a third "app" to these apps objects. I need to do this in Python using the elasticsearch module. All my attempts either overwrite the existing items or do not work. I tried using the "ctx._source.apps+= newapps" scripting, but with Python, it seems to just append a new object named "scripts" and the new data, alongside the "apps" object, instead of just add to the "apps" object. Any thoughts?

解决方案

something like this should work, you can refer to update api for python for more info. You need to put your script inside body param

from elasticsearch import Elasticsearch

es = Elasticsearch()

es.update(

index="index_name",

doc_type="my_type",

id="12345678-abcd-9012-efgh-3456ijkl7890",

body={"script": "ctx._source.apps += app",

"params": {

"app": {

"processName": "process_3",

"name": "name_3",

"VersionName": "version_3"

}

}

}

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值