-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Client
This page describes how to create a new client, what a client does when created, and a few other details you need to know to work with Elastic. If you want to know how to create an index, add documents, or search, you'll find that under Services.
In a nutshell, a client is way to communicate with Elasticsearch. When interacting with Elasticsearch, a few things need to be known in advance. For example, Elastic needs to know to which Elasticsearch cluster/nodes you want to connect to, what happens when a connection to a node is lost (because the node goes down for some reason) etc. All of these details are abstracted away in a client.
That's very simple. Let's assume you have a) Elasticsearch installed and running with its default settings (i.e. available at http://127.0.0.1:9200
) and b) you got Elastic by running go get github.com/olivere/elastic
on the command line, all you need to do is:
// Import net/http and elastic
import (
"net/http"
"github.com/olivere/elastic"
)
...
// Create a client
client, err := elastic.NewClient()
if err != nil {
// Handle error
panic(err)
}
...
If your Elasticsearch server is running on a different IP and/or port, just provide a URL to NewClient
:
// Create a client and connect to http://192.168.2.10:9201
client, err := elastic.NewClient(elastic.SetURL("http://192.168.2.10:9201"))
if err != nil {
// Handle error
panic(err)
}
If the parameters to NewClient seem strange, read this article by Dave Cheney for how it works and why it's useful.
If you have an Elasticsearch cluster with several nodes, you can provide a list of URLs to connect to. However, this is not necessary as Elastic will automatically figure out all nodes in your cluster for you automatically (see sniffing).
// Create a client and connect to nodes http://127.0.0.1:9200 and http://127.0.0.1:9201
client, err := elastic.NewClient(elastic.SetURL("http://127.0.0.1:9200", "http://127.0.0.1:9201"))
if err != nil {
// Handle error
panic(err)
}
If you tried all of the things above but keep getting an error such as No Elastic node is available
, see the Connection Problems page for possible resolution.
See Configuration for the list of configuration settings.
You might want to visit the page about services