-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Connection Problems
This page describes what to do when you cannot get Elastic to connect to an Elasticsearch node or cluster.
You can provide one or more URLs to NewClient(...)
that Elastic will use to connect to a node/a cluster. On startup, Elastic will make sure to have at least one working connection. If Elastic does not find an active connection, NewClient(...)
will fail straight away with a connection error (check with elastic.IsConnErr(err)
). See also: Connection errors.
Elastic comes in different versions: E.g. there is one for Elasticsearch 1.x, one for Elasticsearch 2.x, one for Elasticsearch 5.x, Elasticsearch 6.x and another for Elasticsearch 7.x. You need to use the right combination of Elastic and Elasticsearch.
If you have Elasticsearch 7.x (e.g. 7.3.0) installed, you must use Elastic version 7. Also notice that Elastic version 7 uses Go modules exclusively. Not all dependency managers support Go modules yet, so your mileage may vary.
Anyway, here's what you need to do:
go get github.com/olivere/elastic/v7
- Use
"github.com/olivere/elastic/v7"
as your import path.
// Create a client for Elasticsearch 7.x
import (
"net/http"
"github.com/olivere/elastic/v7"
)
...
// Create a client
client, err := elastic.NewClient()
if err != nil {
// Handle error
panic(err)
}
...
If you have Elasticsearch 6.x (e.g. 6.8.2) installed, you must use Elastic version 6. Here's what you need to do:
go get github.com/olivere/elastic
- Use
"github.com/olivere/elastic"
as your import path.
Optional, pin the Elastic version with a dependency manager like github.com/golang/dep.
// Create a client for Elasticsearch 6.x
import (
"net/http"
"github.com/olivere/elastic"
)
...
// Create a client
client, err := elastic.NewClient()
if err != nil {
// Handle error
panic(err)
}
...
If you have Elasticsearch 5.x (e.g. 5.6.7) installed, you must use Elastic version 5 (yes, they're finally aligned). Here's what you need to do:
go get gopkg.in/olivere/elastic.v5
- Use
"gopkg.in/olivere/elastic.v5"
as your import path.
// Create a client for Elasticsearch 5.x
import (
"net/http"
"gopkg.in/olivere/elastic.v5" // Notice the v5 here!
)
...
// Create a client
client, err := elastic.NewClient()
if err != nil {
// Handle error
panic(err)
}
...
If you have Elasticsearch 2.x (e.g. 2.4.6) installed, you must use Elastic version 3. Here's what you need to do:
go get gopkg.in/olivere/elastic.v3
- Use
"gopkg.in/olivere/elastic.v3"
as your import path.
// Create a client for Elasticsearch 2.x
import (
"net/http"
"gopkg.in/olivere/elastic.v3" // Notice the v3 here!
)
...
// Create a client
client, err := elastic.NewClient()
if err != nil {
// Handle error
panic(err)
}
...
If you have Elasticsearch 1.x (e.g. 1.7.6) installed, you must use Elastic version 2. Here's what you need to do:
go get gopkg.in/olivere/elastic.v2
- Use
"gopkg.in/olivere/elastic.v2"
as your import path.
Example:
// Create a client for Elasticsearch 1.x
import (
"net/http"
"gopkg.in/olivere/elastic.v2"
)
...
// Create a client
client, err := elastic.NewClient()
if err != nil {
// Handle error
panic(err)
}
...
Do not use the import path from the GitHub repository unless you know what you're doing. It's for people who want to hack on and contribute to Elastic. The code released on gopkg.in/olivere/elastic.v2
, gopkg.in/olivere/elastic.v3
and gopkg.in/olivere/elastic.v5
is considered stable and is the import path of choice for end-users.
When sniffing is enabled (the default), Elastic uses the Nodes Info API to find all nodes in the cluster. After it finds these nodes, it updates the internal list of connections and keeps it updated periodically.
Now most connection problems get reported because Elastic either cannot invoke the Nodes Info API or the Nodes Info API reports back IP:port
combinations that are not accessible to Elastic. E.g. Docker sometimes returns internal IP:port
combinations. Unfortunately those internal IP:port
combinations are only accessible from within the Docker container and inaccessible from the outside (where Elastic is running). If that happens you need to change the network bindings of Elasticsearch to bind to externally accessible network interfaces. There's a separate page on Docker as well.
Here's what the Nodes Info API typically returns and what Elastic uses to find the IP:port
combination of the nodes of a cluster (newer versions slightly changed the returned values):
$ curl -s -XGET 'http://127.0.0.1:9200/_nodes/http?pretty=1'
{
"_nodes" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"cluster_name" : "elasticsearch",
"nodes" : {
"v9vBH0xXQ1-GZw-bOfxlFQ" : {
"name" : "v9vBH0x",
"transport_address" : "127.0.0.1:9300",
"host" : "127.0.0.1",
"ip" : "127.0.0.1",
"version" : "5.6.8",
"build_hash" : "688ecce",
"roles" : [
"master",
"data",
"ingest"
],
"attributes" : {
"ml.max_open_jobs" : "10",
"ml.enabled" : "true"
},
"http" : {
"bound_address" : [
"0.0.0.0:9200"
],
"publish_address" : "127.0.0.1:9200",
"max_content_length_in_bytes" : 104857600
}
}
}
}
You can see that the publish_address
field contains the IP:port
combination of the node. If you can curl this address from the server that Elastic runs on, there shouldn't be any connection problems.
If you've turned sniffing off and you still can't connect to Elasticsearch, verify that you have http://
as part of your URL. Using something like localhost:9200
won't work, you need http://localhost:9200
.