|
| 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 | +|=== |
0 commit comments