Quick Start with Elasticsearch on AWS

Libraries

You need elasticsearch (not AWS specific) and requests-aws4auth so that requests python library can use AWS authentication (interesting approach!).

Initialise

 1from elasticsearch import Elasticsearch, RequestsHttpConnection
 2from requests_aws4auth import AWS4Auth
 3
 4awsauth = AWS4Auth(key, secret, region, "es")
 5
 6es = Elasticsearch(
 7    [{"host": endpoint, "port": 443}],
 8    http_auth=awsauth,
 9    use_ssl=True,
10    verify_certs=True,
11    connection_class=RequestsHttpConnection)
12
13es.info()

should print something like

 1{'name': '...',
 2 'cluster_name': '...',
 3 'cluster_uuid': '...',
 4 'version': {'number': '7.4.2',
 5  'build_flavor': 'oss',
 6  'build_type': 'tar',
 7  'build_hash': 'unknown',
 8  'build_date': '...',
 9  'build_snapshot': False,
10  'lucene_version': '8.2.0',
11  'minimum_wire_compatibility_version': '6.8.0',
12  'minimum_index_compatibility_version': '6.0.0-beta1'},
13 'tagline': 'You Know, for Search'}

Listing All Indices

1# list all indices
2es.indices.get_alias("*")
3
4# or all indices keys
5es.indices.get_alias().keys()

Get Specific Document

1es.get(index="...", id="...")

Get Everything in an Index

1r = es.search(index="...", body={"query": { "match_all": {}}}, size=10000)

Not the size arg - has to be large enough. It’s not pretty so don’t use in prod.

Add to Index

1es.index("index_name", id="desired doc id", body={"test1": "1", "test2": "2"})

body is the actual document body to be indexed.

Have feedback or questions? Feel free to email me.