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

from elasticsearch import Elasticsearch, RequestsHttpConnection
from requests_aws4auth import AWS4Auth

awsauth = AWS4Auth(key, secret, region, "es")

es = Elasticsearch(
    [{"host": endpoint, "port": 443}],
    http_auth=awsauth,
    use_ssl=True,
    verify_certs=True,
    connection_class=RequestsHttpConnection)

es.info()

should print something like

{'name': '...',
 'cluster_name': '...',
 'cluster_uuid': '...',
 'version': {'number': '7.4.2',
  'build_flavor': 'oss',
  'build_type': 'tar',
  'build_hash': 'unknown',
  'build_date': '...',
  'build_snapshot': False,
  'lucene_version': '8.2.0',
  'minimum_wire_compatibility_version': '6.8.0',
  'minimum_index_compatibility_version': '6.0.0-beta1'},
 'tagline': 'You Know, for Search'}

Listing All Indices

# list all indices
es.indices.get_alias("*")

# or all indices keys
es.indices.get_alias().keys()

Get Specific Document

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

Get Everything in an Index

r = 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

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

body is the actual document body to be indexed.


To contact me, send an email anytime or leave a comment below.