Skip to content

Commit e68eeab

Browse files
author
Spencer Alger
committed
extended the README.md [ci skip]
1 parent 28e99cf commit e68eeab

File tree

7 files changed

+199
-102
lines changed

7 files changed

+199
-102
lines changed

README.md

Lines changed: 186 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22

33
Official *low-level* client for Elasticsearch.
44

5-
This project's goal it to give the JavaScript community a solif foundation for all Elasticsearch-related code. It features a complete API, provides a module for use in Node.js as well as several different builds for use in the browser. We have tried to be opinion-free and very plugable.
6-
75
## Features
86

97
- One-to-one mapping with REST API and other language clients
10-
- Generalized, pluggable architecture. See [replacing core components](docs/replacing-core-components.md)
8+
- Generalized, pluggable architecture. See [replacing core components](TODO: details the peices that are replaceable)
119
- Configurable, automatic discovery of cluster nodes
1210
- Persistent, Keep-Alive connections
13-
- Load balancing (with pluggable selection strategy) across all availible nodes. Defaults to round-robin
14-
- Pluggable connection pools to offer different connection strategies
11+
- Load balancing (with pluggable selection strategy) across all availible nodes.
1512

13+
## Node and the browser
1614

15+
elasticsearch.js works great in node, as well as modern browsers (many thanks to [browserify](https://github.com/substack/browserify)!!).
1716

18-
## Node and the browser
17+
- Node:
18+
19+
[![Build Status](https://magnum.travis-ci.com/spenceralger/elasticsearch-js.png?token=tsFxSKHtVKG8EZavSjXY)](https://magnum.travis-ci.com/spenceralger/elasticsearch-js)
20+
21+
- Browsers (see [browser builds](#browser-builds)):
1922

20-
elasticsearch.js works great in node, as well as the browser (many thanks to [browserify](https://github.com/substack/browserify)).
23+
![testling results for browser clients](https://ci.testling.com/spenceralger/xhr-method-test.png)
2124

22-
- Node: [![Build Status](https://magnum.travis-ci.com/spenceralger/elasticsearch-js.png?token=tsFxSKHtVKG8EZavSjXY)](https://magnum.travis-ci.com/spenceralger/elasticsearch-js)
23-
- Browsers:
24-
+ ![testling results for browser clients](https://ci.testling.com/spenceralger/xhr-method-test.png)
2525

2626
## Install in Node
2727

@@ -30,21 +30,178 @@ npm install --save elasticsearch
3030
```
3131

3232
## Browser Builds
33-
Download one of these builds:
3433

35-
- [elasticsearch.js](dist/elasticsearch.js) - [minified](dist/elasticsearch.min.js)
36-
- uses the browser's native XHR object
37-
- Node style callbacks with a bare-bones `.then()` method
38-
- [elasticsearch.angular.js](dist/elasticsearch.angular.js) - [minified](dist/elasticsearch.angular.min.js)
34+
Download one of these browser-ready builds, or install them with `bower`
35+
36+
- [elasticsearch.js](dist/elasticsearch.min.js) - [dev](dist/elasticsearch.js)
37+
- uses the browser's native XMLHttpRequest object
38+
- Fully Compatible with IE 10+, Chrome, Firefox, Safari, Opera
39+
- Only GET and POST requests available in IE 8 & 9
40+
- Node style callbacks or promises provided by [when.js](https://github.com/cujojs/when)
41+
42+
```
43+
bower install elasticsearch
44+
```
45+
46+
- [elasticsearch.angular.js](dist/elasticsearch.angular.min.js) - [dev](dist/elasticsearch.angular.js)
3947
- Uses angular's $http servive
40-
- returns promisses using angular's $q servive
48+
- Returns promisses using angular's $q servive (Adds an `abort()` method)
49+
50+
```
51+
bower install elasticsearch-angular
52+
```
53+
54+
## Configuration
55+
56+
The `Client` constructor accepts a single object as it's argument, and the following keys can be used to configure that client instance:
57+
58+
```js
59+
var elasticsearch = require('elasticsearch');
60+
var es = new elasticsearch.Client({
61+
...
62+
});
63+
64+
### hosts
65+
Type: `String`, `String[]` or `Object[]`
66+
67+
Default:
68+
```js
69+
hosts: [
70+
{
71+
host: 'localhost', port: '9200', protocol: 'http'
72+
}
73+
]
74+
```
75+
76+
Specify the list of hosts that this client will connect to. If sniffing is enabled, or you call sniff, this list will be used as seeds for discovery of the rest of the cluster.
77+
78+
### log
79+
Type: `String`, `String[]`, `Object`, `Object[]`, or `Constructor`
80+
81+
Default:
82+
```js
83+
log: {
84+
type: 'stdio',
85+
levels: ['error', 'warning']
86+
}
87+
```
88+
89+
Unless a constructor is specified, this sets the output settings for the bundled logger. See [setting up logging](TODO: detail logging why) for more information.
90+
91+
### connectionClass
92+
Type: `String`, `Constructor`
93+
94+
Default:
95+
- Node: `'http'`
96+
- Browser: `'xhr'`
97+
- Angular Build: `'angular'`
98+
99+
Options:
100+
- Node: `'http'`
101+
- Browser: based on bundle, `'xhr'`, `'angular'`, and `'jquery'` are currently available
102+
103+
Defines the class that will be created once for each node/host that the client communicates with. If you are looking to implement a special protocol you will probably start by writing a Connection class and specifying it here.
104+
105+
### selector
106+
Type: `String`, `Function`
107+
108+
Default: `'roundRobin'`
109+
110+
Options:
111+
- `'roundRobin'`
112+
- `'random'`
113+
114+
Defined a function that will be used to select a connection from the ConnectionPool. It should received a single argument, the list of "active" connections, and return the connection to use. Use this selector to implement special logic for your client such as prefering connections in a certain rack, or datacenter.
115+
116+
To make this function asynchronous, accept a second argument which will be the callback which should be called as a Node style callback with a possible error: `cb(err, selectedConnection)`.
117+
118+
### sniffOnStart
119+
Type: `Boolean`
120+
121+
Default: `false`
122+
123+
Should the client attempt to detect the rest of the cluster when it is first instanciated?
124+
125+
### sniffAfterRequests
126+
Type: `Number` or `false`
127+
128+
Default: `false`
129+
130+
After `n` requests, perform a sniff operation and ensure out list of nodes is up to date
131+
132+
133+
### sniffOnConnectionFail
134+
Type: `Boolean`
135+
136+
Default: `false`
137+
138+
Should the client immediately sniff for a more current list of nodes when a connection dies? (see [node death](#node-death))
139+
140+
### maxRetries
141+
Type: `Number`
142+
143+
Defailt: `3`
144+
145+
How many times should the client try to connect to other nodes before returning a [ConnectionFault](TODO: error types) error. (see [node death](#node-death))
146+
147+
### timeout
148+
Type: `Number`
149+
150+
Default: 10000
151+
152+
How many milliseconds can the connection take before the request is aboorted and retried. (TODO: timeout errors shouldn't cause a retry).
153+
154+
### deadTimeout
155+
Type: `Number`
156+
157+
Default: 30000
158+
159+
How many milliseconds should a dead a connection/node sit and wait before it is ping-ed? (see [node death](#node-death))
160+
161+
### maxSockets
162+
Type: `Number`
163+
164+
Default: 10
165+
166+
How many sockets should a connection/node keep to the server? These sockets are currently kept alive ***forever*** (not like nodes current "keep alive" sockets).
167+
168+
### nodesToHostCallback
169+
Type: `Function`
170+
171+
Default: simple, not much going on [here](src/lib/client_config.js#L65).
172+
173+
This function will receive a list of nodes received durring a sniff. The list of nodes should be transformed into an array of objects which will be fed to the [Host](src/lib/host.js) class. (TODO: allow this function to be async).
41174

42175
## API
43176

44-
To maintain consistency across all the low-level clients (Ruby, Python, etc), clients accept all of their parameters via a single object, along with a single callback.
177+
To maintain consistency across all the low-level clients ([PHP](https://github.com/elasticsearch/elasticsearch-php), [Python](https://github.com/elasticsearch/elasticsearch-ph), [Ruby](https://github.com/elasticsearch/elasticsearch-ruby), [Perl](https://github.com/elasticsearch/elasticsearch-perl)), all API methods accept an object with parameters and a callback. If you don't pass the callback, the functions will return a promise.
178+
179+
### Generic Params
180+
181+
Several parameters work on all API methods, and control the way that those requests are carried out:
182+
183+
### ignore
184+
Type: `Number` or `Number[]`
185+
186+
Default: `null`
187+
188+
Don't treat these HTTP status codes as "errors". Example use cases could be `ignore: 404` or `ignore: [404]`
189+
190+
### timeout
191+
Type: `Number`
192+
193+
Default: `client.config.timeout`
194+
195+
The number of milliseconds this request has to complete. It defaults to the timeout specified at the client level, which defaults to 10 seconds.
196+
197+
### Methods
198+
199+
All the methods can be seen [here](TODO: api docs), or take a look at [api.js](src/lib/api.js).
200+
201+
### Examples
45202

46203
#### create the client
47-
```
204+
```js
48205
var es = new elasticsearch.Client({
49206
hosts: [
50207
'localhost:9200'
@@ -55,7 +212,7 @@ var es = new elasticsearch.Client({
55212
```
56213

57214
#### call an endpoint
58-
```
215+
```js
59216
es.cluster.nodeInfo({
60217
clear: true,
61218
jvm: true,
@@ -66,7 +223,7 @@ es.cluster.nodeInfo({
66223
```
67224

68225
#### skip the callback to get a promise back
69-
```
226+
```js
70227
es.search({
71228
q: 'pants'
72229
}).then(function (resp) {
@@ -77,7 +234,7 @@ es.search({
77234
```
78235

79236
#### abort a request
80-
```
237+
```js
81238
var req = es.search({
82239
q: 'robots'
83240
}, function (err, body, status) {
@@ -91,11 +248,18 @@ var timeout = setTimeout(function () {
91248
```
92249

93250
#### or just use the timeout param
94-
```
251+
```js
95252
es.search({
96253
q: '*',
97254
timeout: 200
98255
}).then(function (resp) {
99256
// Iterate all the hits
100257
})
101258
```
259+
260+
## FAQ
261+
262+
### dead nodes
263+
Q: When is a connection/node considered dead?
264+
265+
A; A connection is considered dead when a request to it does not complete properly. If the server responds with any status, even 500, it is not considered dead.

docs/replacing-core-components.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

scripts/grunt-run-wait-stop.js

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/lib/client.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ Client.prototype.ping = function (params, cb) {
6868

6969
this.config.transport.request({
7070
method: 'HEAD',
71-
path: '/'
71+
path: '/',
72+
timeout: 100,
7273
}, cb);
7374
};
7475

src/lib/connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ ConnectionAbstract.prototype.ping = function (params, cb) {
4545
return this.request({
4646
path: '/',
4747
method: 'HEAD',
48-
timeout: '100'
48+
timeout: 100
4949
}, cb);
5050
};
5151

src/lib/errors.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ errors.ConnectionFault = function ConnectionFault(msg) {
2222
};
2323
_.inherits(errors.ConnectionFault, ErrorAbstract);
2424

25+
/**
26+
* No Living Connections
27+
* @param {String} [msg] - An error message that will probably end up in a log.
28+
*/
29+
errors.NoConnections = function NoConnections(msg) {
30+
ErrorAbstract.call(this, msg || 'No Living connections', errors.NoConnections);
31+
};
32+
_.inherits(errors.NoConnections, ErrorAbstract);
33+
2534
/**
2635
* Generic Error
2736
* @param {String} [msg] - An error message that will probably end up in a log.

src/lib/transport_request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ TransportRequest.prototype._sendReqWithCon = _.handler(function (err, con) {
6464
this._request = con.request(this._params.req, this.bound._checkRespForFail);
6565
} else {
6666
this._log.warning('No living connections');
67-
this._respond(new errors.ConnectionFault('No living connections.'));
67+
this._respond(new errors.NoConnections());
6868
}
6969
});
7070

0 commit comments

Comments
 (0)