Created
January 24, 2024 00:38
-
-
Save julie-mills/c2bc1b4d32198fbc9df0975cd44546c0 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 you want to update | |
index_name = 'movies' | |
document_id = 'your_document_id' | |
# Retrieve the current document to get the current 'views' value | |
try: | |
current_doc = es.get(index=index_name, id=document_id) | |
current_views = current_doc['_source']['views'] | |
except Exception as e: | |
print(f"Error retrieving current document: {e}") | |
current_views = 0 # Set a default value if there's an error | |
# Define the update body to increment 'views' by 1 | |
update_body = { | |
"doc": { | |
"views": current_views + 1 # Increment 'views' by 1 | |
} | |
} | |
# Perform the update | |
try: | |
es.update(index=index_name, id=document_id, body=update_body) | |
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