Skip to content
Oliver Eilhard edited this page May 23, 2018 · 7 revisions

The Ping service connects to an Elasticsearch server (http://127.0.0.1:9200 by default, see below) and gets e.g. the version number.

// Ping the Elasticsearch server to get e.g. the version number
ctx := context.Background()
info, code, err := client.Ping().Do(ctx)
if err != nil {
    // Handle error
    panic(err)
}
fmt.Printf("Elasticsearch returned with code %d and version %s", code, info.Version.Number)

Notice: In elastic v2, the Ping service is special in that it doesn't use the URLs of the client but the one specified via the URL function of the PingService (which is http://127.0.0.1:9200 by default).

So to use the PingService in v2 correctly, specify the URL directly:

info, code, err := client.Ping().URL("<es-node-url>").Do(ctx)
...

The reason for this special behavior are as follows: First, you typically use Ping to not only find nodes in your current cluster, but also nodes outside your Elasticsearch cluster. Second, you can specify many URLs for your cluster and Elastic happily does round-robin to load balance between the nodes. If you use many URLs, which one do you use by default for the PingService? Third, pinging is for individual nodes, not for a cluster. Use the Cluster API, e.g. ClusterState or ClusterHealth to get information about your cluster (and its individual nodes).

Anyway, just specify a URL with your Ping and you should be fine. :-)

As this is probably the no. 1 question asked via GitHub issues, the behavior of Ping has changed in elastic v3. In v3 and later, you always have to specify the URL of the server you want to Ping in the PingService:

// Always specify the URL with elastic.v3 or later
info, code, err := client.Ping("<es-node-url>").Do(ctx)
...
Clone this wiki locally