107 lines
2.9 KiB
Plaintext
107 lines
2.9 KiB
Plaintext
[[advanced-config]]
|
|
=== Advanced configuration
|
|
|
|
If you need to customize the client behavior heavily, you are in the right
|
|
place! The client enables you to customize the following internals:
|
|
|
|
* `ConnectionPool` class
|
|
* `Connection` class
|
|
* `Serializer` class
|
|
|
|
NOTE: For information about the `Transport` class, refer to <<transport>>.
|
|
|
|
|
|
[discrete]
|
|
==== `ConnectionPool`
|
|
|
|
This class is responsible for keeping in memory all the {es} Connection that you
|
|
are using. There is a single Connection for every node. The connection pool
|
|
handles the resurrection strategies and the updates of the pool.
|
|
|
|
[source,js]
|
|
----
|
|
const { Client, ConnectionPool } = require('@elastic/elasticsearch')
|
|
|
|
class MyConnectionPool extends ConnectionPool {
|
|
markAlive (connection) {
|
|
// your code
|
|
super.markAlive(connection)
|
|
}
|
|
}
|
|
|
|
const client = new Client({
|
|
ConnectionPool: MyConnectionPool,
|
|
cloud: { id: '<cloud-id>' },
|
|
auth: { apiKey: 'base64EncodedKey' }
|
|
})
|
|
----
|
|
|
|
|
|
[discrete]
|
|
==== `Connection`
|
|
|
|
This class represents a single node, it holds every information we have on the
|
|
node, such as roles, id, URL, custom headers and so on. The actual HTTP request
|
|
is performed here, this means that if you want to swap the default HTTP client
|
|
(Node.js core), you should override the `request` method of this class.
|
|
|
|
[source,js]
|
|
----
|
|
const { Client, BaseConnection } = require('@elastic/elasticsearch')
|
|
|
|
class MyConnection extends BaseConnection {
|
|
request (params, callback) {
|
|
// your code
|
|
}
|
|
}
|
|
|
|
const client = new Client({
|
|
Connection: MyConnection,
|
|
cloud: { id: '<cloud-id>' },
|
|
auth: { apiKey: 'base64EncodedKey' }
|
|
})
|
|
----
|
|
|
|
|
|
[discrete]
|
|
==== `Serializer`
|
|
|
|
This class is responsible for the serialization of every request, it offers the
|
|
following methods:
|
|
|
|
* `serialize(object: any): string;` serializes request objects.
|
|
* `deserialize(json: string): any;` deserializes response strings.
|
|
* `ndserialize(array: any[]): string;` serializes bulk request objects.
|
|
* `qserialize(object: any): string;` serializes request query parameters.
|
|
|
|
[source,js]
|
|
----
|
|
const { Client, Serializer } = require('@elastic/elasticsearch')
|
|
|
|
class MySerializer extends Serializer {
|
|
serialize (object) {
|
|
// your code
|
|
}
|
|
}
|
|
|
|
const client = new Client({
|
|
Serializer: MySerializer,
|
|
cloud: { id: '<cloud-id>' },
|
|
auth: { apiKey: 'base64EncodedKey' }
|
|
})
|
|
----
|
|
|
|
[discrete]
|
|
==== Migrate to v8
|
|
|
|
The Node.js client can be configured to emit an HTTP header
|
|
`Accept: application/vnd.elasticsearch+json; compatible-with=7`
|
|
which signals to Elasticsearch that the client is requesting
|
|
`7.x` version of request and response bodies. This allows for
|
|
upgrading from 7.x to 8.x version of Elasticsearch without upgrading
|
|
everything at once. Elasticsearch should be upgraded first after
|
|
the compatibility header is configured and clients should be upgraded
|
|
second.
|
|
To enable to setting, configure the environment variable
|
|
`ELASTIC_CLIENT_APIVERSIONING` to `true`.
|