Examples

edit

Using your application to proxy/filter elasticsearch requests can sometimes be a good idea. Use this configuration to always send requests to https://my-site.com/elasticsearch/{{request url}} rather than directly to elasticsearch.

var client = new elasticsearch.Client({
  host: {
    protocol: 'https',
    host: 'my-site.com',
    port: 80,
    path: '/elasticsearch/'
  }
})

Use custom keys/vals to add special properties to the hosts, that are used by the selector.

var client = new elasticsearch.Client({
  hosts: [
    {
      protocol: 'https',
      host: 'box1.server.org',
      port: 56394,
      // these custom values are used below by the selector
      country: 'EU',
      weight: 10
    },
    {
      protocol: 'https',
      host: 'box2.server.org',
      port: 56394,
      // these custom values are used below by the selector
      country: 'US',
      weight: 50
    }
  ],
  selector: function (nodes) {
    var myCountry = process.env.COUNTRY;
    // first try to find a node that is in the same country
    var selection = _.find(nodes, function (node) {
      return node.host.country === myCountry;
    });

    if (!selection) {
      // choose the node with the lightest weight.
      selection = _(nodes).sortBy(function (node) {
        return node.host.weight;
      }).first();
    }

    return selection;
  }
});