How to Instantiate AWS ES Connection using python elasticsearch_dsl
import boto3, osfrom requests_aws4auth import AWS4Authfrom elasticsearch import RequestsHttpConnectionfrom elasticsearch_dsl import connectionsdef init_connection():service = "es"region = "us-west-2"credentials = boto3.Session().get_credentials()awsauth = AWS4Auth(credentials.access_key,credentials.secret_key,region,service,session_token=credentials.token,)connections.configure(default={"hosts": [{"host": os.environ["ES_ENDPOINT"], "port": 443,}],"http_auth": awsauth,"use_ssl": True,"verify_certs": True,"connection_class": RequestsHttpConnection,})connections.get_connection() # Checks if conn already exists if not creates a new one
Checkout the gist here: https://gist.github.com/asthinasthi/9ceb20159edc70df0997ca2a79361548
Saving a document
- Tweet Document/Entity
from elasticsearch_dsl import Document, Date, Text, Integer, Nestedclass Tweet(Document): class Index:
name = "index-tweet"
created_at = Date(default_timezone="UTC")
body = Text()
favorites = Integer()
retweets = Integer()
2. Save a tweet/Persist Tweet
init_connection() # See above
tweet = Tweet(body="My first tweet", favorites=5, retweets=10)
tweet.save()
Check your ES for all docs in index-tweet You should see this document