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?

1 Answer 1

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"
              }
            }
          }
)
Sign up to request clarification or add additional context in comments.

6 Comments

This can't be done that way as groovy scripting is disabled and is not something I am allowed to enable.
which version of ES you are using?
using AWS ElasticSearch, so version 1.5.2 . This is for a business use, so I will not be able to enable groovy.
then how are you doing in right now? do you have ability to store script file in config folder?
Sorry, when I said I tried the "script" thing before, it used a "doc" tag, which is why it didn't work as expected. So using a script is out for me.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.