Skip to content

Instantly share code, notes, and snippets.

@julie-mills
Created January 24, 2024 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julie-mills/50da3261ae1866bd95734544c98b58af to your computer and use it in GitHub Desktop.
Save julie-mills/50da3261ae1866bd95734544c98b58af to your computer and use it in GitHub Desktop.
from elasticsearch import Elasticsearch
# Connect to your Elasticsearch instance
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
# Index name and document ID you want to update
index_name = 'movies'
document_id = 'your_document_id'
# Define the Painless script for the update
update_script = {
"script": {
"lang": "painless",
"source": "ctx._source.views += 1" # Increment 'views' by 1
}
}
# Perform the update using the Painless script
try:
es.update(index=index_name, id=document_id, body=update_script)
print("Document updated successfully!")
except Exception as e:
print(f"Error updating document: {e}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment