Python
Setting up your Python app to use Bonsai Elasticsearch is quick and easy. Just follow the steps below:
Adding the Elasticsearch library
You’ll need to add the elasticsearch library to your Pipfile
file like so:
pipenv install elasticsearch elasticsearch-dsl
Connecting to Bonsai
Bonsai requires basic authentication for all read/write requests. You’ll need to configure the client so that it includes the username and password when communicating with the cluster. The following code is a good starter for integrating Bonsai Elasticsearch into your app:
import os, base64, re, logging from elasticsearch import Elasticsearch # Log transport details (optional): logging.basicConfig(level=logging.INFO) # Parse the auth and host from env: bonsai = os.environ['BONSAI_URL'] auth = re.search('https\:\/\/(.*)\@', bonsai).group(1).split(':') host = bonsai.replace('https://%s:%s@' % (auth[0], auth[1]), '') # optional port match = re.search('(:\d+)', host) if match: p = match.group(0) host = host.replace(p, '') port = int(p.split(':')[1]) else: port=443 # Connect to cluster over SSL using auth for best security: es_header = [{ 'host': host, 'port': port, 'use_ssl': True, 'http_auth': (auth[0],auth[1]) }] # Instantiate the new Elasticsearch connection: es = Elasticsearch(es_header) # Verify that Python can talk to Bonsai (optional): es.ping()
Feel free to adapt that code to your particular app. Keep in mind that the core elements here can be moved around, but really shouldn’t be changed or further simplified.
For example, the snippet above parses out the authentication credentials from the BONSAI_URL
environment variable. This is so the username and password don’t need to be hard coded into your app, where they could pose a security risk. Also, the host
, port
and use_ssl
parameters are important for SSL encryption to work properly. Simply using your BONSAI_URL
as the host will not work because of limitations in urllib. It needs to be a plain URL, with the credentials passed to the client as a tuple.