Skip to content
Oliver Eilhard edited this page Oct 4, 2019 · 12 revisions

Elastic comes with the following configuration settings:

  • SetHttpClient(*http.Client) allows you to configure your own http.Client and/or http.Transport (default is http.DefaultClient); it is a good idea to use the same http.Client (even if using http.DefaultClient) across many instances of Elastic in order to efficiently use open TCP connections.
  • SetURL(...string) allows you to specify which URLs to connect to (default is http://127.0.0.1:9200).
  • SetBasicAuth(username, password string) allows you to specify the HTTP Basic Authentication details. Use this e.g. with Shield.
  • SetSniff(bool) allows you to specify whether Elastic should sniff the cluster periodically (default is true).
  • SetSnifferTimeout(time.Duration) is the time before Elastic times out on sniffing nodes (default is 2 seconds).
  • SetSnifferTimeoutStartup(time.Duration) is the sniffing timeout used while creating a new client. It is typically larger than sniffer timeout and proved to help on slow startup (default is 5 seconds).
  • SetSnifferInterval(time.Duration) allows you to specify the interval between two sniffer processes (default is 15 minutes).
  • SetHealthcheck(bool) allows you to specify whether Elastic will perform health checks by trying to connect to its nodes periodically (default is true).
  • SetHealthcheckTimeout(time.Duration) is the timeout for health checks (default is 1 second).
  • SetHealthcheckTimeoutStartup(time.Duration) is the health check timeout used while creating a new client. It is typically larger than health check timeout and might help on slow startup (default is 5 seconds).
  • SetHealthcheckInterval(time.Duration) specifies the interval between two health checks (default is 60 seconds).
  • SetDecoder(elastic.Decoder) allows you to set your own decoder for JSON messages from Elasticsearch (default is &elastic.DefaultDecoder{}).
  • SetErrorLog(*log.Logger) sets the logger to use for error messages (default is nil). The error log will contain e.g. messages about nodes joining the cluster or marked as dead.
  • SetInfoLog(*log.Logger) sets the logger to use for informational messages (default is nil). The info log will contain e.g. requests and their response time.
  • SetTraceLog(*log.Logger) sets the logger to use for printing HTTP requests and responses (default is nil). This is useful for debugging what's going on on the wire.
  • SetRequiredPlugins(plugins ...string) sets the list of plugins that need to be registered. Elastic will try to find them on startup. If one of them is not found, you will get an error of kind elastic.ErrPluginNotFound at startup time.
  • SetRetrier(...) sets the Retry strategy for handling failed requests. See Retry and Backoff for details.
  • SetGzip(bool) enables or disables compression on the request side. It is disabled by default.
  • SetHeaders(http.Header{...}) (7.0.7 or later) specifies a list of HTTP headers to be sent on every invocation of Client.PerformRequest. Notice that request-level HTTP headers have preference over client-level HTTP headers.

Here's a full example of configuring a new client:

// Obtain a client for an Elasticsearch cluster of two nodes,
// running on 10.0.1.1 and 10.0.1.2. Do not run the sniffer.
// Set the healthcheck interval to 10s. When requests fail,
// retry 5 times. Print error messages to os.Stderr and informational
// messages to os.Stdout.
client, err := elastic.NewClient(
  elastic.SetURL("http://10.0.1.1:9200", "http://10.0.1.2:9200"),
  elastic.SetSniff(false),
  elastic.SetHealthcheckInterval(10*time.Second),
  elastic.SetRetrier(NewCustomRetrier()),
  elastic.SetGzip(true),
  elastic.SetErrorLog(log.New(os.Stderr, "ELASTIC ", log.LstdFlags)),
  elastic.SetInfoLog(log.New(os.Stdout, "", log.LstdFlags)),
  elastic.SetHeaders(http.Header{
    "X-Caller-Id": []string{"..."},
  }),
)