使用python附加到现有的elasticsearch数组字段 [英] Append to an existing elasticsearch array field using python

查看:1426
本文介绍了使用python附加到现有的elasticsearch数组字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下ES对象:

    {
      "_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"
          }
        ]
      }
    }

我想在apps数组中添加另一个对象,同时保留现有数据,如下所示:

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"
          }
        ]
      }
    }

如您所见,我只向这些应用程序对象添加了第三个应用程序。我需要使用弹性搜索模块在Python中执行此操作。我所有的尝试都会覆盖现有的项目或不起作用。我尝试使用ctx._source.apps + = newapps脚本,但使用Python,似乎只是附加一个名为scripts的新对象和新数据,与apps对象一起,而不是添加到应用程序对象。任何想法?

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?

推荐答案

这样的东西应该可以工作,你可以参考。您需要将正文中 param

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"
              }
            }
          }
)

这篇关于使用python附加到现有的elasticsearch数组字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆