Skip to content

Commit eeacdff

Browse files
authored
[7.x][DOCS] Adds Configuration section to Node.JS docs (#1396)
1 parent 5eab7e9 commit eeacdff

File tree

6 files changed

+382
-373
lines changed

6 files changed

+382
-373
lines changed

docs/advanced-config.asciidoc

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
[[advanced-config]]
2+
=== Advanced configuration
3+
4+
If you need to customize the client behavior heavily, you are in the right
5+
place! The client enables you to customize the following internals:
6+
7+
* `Transport` class
8+
* `ConnectionPool` class
9+
* `Connection` class
10+
* `Serializer` class
11+
12+
13+
[discrete]
14+
==== `Transport`
15+
16+
This class is responsible for performing the request to {es} and handling
17+
errors, it also handles the sniffing.
18+
19+
[source,js]
20+
----
21+
const { Client, Transport } = require('@elastic/elasticsearch')
22+
23+
class MyTransport extends Transport {
24+
request (params, options, callback) {
25+
// your code
26+
}
27+
}
28+
29+
const client = new Client({
30+
Transport: MyTransport
31+
})
32+
----
33+
34+
Sometimes you need to inject a small snippet of your code and then continue to
35+
use the usual client code. In such cases, call `super.method`:
36+
37+
[source,js]
38+
----
39+
class MyTransport extends Transport {
40+
request (params, options, callback) {
41+
// your code
42+
return super.request(params, options, callback)
43+
}
44+
}
45+
----
46+
47+
48+
[discrete]
49+
==== `ConnectionPool`
50+
51+
This class is responsible for keeping in memory all the {es} Connection that you
52+
are using. There is a single Connection for every node. The connection pool
53+
handles the resurrection strategies and the updates of the pool.
54+
55+
[source,js]
56+
----
57+
const { Client, ConnectionPool } = require('@elastic/elasticsearch')
58+
59+
class MyConnectionPool extends ConnectionPool {
60+
markAlive (connection) {
61+
// your code
62+
super.markAlive(connection)
63+
}
64+
}
65+
66+
const client = new Client({
67+
ConnectionPool: MyConnectionPool
68+
})
69+
----
70+
71+
72+
[discrete]
73+
==== `Connection`
74+
75+
This class represents a single node, it holds every information we have on the
76+
node, such as roles, id, URL, custom headers and so on. The actual HTTP request
77+
is performed here, this means that if you want to swap the default HTTP client
78+
(Node.js core), you should override the `request` method of this class.
79+
80+
[source,js]
81+
----
82+
const { Client, Connection } = require('@elastic/elasticsearch')
83+
84+
class MyConnection extends Connection {
85+
request (params, callback) {
86+
// your code
87+
}
88+
}
89+
90+
const client = new Client({
91+
Connection: MyConnection
92+
})
93+
----
94+
95+
96+
[discrete]
97+
==== `Serializer`
98+
99+
This class is responsible for the serialization of every request, it offers the
100+
following methods:
101+
102+
* `serialize(object: any): string;` serializes request objects.
103+
* `deserialize(json: string): any;` deserializes response strings.
104+
* `ndserialize(array: any[]): string;` serializes bulk request objects.
105+
* `qserialize(object: any): string;` serializes request query parameters.
106+
107+
[source,js]
108+
----
109+
const { Client, Serializer } = require('@elastic/elasticsearch')
110+
111+
class MySerializer extends Serializer {
112+
serialize (object) {
113+
// your code
114+
}
115+
}
116+
117+
const client = new Client({
118+
Serializer: MySerializer
119+
})
120+
----

docs/basic-config.asciidoc

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
[[basic-config]]
2+
=== Basic configuration
3+
4+
This page shows you the possible basic configuration options that the clients
5+
offers.
6+
7+
8+
[source,js]
9+
----
10+
const { Client } = require('@elastic/elasticsearch')
11+
12+
const client = new Client({
13+
node: 'http://localhost:9200',
14+
maxRetries: 5,
15+
requestTimeout: 60000,
16+
sniffOnStart: true
17+
})
18+
----
19+
20+
21+
[cols=2*]
22+
|===
23+
|`node` or `nodes`
24+
a|The Elasticsearch endpoint to use. +
25+
It can be a single string or an array of strings:
26+
[source,js]
27+
----
28+
node: 'http://localhost:9200'
29+
----
30+
Or it can be an object (or an array of objects) that represents the node:
31+
[source,js]
32+
----
33+
node: {
34+
url: new URL('http://localhost:9200'),
35+
ssl: 'ssl options',
36+
agent: 'http agent options',
37+
id: 'custom node id',
38+
headers: { 'custom': 'headers' }
39+
roles: {
40+
master: true,
41+
data: true,
42+
ingest: true,
43+
ml: false
44+
}
45+
}
46+
----
47+
48+
|`auth`
49+
a|Your authentication data. You can use both basic authentication and
50+
{ref}/security-api-create-api-key.html[ApiKey]. +
51+
See https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/auth-reference.html[Authentication]
52+
for more details. +
53+
_Default:_ `null`
54+
55+
Basic authentication:
56+
[source,js]
57+
----
58+
auth: {
59+
username: 'elastic',
60+
password: 'changeme'
61+
}
62+
----
63+
{ref}/security-api-create-api-key.html[ApiKey] authentication:
64+
[source,js]
65+
----
66+
auth: {
67+
apiKey: 'base64EncodedKey'
68+
}
69+
----
70+
71+
72+
|`maxRetries`
73+
|`number` - Max number of retries for each request. +
74+
_Default:_ `3`
75+
76+
|`requestTimeout`
77+
|`number` - Max request timeout in milliseconds for each request. +
78+
_Default:_ `30000`
79+
80+
|`pingTimeout`
81+
|`number` - Max ping request timeout in milliseconds for each request. +
82+
_Default:_ `3000`
83+
84+
|`sniffInterval`
85+
|`number, boolean` - Perform a sniff operation every `n` milliseconds. Sniffing might not be the best solution for you, take a look https://www.elastic.co/blog/elasticsearch-sniffing-best-practices-what-when-why-how[here] to know more. +
86+
_Default:_ `false`
87+
88+
|`sniffOnStart`
89+
|`boolean` - Perform a sniff once the client is started. Sniffing might not be the best solution for you, take a look https://www.elastic.co/blog/elasticsearch-sniffing-best-practices-what-when-why-how[here] to know more. +
90+
_Default:_ `false`
91+
92+
|`sniffEndpoint`
93+
|`string` - Endpoint to ping during a sniff. +
94+
_Default:_ `'_nodes/_all/http'`
95+
96+
|`sniffOnConnectionFault`
97+
|`boolean` - Perform a sniff on connection fault. Sniffing might not be the best solution for you, take a look https://www.elastic.co/blog/elasticsearch-sniffing-best-practices-what-when-why-how[here] to know more. +
98+
_Default:_ `false`
99+
100+
|`resurrectStrategy`
101+
|`string` - Configure the node resurrection strategy. +
102+
_Options:_ `'ping'`, `'optimistic'`, `'none'` +
103+
_Default:_ `'ping'`
104+
105+
|`suggestCompression`
106+
|`boolean` - Adds `accept-encoding` header to every request. +
107+
_Default:_ `false`
108+
109+
|`compression`
110+
|`string, boolean` - Enables gzip request body compression. +
111+
_Options:_ `'gzip'`, `false` +
112+
_Default:_ `false`
113+
114+
|`ssl`
115+
|`http.SecureContextOptions` - ssl https://nodejs.org/api/tls.html[configuraton]. +
116+
_Default:_ `null`
117+
118+
|`proxy`
119+
a|`string, URL` - If you are using an http(s) proxy, you can put its url here.
120+
The client will automatically handle the connection to it. +
121+
_Default:_ `null`
122+
[source,js]
123+
----
124+
const client = new Client({
125+
node: 'http://localhost:9200',
126+
proxy: 'http://localhost:8080'
127+
})
128+
129+
// Proxy with basic authentication
130+
const client = new Client({
131+
node: 'http://localhost:9200',
132+
proxy: 'http://user:pwd@localhost:8080'
133+
})
134+
----
135+
136+
|`agent`
137+
a|`http.AgentOptions, function` - http agent https://nodejs.org/api/http.html#http_new_agent_options[options],
138+
or a function that returns an actual http agent instance. If you want to disable the http agent use entirely
139+
(and disable the `keep-alive` feature), set the agent to `false`. +
140+
_Default:_ `null`
141+
[source,js]
142+
----
143+
const client = new Client({
144+
node: 'http://localhost:9200',
145+
agent: { agent: 'options' }
146+
})
147+
148+
const client = new Client({
149+
node: 'http://localhost:9200',
150+
// the function takes as parameter the option
151+
// object passed to the Connection constructor
152+
agent: (opts) => new CustomAgent()
153+
})
154+
155+
const client = new Client({
156+
node: 'http://localhost:9200',
157+
// Disable agent and keep-alive
158+
agent: false
159+
})
160+
----
161+
162+
|`nodeFilter`
163+
a|`function` - Filters which node not to use for a request. +
164+
_Default:_
165+
[source,js]
166+
----
167+
function defaultNodeFilter (node) {
168+
// avoid master only nodes
169+
if (node.roles.master === true &&
170+
node.roles.data === false &&
171+
node.roles.ingest === false) {
172+
return false
173+
}
174+
return true
175+
}
176+
----
177+
178+
|`nodeSelector`
179+
a|`function` - custom selection strategy. +
180+
_Options:_ `'round-robin'`, `'random'`, custom function +
181+
_Default:_ `'round-robin'` +
182+
_Custom function example:_
183+
[source,js]
184+
----
185+
function nodeSelector (connections) {
186+
const index = calculateIndex()
187+
return connections[index]
188+
}
189+
----
190+
191+
|`generateRequestId`
192+
a|`function` - function to generate the request id for every request, it takes
193+
two parameters, the request parameters and options. +
194+
By default it generates an incremental integer for every request. +
195+
_Custom function example:_
196+
[source,js]
197+
----
198+
function generateRequestId (params, options) {
199+
// your id generation logic
200+
// must be syncronous
201+
return 'id'
202+
}
203+
----
204+
205+
|`name`
206+
|`string, symbol` - The name to identify the client instance in the events. +
207+
_Default:_ `elasticsearch-js`
208+
209+
|`opaqueIdPrefix`
210+
|`string` - A string that will be use to prefix any `X-Opaque-Id` header. +
211+
See https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/observability.html#_x-opaque-id_support[`X-Opaque-Id` support] for more details. +
212+
_Default:_ `null`
213+
214+
|`headers`
215+
|`object` - A set of custom headers to send in every request. +
216+
_Default:_ `{}`
217+
218+
|`context`
219+
|`object` - A custom object that you can use for observability in your events.
220+
It will be merged with the API level context option. +
221+
_Default:_ `null`
222+
223+
|`enableMetaHeader`
224+
|`boolean` - If true, adds an header named `'x-elastic-client-meta'`, containing some minimal telemetry data,
225+
such as the client and platform version. +
226+
_Default:_ `true`
227+
228+
|`cloud`
229+
a|`object` - Custom configuration for connecting to
230+
https://cloud.elastic.co[Elastic Cloud]. See https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/auth-reference.html[Authentication]
231+
for more details. +
232+
_Default:_ `null` +
233+
_Cloud configuration example:_
234+
[source,js]
235+
----
236+
const client = new Client({
237+
cloud: {
238+
id: 'name:bG9jYWxob3N0JGFiY2QkZWZnaA=='
239+
},
240+
auth: {
241+
username: 'elastic',
242+
password: 'changeme'
243+
}
244+
})
245+
----
246+
247+
|===

docs/child.asciidoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
[[child-client]]
2-
== Creating a child client
1+
[[child]]
2+
=== Creating a child client
33

44
There are some use cases where you may need multiple instances of the client.
55
You can easily do that by calling `new Client()` as many times as you need, but

0 commit comments

Comments
 (0)