Files
elasticsearch-js/docs/host.asciidoc
2017-04-25 17:39:06 -07:00

106 lines
2.8 KiB
Plaintext

[[host-reference]]
== Host
The host class is used to represent a single node/host/endpoint. These objects are created automatically by the Client constructor, as well as during sniff operations.
=== Constructor(params)
==== Params
Params can either be URL string, or an object with the following properties
[horizontal]
`host`::
`String` -- The name of the host
Default ::: `"localhost"`
`port`::
`Number` -- The port number to use for this host. Note the default of 9200 is only true if the params are passed as an object. If a string, then default port is evaluated based on the protocol (`80` for http, `443` for https).
Default ::: `9200`
`protocol`::
`String` -- The name of the protocol this host is reachable on
Default ::: `"http"`
Options :::
* `"http"`
* `"https"`
`path`::
`String` -- A path prefix that should be prepended to every path requested.
`auth`::
`String` -- Basic authentication i.e. 'user:password' to compute an Authorization header.
`query`::
`String,Object` -- A default set of query string parameters to use on every request.
`headers`::
`Object` -- An object describing the headers to send for every request to this node.
`...`::
`Any` -- When the host receives a configuration object, it assigns all of the object's keys to itself. This allows you to pass in arbitrary keys and values that can be used within selectors, or other custom functionality.
`ssl`::
`Object` -- Host specific version of the <<config-ssl,ssl config>>.
=== Examples
.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.
[source,js]
-----
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.
[source,js]
-----
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;
}
});
-----