Updated on 2026-01-09 GMT+08:00

Accessing an Elasticsearch Cluster Using Go

When querying and managing data in CSS's Elasticsearch clusters, developers sometimes use the Go language. This section describes how to connect to a CSS Elasticsearch cluster using Go.

Prerequisites

  • The target Elasticsearch cluster is available.
  • The server that runs Go can communicate with the Elasticsearch cluster.
  • Depending on the network configuration method used, obtain the cluster access address. For details, see Network Configuration.
  • The Elasticsearch Go client has been installed on the server. For details, see Elasticsearch Go Client.

Connecting to a Non-Security Mode Cluster

Connect to a non-security mode cluster. The sample code is as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package main

import (
	"github.com/elastic/go-elasticsearch/v7"
	"log"
)

func main() {
	cfg := elasticsearch.Config{
		Addresses: []string{
			"http://HOST:9200/",
		},
	}

	es, _ := elasticsearch.NewClient(cfg)
	log.Println(es.Info())
}

In the information above, HOST indicates the internal IP address of a cluster node.

Connecting to a Security-Mode Cluster That Uses HTTP

The sample code is as follows:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package main

import (
	"github.com/elastic/go-elasticsearch/v7"
	"log"
)

func main() {
	cfg := elasticsearch.Config{
		Addresses: []string{
			"http://HOST:9200/",
		},
		Username: "USERNAME",
		Password: "PASSWORD",
	}

	es, _ := elasticsearch.NewClient(cfg)
	log.Println(es.Info())
}
Table 1 Variables

Parameter

Description

HOST

IP address for accessing the cluster. If there are multiple IP addresses, separate them using a comma (,).

USERNAME

Username for accessing the cluster.

PASSWORD

Password of the user.

Connecting to a Security-Mode Cluster That Uses HTTPS (Without a Certificate)

The sample code is as follows:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import (
	"crypto/tls"
	"github.com/elastic/go-elasticsearch/v7"
	"log"
	"net/http"
)

func main() {
	cfg := elasticsearch.Config{
		Addresses: []string{
			"https://HOST:9200/",
		},
		Username: "USERNAME",
		Password: "PASSWORD",
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: true,
			},
		},
	}

	es, _ := elasticsearch.NewClient(cfg)
	log.Println(es.Info())
}
Table 2 Variables

Parameter

Description

HOST

IP address for accessing the cluster. If there are multiple IP addresses, separate them using a comma (,).

USERNAME

Username for accessing the cluster.

PASSWORD

Password of the user.

Connecting to a Security-Mode Cluster That Uses HTTPS (With a Certificate)

The sample code is as follows:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main

import (
	"crypto/tls"
	"crypto/x509"
	"flag"
	"github.com/elastic/go-elasticsearch/v7"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"time"
)

func main() {
	insecure := flag.Bool("insecure-ssl", false, "Accept/Ignore all server SSL certificates")
	flag.Parse()

	// Get the SystemCertPool, continue with an empty pool on error
	rootCAs, _ := x509.SystemCertPool()
	if rootCAs == nil {
		rootCAs = x509.NewCertPool()
	}

	// Read in the cert file
	certs, err := ioutil.ReadFile("/tmp/CloudSearchService.cer")
	if err != nil {
		log.Fatalf("Failed to append %q to RootCAs: %v", "xxx", err)
	}

	// Append our cert to the system pool
	if ok := rootCAs.AppendCertsFromPEM(certs); !ok {
		log.Println("No certs appended, using system certs only")
	}

	config := elasticsearch.Config{
		Addresses: []string{
			"https://HOST:9200/",
		},
		Username: "USERNAME",
		Password: "PASSWORD",
		Transport: &http.Transport{
			MaxIdleConnsPerHost:   10,
			ResponseHeaderTimeout: time.Second,
			DialContext: (&net.Dialer{
				Timeout:   30 * time.Second,
				KeepAlive: 30 * time.Second,
			}).DialContext,
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: *insecure,
				RootCAs:            rootCAs,
			},
		},
	}
	es, _ := elasticsearch.NewClient(config)
	log.Println(elasticsearch.Version)
	log.Println(es.Info())
}
Table 3 Variables

Parameter

Description

HOST

IP address for accessing the cluster. If there are multiple IP addresses, separate them using a comma (,).

USERNAME

Username for accessing the cluster.

PASSWORD

Password of the user.

Running Code

Write the code above to the EsTest.gc file based on the cluster type and save the file to an independent directory. Run the following command in that directory to run the code:

go env -w GO111MODULE=on
go env -w GOPROXY=https://repo.huaweicloud.com/repository/goproxy/ 
go env -w GONOSUMDB=*

go mod init test
go mod tidy
go run EsTest.go

If the cluster information is returned, the cluster is connected.