IMPORTANT: elasticsearch.js has been replaced by the new Elasticsearch JavaScript client. We strongly advise you to migrate to the new client. To learn more, see the migration guide.
Examples
edit
IMPORTANT: This documentation is no longer updated. Refer to Elastic's version policy and the latest documentation.
Examples
editUsing 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;
}
});