Skip to content

Commit dfb09b4

Browse files
szabostevedelvedor
authored andcommitted
[7.x][DOCS] Adds Connecting section to Node.JS docs (#1344)
1 parent 11c11e4 commit dfb09b4

File tree

6 files changed

+223
-183
lines changed

6 files changed

+223
-183
lines changed

docs/authentication.asciidoc

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

docs/usage.asciidoc renamed to docs/connecting.asciidoc

Lines changed: 174 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,164 @@
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]
1160
[[client-usage]]
2-
== Usage
161+
=== Usage
3162

4163
Using the client is straightforward, it supports all the public APIs of {es},
5164
and every method exposes the same signature.
@@ -33,7 +192,7 @@ client.search({
33192
})
34193
----
35194

36-
The returned value of every API call is formed as follows:
195+
The returned value of every API call is designed as follows:
37196

38197
[source,ts]
39198
----
@@ -82,11 +241,13 @@ client.search({
82241

83242

84243
[discrete]
85-
=== Aborting a request
244+
==== Aborting a request
86245

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.
88248

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`.
90251

91252

92253
[source,js]
@@ -113,6 +274,7 @@ request.abort()
113274
----
114275

115276
The same behavior is valid for the promise style API as well.
277+
116278
[source,js]
117279
----
118280
const request = client.search({
@@ -136,7 +298,8 @@ request.abort()
136298

137299

138300
[discrete]
139-
=== Request specific options
301+
==== Request specific options
302+
140303
If needed you can pass request specific options in a second object:
141304

142305
[source,js]
@@ -214,13 +377,14 @@ _Default:_ `null`
214377

215378

216379
[discrete]
380+
[[client-connect-proxy]]
217381
=== Connecting through a proxy
218382

219383
~Added~ ~in~ ~`v7.10.0`~
220384

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.
224388

225389
[source,js]
226390
----
@@ -256,6 +420,7 @@ const client = new Client({
256420

257421

258422
[discrete]
423+
[[client-error-handling]]
259424
=== Error handling
260425

261426
The client exposes a variety of error objects that you can use to enhance your

docs/index.asciidoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ include::{asciidoc-dir}/../../shared/attributes.asciidoc[]
55

66
include::introduction.asciidoc[]
77
include::installation.asciidoc[]
8+
include::connecting.asciidoc[]
89
include::changelog.asciidoc[]
9-
include::usage.asciidoc[]
1010
include::configuration.asciidoc[]
1111
include::reference.asciidoc[]
1212
include::breaking-changes.asciidoc[]
13-
include::authentication.asciidoc[]
1413
include::observability.asciidoc[]
1514
include::child.asciidoc[]
1615
include::extend.asciidoc[]
1716
include::helpers.asciidoc[]
1817
include::typescript.asciidoc[]
1918
include::testing.asciidoc[]
2019
include::examples/index.asciidoc[]
20+
include::redirects.asciidoc[]

0 commit comments

Comments
 (0)