Skip to content
Oliver Eilhard edited this page Feb 22, 2015 · 25 revisions

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.

What is a client?

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.

How do I create 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(http.DefaultClient)
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(http.DefaultClient, "http://192.168.2.10:9201")
if err != nil {
  // Handle error
  panic(err)
}

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(http.DefaultClient, "http://127.0.0.1:9200", "http://127.0.0.1:9201")
if err != nil {
  // Handle error
  panic(err)
}

Okay, I have a client. What do I do now?

You might want to visit the page about services

Clone this wiki locally