1
+ [[client-connecting]]
2
+ == Connecting
3
+
4
+ This page contains the information you need to connect and use the Client with
5
+ {es}.
6
+
7
+ **On this page**
8
+
9
+ * <<auth-reference, Authentication options>>
10
+ * <<client-usage, Using the client>>
11
+ * <<client-connect-proxy, Connecting through a proxy>>
12
+ * <<client-error-handling, Handling errors>>
13
+
14
+ [discrete]
15
+ [[authentication]]
16
+ === Authentication
17
+
18
+ This document contains code snippets to show you how to connect to various {es}
19
+ providers.
20
+
21
+
22
+ [discrete]
23
+ [[auth-ec]]
24
+ ==== Elastic Cloud
25
+
26
+ If you are using https://www.elastic.co/cloud[Elastic Cloud], the client offers
27
+ an easy way to connect to it via the `cloud` option. You must pass the Cloud ID
28
+ that you can find in the cloud console, then your username and password inside
29
+ the `auth` option.
30
+
31
+ NOTE: When connecting to Elastic Cloud, the client will automatically enable
32
+ both request and response compression by default, since it yields significant
33
+ throughput improvements. Moreover, the client will also set the ssl option
34
+ `secureProtocol` to `TLSv1_2_method` unless specified otherwise. You can still
35
+ override this option by configuring them.
36
+
37
+ IMPORTANT: Do not enable sniffing when using Elastic Cloud, since the nodes are
38
+ behind a load balancer, Elastic Cloud will take care of everything for you.
39
+ Take a look https://www.elastic.co/blog/elasticsearch-sniffing-best-practices-what-when-why-how[here]
40
+ to know more.
41
+
42
+ [source,js]
43
+ ----
44
+ const { Client } = require('@elastic/elasticsearch')
45
+ const client = new Client({
46
+ cloud: {
47
+ id: 'name:bG9jYWxob3N0JGFiY2QkZWZnaA==',
48
+ },
49
+ auth: {
50
+ username: 'elastic',
51
+ password: 'changeme'
52
+ }
53
+ })
54
+ ----
55
+
56
+
57
+ [discrete]
58
+ [[auth-apikey]]
59
+ ==== ApiKey authentication
60
+
61
+ You can use the
62
+ https://www.elastic.co/guide/en/elasticsearch/reference/7.x/security-api-create-api-key.html[ApiKey]
63
+ authentication by passing the `apiKey` parameter via the `auth` option. The
64
+ `apiKey` parameter can be either a base64 encoded string or an object with the
65
+ values that you can obtain from the
66
+ https://www.elastic.co/guide/en/elasticsearch/reference/7.x/security-api-create-api-key.html[create api key endpoint].
67
+
68
+ NOTE: If you provide both basic authentication credentials and the ApiKey
69
+ configuration, the ApiKey takes precedence.
70
+
71
+ [source,js]
72
+ ----
73
+ const { Client } = require('@elastic/elasticsearch')
74
+ const client = new Client({
75
+ node: 'https://localhost:9200',
76
+ auth: {
77
+ apiKey: 'base64EncodedKey'
78
+ }
79
+ })
80
+ ----
81
+
82
+ [source,js]
83
+ ----
84
+ const { Client } = require('@elastic/elasticsearch')
85
+ const client = new Client({
86
+ node: 'https://localhost:9200',
87
+ auth: {
88
+ apiKey: {
89
+ id: 'foo',
90
+ api_key: 'bar'
91
+ }
92
+ }
93
+ })
94
+ ----
95
+
96
+
97
+ [discrete]
98
+ [[auth-basic]]
99
+ ==== Basic authentication
100
+
101
+ You can provide your credentials by passing the `username` and `password`
102
+ parameters via the `auth` option.
103
+
104
+ NOTE: If you provide both basic authentication credentials and the Api Key
105
+ configuration, the Api Key will take precedence.
106
+
107
+ [source,js]
108
+ ----
109
+ const { Client } = require('@elastic/elasticsearch')
110
+ const client = new Client({
111
+ node: 'https://localhost:9200',
112
+ auth: {
113
+ username: 'elastic',
114
+ password: 'changeme'
115
+ }
116
+ })
117
+ ----
118
+
119
+
120
+ Otherwise, you can provide your credentials in the node(s) URL.
121
+
122
+ [source,js]
123
+ ----
124
+ const { Client } = require('@elastic/elasticsearch')
125
+ const client = new Client({
126
+ node: 'https://username:password@localhost:9200'
127
+ })
128
+ ----
129
+
130
+
131
+ [discrete]
132
+ [[auth-ssl]]
133
+ ==== SSL configuration
134
+
135
+ Without any additional configuration you can specify `https://` node urls, and
136
+ the certificates used to sign these requests will be verified. To turn off
137
+ certificate verification, you must specify an `ssl` object in the top level
138
+ config and set `rejectUnauthorized: false`. The default `ssl` values are the
139
+ same that Node.js's
140
+ https://nodejs.org/api/tls.html#tls_tls_connect_options_callback[`tls.connect()`]
141
+ uses.
142
+
143
+ [source,js]
144
+ ----
145
+ const { Client } = require('@elastic/elasticsearch')
146
+ const client = new Client({
147
+ node: 'https://localhost:9200',
148
+ auth: {
149
+ username: 'elastic',
150
+ password: 'changeme'
151
+ },
152
+ ssl: {
153
+ ca: fs.readFileSync('./cacert.pem'),
154
+ rejectUnauthorized: false
155
+ }
156
+ })
157
+ ----
158
+
159
+ [discrete]
1
160
[[client-usage]]
2
- == Usage
161
+ === Usage
3
162
4
163
Using the client is straightforward, it supports all the public APIs of {es},
5
164
and every method exposes the same signature.
@@ -33,7 +192,7 @@ client.search({
33
192
})
34
193
----
35
194
36
- The returned value of every API call is formed as follows:
195
+ The returned value of every API call is designed as follows:
37
196
38
197
[source,ts]
39
198
----
@@ -82,11 +241,13 @@ client.search({
82
241
83
242
84
243
[discrete]
85
- === Aborting a request
244
+ ==== Aborting a request
86
245
87
- If needed, you can abort a running request by calling the `request.abort()` method returned by the API.
246
+ If needed, you can abort a running request by calling the `request.abort()`
247
+ method returned by the API.
88
248
89
- CAUTION: If you abort a request, the request will fail with a `RequestAbortedError`.
249
+ CAUTION: If you abort a request, the request will fail with a
250
+ `RequestAbortedError`.
90
251
91
252
92
253
[source,js]
@@ -113,6 +274,7 @@ request.abort()
113
274
----
114
275
115
276
The same behavior is valid for the promise style API as well.
277
+
116
278
[source,js]
117
279
----
118
280
const request = client.search({
@@ -136,7 +298,8 @@ request.abort()
136
298
137
299
138
300
[discrete]
139
- === Request specific options
301
+ ==== Request specific options
302
+
140
303
If needed you can pass request specific options in a second object:
141
304
142
305
[source,js]
@@ -214,13 +377,14 @@ _Default:_ `null`
214
377
215
378
216
379
[discrete]
380
+ [[client-connect-proxy]]
217
381
=== Connecting through a proxy
218
382
219
383
~Added~ ~in~ ~`v7.10.0`~
220
384
221
- If you need to pass through an http(s) proxy for connecting to Elasticsearch , the client offers
222
- out of the box a handy configuration for helping you with it. Under the hood it
223
- uses the https://github.com/delvedor/hpagent[`hpagent`] module.
385
+ If you need to pass through an http(s) proxy for connecting to {es} , the client
386
+ offers out of the box a handy configuration for helping you with it. Under the
387
+ hood, it uses the https://github.com/delvedor/hpagent[`hpagent`] module.
224
388
225
389
[source,js]
226
390
----
@@ -256,6 +420,7 @@ const client = new Client({
256
420
257
421
258
422
[discrete]
423
+ [[client-error-handling]]
259
424
=== Error handling
260
425
261
426
The client exposes a variety of error objects that you can use to enhance your
0 commit comments