Last active
January 25, 2024 16:49
-
-
Save julie-mills/bd4f5735ce2ef9921c7d1671c50ebb00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from elasticsearch import Elasticsearch | |
# Connect to your Elasticsearch instance | |
es = Elasticsearch([{'host': 'localhost', 'port': 9200}]) | |
# Index name and document ID for the example | |
index_name = 'your_index' | |
document_id = 'your_document_id' | |
# Specify the nested field and the updated value | |
nested_field = "nested_field_name" | |
updated_value = "new_value" | |
# Define the Painless script for the update | |
update_script = { | |
"script": { | |
"lang": "painless", | |
"source": "ctx._source.nested_field_name = params.updated_value", | |
"params": { | |
"updated_value": updated_value | |
} | |
} | |
} | |
# Perform the update using the Update API and the Painless script | |
try: | |
es.update(index=index_name, id=document_id, body=update_script) | |
print("Nested object updated successfully!") | |
except Exception as e: | |
print(f"Error updating nested object: {e}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment