bytebase/bytebase · error

failed to create elasticsearch client

Error message

failed to create elasticsearch client

What it means

elasticsearch.NewClient (go-elasticsearch typed client) returns an error only when the client configuration itself is invalid — it does not contact the server. Wrapped here as "failed to create elasticsearch client". Typical causes are a malformed address in esConfig.Addresses or a CA certificate the client cannot decode.

Source

Thrown at backend/plugin/db/elasticsearch/elasticsearch.go:169

			InsecureSkipVerify: true,
		}
	} else {
		// Ensure minimum TLS version
		tlsConfig.MinVersion = tls.VersionTLS12
	}

	esConfig := newElasticsearchConfig(config, address, tlsConfig)
	// default http client.
	httpClient := &http.Client{
		Transport: &http.Transport{
			TLSClientConfig: tlsConfig,
		},
	}

	// typed elasticsearch client.
	typedClient, err := elasticsearch.NewClient(esConfig)
	if err != nil {
		return nil, errors.Wrapf(err, "failed to create elasticsearch client")
	}

	// generate basic authentication string for http client.
	encodedUsrAndPasswd := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", config.DataSource.Username, config.Password)))
	basicAuthString := fmt.Sprintf("Basic %s", string(encodedUsrAndPasswd))

	return &Driver{
		typedClient: typedClient,
		basicAuthClient: &BasicAuthClient{
			httpClient: httpClient,
			addrScheduler: &AddressScheduler{
				addresses: []string{address},
				count:     0,
			},
			basicAuthString: basicAuthString,
		},
		config: config,
	}, nil

View on GitHub (pinned to 1870550677)

Solutions

  1. Ensure the datasource Host is non-empty and forms a valid address; check the wrapped error for the exact URL complaint.
  2. Validate the SslCa PEM content; clear it if the cluster uses a CA your OS already trusts.
  3. Check the wrapped error from elasticsearch.NewClient for the specific config field it rejects and fix that field.
  4. If this appeared after a dependency bump, review the go-elasticsearch changelog for Config validation changes.

Example fix

// before
if config.DataSource.Host == "" { /* falls through */ }
// after
if config.DataSource.Host == "" {
	return nil, errors.New("elasticsearch host is required")
}
Defensive patterns

Strategy: validation

Validate before calling

if ds.Host == "" {
	return errors.New("elasticsearch host is required")
}
if _, err := url.Parse(fmt.Sprintf("http://%s:%s", ds.Host, ds.Port)); err != nil {
	return fmt.Errorf("invalid address: %w", err)
}

Try / catch

if _, err := elasticsearch.NewClient(cfg); err != nil {
	var cfgErr *elasticsearch.Error
	if errors.As(err, &cfgErr) {
		log.Printf("elasticsearch client config invalid: %v", cfgErr)
	}
	return fmt.Errorf("failed to create elasticsearch client: %w", err)
}

Prevention

When it happens

Trigger: Open with basic-auth auth type where the assembled address (e.g. "http://:9200" when Host is empty) or CACert bytes are rejected by the elasticsearch-go client constructor.

Common situations: Empty host field producing an address like "http://:9200"; CA cert blob that is not decodable PEM; incompatible go-elasticsearch config after a library version upgrade.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of bytebase/bytebase@1870550677 (2026-09-06). Data as JSON: /api/errors/3ca345bb3ed25430. Report an issue: GitHub.