{"_id":"56b3cd7ac25dea2b00b0caf6","user":"5637d336aa96490d00a64f81","__v":1,"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":1,"slug":"quickstart-guides-1","title":"Quickstart Guides"},"parentDoc":null,"project":"5633ebff7e9e880d00af1a53","version":{"_id":"5633ec007e9e880d00af1a56","project":"5633ebff7e9e880d00af1a53","__v":16,"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","5a062c15a66ae1001a3f5b09"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-02-04T22:15:22.014Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":4,"body":"Bonsai Elasticsearch integrates with your node.js app quickly and easily, whether you're running on Heroku or self hosting. \n\nFirst, make sure to add the Elasticsearch.js client to your dependencies list:\n\n_package.json:_\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"      \\\"dependencies\\\": {\\n        \\\"elasticsearch\\\": \\\"10.1.2\\\"\\n        ....\",\n      \"language\": \"json\"\n    }\n  ]\n}\n[/block]\nYou'll also want to make sure the client is installed locally for testing purposes:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$ npm install --save elasticsearch\\n$ npm install\",\n      \"language\": \"shell\"\n    }\n  ]\n}\n[/block]\n*Self-hosted users:* when you sign up with Bonsai and create a cluster, it will be provisioned with a custom URL that looks like https://user:pass:::at:::my-awesome-cluster.us-east-1.bonsai.io. Make note of this URL because it will be needed momentarily.\n\n*Heroku users:* you'll want to make sure that you've added Bonsai Elasticsearch to your app. Visit our addons page to see available plans. You can add Bonsai to your app through the dashboard, or by running:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$ heroku addons:create bonsai:<plan_slug> -a <app_name>\",\n      \"language\": \"shell\"\n    }\n  ]\n}\n[/block]\nUpdate your index.js file with the following:\n\n_index.js:_\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"    var bonsai_url    = process.env.BONSAI_URL;\\n    var elasticsearch = require('elasticsearch');\\n    var client        = new elasticsearch.Client({\\n                                host: bonsai_url,\\n                                log: 'trace' \\n                            });\\n    \\n    // Test the connection:\\n    // Send a HEAD request to \\\"/\\\" and allow\\n    // up to 30 seconds for it to complete.\\n    client.ping({\\n      requestTimeout: 30000,\\n    }, function (error) {\\n      if (error) {\\n        console.error('elasticsearch cluster is down!');\\n      } else {\\n        console.log('All is well');\\n      }\\n    });\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThe code above does several things:\n\n1. Pulls your Bonsai URL from the environment (you never want to hard-code this value -- it's a bad practice)\n2. Adds the Elasticsearch.js library\n3. Instantiates the client using your private Bonsai URL\n4. Pings the cluster to test the connection\n\n*Self-hosted users:* You will need to take an additional step and add your Bonsai URL to your server/dev environment. Something like this should work:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$ export BONSAI_URL=\\\"https://username:password@my-awesome-cluster-123.us-east-1.bonsai.io\\\"\",\n      \"language\": \"shell\"\n    }\n  ]\n}\n[/block]\n*Heroku users:* You don't need to worry about this step. The environment variable is automatically populated for you when you add Bonsai Elasticsearch to your app. You can verify that it exists by running:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$ heroku config:get BONSAI_URL\",\n      \"language\": \"shell\"\n    }\n  ]\n}\n[/block]\nGo ahead and spin up your node app with `npm start`. Heroku users can also push the changes to Heroku, and monitor the logs with `heroku logs --tail`. Either way, you should see something like this:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"    Elasticsearch INFO: 2016-02-03T23:44:41Z\\n      Adding connection to https://nodejs-test-012345.us-east-1.bonsai.io/\\n    \\n    Elasticsearch DEBUG: 2016-02-03T23:44:41Z\\n      Request complete\\n    \\n    Elasticsearch TRACE: 2016-02-03T23:44:41Z\\n      -> HEAD https://nodejs-test-012345.us-east-1.bonsai.io:443/?hello=elasticsearch\\n      \\n      <- 200\",\n      \"language\": \"shell\"\n    }\n  ]\n}\n[/block]\nThe above output indicates that the client was successfully initiated and was able to contact the cluster.\n\n*Questions? Problems?*\nFeel free to [ping our support](mailto:support@bonsai.io) if something isn't working right and we'll do what we can to help out.","excerpt":"","slug":"nodejs","type":"basic","title":"Node.js"}
Bonsai Elasticsearch integrates with your node.js app quickly and easily, whether you're running on Heroku or self hosting. First, make sure to add the Elasticsearch.js client to your dependencies list: _package.json:_ [block:code] { "codes": [ { "code": " \"dependencies\": {\n \"elasticsearch\": \"10.1.2\"\n ....", "language": "json" } ] } [/block] You'll also want to make sure the client is installed locally for testing purposes: [block:code] { "codes": [ { "code": "$ npm install --save elasticsearch\n$ npm install", "language": "shell" } ] } [/block] *Self-hosted users:* when you sign up with Bonsai and create a cluster, it will be provisioned with a custom URL that looks like https://user:pass@my-awesome-cluster.us-east-1.bonsai.io. Make note of this URL because it will be needed momentarily. *Heroku users:* you'll want to make sure that you've added Bonsai Elasticsearch to your app. Visit our addons page to see available plans. You can add Bonsai to your app through the dashboard, or by running: [block:code] { "codes": [ { "code": "$ heroku addons:create bonsai:<plan_slug> -a <app_name>", "language": "shell" } ] } [/block] Update your index.js file with the following: _index.js:_ [block:code] { "codes": [ { "code": " var bonsai_url = process.env.BONSAI_URL;\n var elasticsearch = require('elasticsearch');\n var client = new elasticsearch.Client({\n host: bonsai_url,\n log: 'trace' \n });\n \n // Test the connection:\n // Send a HEAD request to \"/\" and allow\n // up to 30 seconds for it to complete.\n client.ping({\n requestTimeout: 30000,\n }, function (error) {\n if (error) {\n console.error('elasticsearch cluster is down!');\n } else {\n console.log('All is well');\n }\n });", "language": "javascript" } ] } [/block] The code above does several things: 1. Pulls your Bonsai URL from the environment (you never want to hard-code this value -- it's a bad practice) 2. Adds the Elasticsearch.js library 3. Instantiates the client using your private Bonsai URL 4. Pings the cluster to test the connection *Self-hosted users:* You will need to take an additional step and add your Bonsai URL to your server/dev environment. Something like this should work: [block:code] { "codes": [ { "code": "$ export BONSAI_URL=\"https://username:password@my-awesome-cluster-123.us-east-1.bonsai.io\"", "language": "shell" } ] } [/block] *Heroku users:* You don't need to worry about this step. The environment variable is automatically populated for you when you add Bonsai Elasticsearch to your app. You can verify that it exists by running: [block:code] { "codes": [ { "code": "$ heroku config:get BONSAI_URL", "language": "shell" } ] } [/block] Go ahead and spin up your node app with `npm start`. Heroku users can also push the changes to Heroku, and monitor the logs with `heroku logs --tail`. Either way, you should see something like this: [block:code] { "codes": [ { "code": " Elasticsearch INFO: 2016-02-03T23:44:41Z\n Adding connection to https://nodejs-test-012345.us-east-1.bonsai.io/\n \n Elasticsearch DEBUG: 2016-02-03T23:44:41Z\n Request complete\n \n Elasticsearch TRACE: 2016-02-03T23:44:41Z\n -> HEAD https://nodejs-test-012345.us-east-1.bonsai.io:443/?hello=elasticsearch\n \n <- 200", "language": "shell" } ] } [/block] The above output indicates that the client was successfully initiated and was able to contact the cluster. *Questions? Problems?* Feel free to [ping our support](mailto:support@bonsai.io) if something isn't working right and we'll do what we can to help out.