3

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?

Scotty
  • 399
  • 1
  • 4
  • 13

1 Answers1

1

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"
              }
            }
          }
)
ChintanShah25
  • 12,366
  • 3
  • 43
  • 44