Advanced configuration

edit

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

For information about the Transport class, refer to Transport.

ConnectionPool

edit

This class is responsible for keeping in memory all the Elasticsearch 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.

const { Client, ConnectionPool } = require('@elastic/elasticsearch')

class MyConnectionPool extends ConnectionPool {
  markAlive (connection) {
    // your code
    super.markAlive(connection)
  }
}

const client = new Client({
    ConnectionPool: MyConnectionPool
})

Connection

edit

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.

const { Client, Connection } = require('@elastic/elasticsearch')

class MyConnection extends Connection {
  request (params, callback) {
    // your code
  }
}

const client = new Client({
  Connection: MyConnection
})

Serializer

edit

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.
const { Client, Serializer } = require('@elastic/elasticsearch')

class MySerializer extends Serializer {
  serialize (object) {
    // your code
  }
}

const client = new Client({
  Serializer: MySerializer
})

Migrate to v8

edit

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