Bonsai Elasticsearch integrates with your .Net app quickly and easily, whether you’re running on Heroku or self hosting.

First, make sure to add the Elasticsearch.Net and NEST client to your dependencies list:

<PackageReference Include="Elasticsearch.Net" Version="6.0.2" />
<PackageReference Include="NEST" Version="6.0.2" />

You’ll also want to make sure the client is installed locally for testing purposes:

$ dotnet restore

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:

$ heroku addons:create bonsai:<plan_slug> -a <app_name>

Update your Main.cs file with the following:

using System;
using Nest;

namespace search.con
    class Program
        static void Main(string[] args)
            var server = new Uri(Environment.GetEnvironmentVariable("BONSAI_URL"));
            var conn = new ConnectionSettings(server);
            // use gzip to reduce network bandwidth
            // lets the HttpConnection control this for optimal access (default: 80)
            var client = new ElasticClient(conn);

            var resp = client.Ping();
            if (resp.IsValid)
                Console.WriteLine("All is well");
                Console.WriteLine("Elasticsearch cluster is down");

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. References the Elasticsearch.Net and NEST libraries
  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 variables. Something like this should work:

$ export BONSAI_URL="https://username:password@my-awesome-cluster-123.us-east-1.bonsai.io"

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:

$ heroku config:get BONSAI_URL

Go ahead and spin up your .Net app with dotnet run. 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:

Elasticsearch INFO: 2016-02-03T23:44:41Z
  Adding connection to https://nodejs-test-012345.us-east-1.bonsai.io/

Elasticsearch DEBUG: 2016-02-03T23:44:41Z
  Request complete

Elasticsearch TRACE: 2016-02-03T23:44:41Z
  -> HEAD https://nodejs-test-012345.us-east-1.bonsai.io:443/?hello=elasticsearch

  <- 200

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 if something isn’t working right and we’ll do what we can to help out.