You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
-
7
5
## Features
8
6
9
7
- 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)
11
9
- Configurable, automatic discovery of cluster nodes
12
10
- 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.
15
12
13
+
## Node and the browser
16
14
15
+
elasticsearch.js works great in node, as well as modern browsers (many thanks to [browserify](https://github.com/substack/browserify)!!).
- 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 =newelasticsearch.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:detailloggingwhy) 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).
41
174
42
175
## API
43
176
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
45
202
46
203
#### create the client
47
-
```
204
+
```js
48
205
var es = new elasticsearch.Client({
49
206
hosts: [
50
207
'localhost:9200'
@@ -55,7 +212,7 @@ var es = new elasticsearch.Client({
55
212
```
56
213
57
214
#### call an endpoint
58
-
```
215
+
```js
59
216
es.cluster.nodeInfo({
60
217
clear:true,
61
218
jvm:true,
@@ -66,7 +223,7 @@ es.cluster.nodeInfo({
66
223
```
67
224
68
225
#### skip the callback to get a promise back
69
-
```
226
+
```js
70
227
es.search({
71
228
q:'pants'
72
229
}).then(function (resp) {
@@ -77,7 +234,7 @@ es.search({
77
234
```
78
235
79
236
#### abort a request
80
-
```
237
+
```js
81
238
var req =es.search({
82
239
q:'robots'
83
240
}, function (err, body, status) {
@@ -91,11 +248,18 @@ var timeout = setTimeout(function () {
91
248
```
92
249
93
250
#### or just use the timeout param
94
-
```
251
+
```js
95
252
es.search({
96
253
q:'*',
97
254
timeout:200
98
255
}).then(function (resp) {
99
256
// Iterate all the hits
100
257
})
101
258
```
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.
0 commit comments