{"_id":"567af41fc952030d0038c782","project":"5633ebff7e9e880d00af1a53","version":{"_id":"5633ec007e9e880d00af1a56","project":"5633ebff7e9e880d00af1a53","__v":15,"createdAt":"2015-10-30T22:15:28.105Z","releaseDate":"2015-10-30T22:15:28.105Z","categories":["5633ec007e9e880d00af1a57","5633f072737ea01700ea329d","5637a37d0704070d00f06cf4","5637cf4e7ca5de0d00286aeb","564503082c74cf1900da48b4","564503cb7f1fff210078e70a","567af26cb56bac0d0019d87d","567afeb8802b2b17005ddea0","567aff47802b2b17005ddea1","567b0005802b2b17005ddea3","568adfffcbd4ca0d00aebf7e","56ba80078cf7c9210009673e","574d127f6f075519007da3d0","574fde60aef76a0e00840927","57a22ba6cd51b22d00f623a0"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"parentDoc":null,"__v":2,"category":{"_id":"567afeb8802b2b17005ddea0","project":"5633ebff7e9e880d00af1a53","pages":["56b3cd7ac25dea2b00b0caf6"],"version":"5633ec007e9e880d00af1a56","__v":1,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-12-23T20:06:16.013Z","from_sync":false,"order":2,"slug":"quickstart-guides-1","title":"Quickstart Guides"},"user":"5633ec9b35355017003ca3f2","updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-12-23T19:21:03.323Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"Setting up your Python app to use Bonsai Elasticsearch is quick and easy. Just follow these simple steps:\n\n\n## Adding the Elasticsearch library\n\nYou'll need to add the elasticsearch-py library to your `requirements.txt` file like so:\n\n`elasticsearch>=1.0.0,<2.0.0`\n\nWhen you push the change to Heroku, it should be automatically detected and installed.\n\n\n## Connecting to Bonsai\n\nBonsai 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:\n\n ```python\nimport os, base64, re, logging\nfrom elasticsearch import Elasticsearch\n\n# Log transport details (optional):\nlogging.basicConfig(level=logging.INFO)\n\n# Parse the auth and host from env:\nbonsai = os.environ['BONSAI_URL']\nauth = re.search('https\\:\\/\\/(.*)\\:::at:::', bonsai).group(1).split(':')\nhost = bonsai.replace('https://%s:%s@' % (auth[0], auth[1]), '')\n\n# Connect to cluster over SSL using auth for best security:\nes_header = [{\n  'host': host,\n  'port': 443,\n  'use_ssl': True,\n  'http_auth': (auth[0],auth[1])\n}]\n\n# Instantiate the new Elasticsearch connection:\nes = Elasticsearch(es_header)\n\n# Verify that Python can talk to Bonsai (optional):\nes.ping()\n```\n\nFeel 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.\n\nFor 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.","excerpt":"","slug":"python","type":"basic","title":"Python"}
Setting up your Python app to use Bonsai Elasticsearch is quick and easy. Just follow these simple steps: ## Adding the Elasticsearch library You'll need to add the elasticsearch-py library to your `requirements.txt` file like so: `elasticsearch>=1.0.0,<2.0.0` When you push the change to Heroku, it should be automatically detected and installed. ## 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: ```python 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]), '') # Connect to cluster over SSL using auth for best security: es_header = [{ 'host': host, 'port': 443, '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.