Skip to content
Oliver Eilhard edited this page Jan 21, 2016 · 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.

There are two use cases for a client.

NewClient

The default use case is to have a client that does all the heavy lifting for you: It monitors nodes in your cluster and adds and removes them automatically; it does health checks for existing nodes; it has retries etc. You create this type of client with elastic.NewClient(...) (see below).

NewSimpleClient

When in a restricted environment like [App Engine](/olivere/elastic/wiki/App Engine), you might need a very simple, request-scoped client. For your convenience, there is a elastic.NewSimpleClient(...) that disables all the fancy stuff like sniffing and health checks. Furthermore, it sets retries to 1 by default.

Under the hood, a simple client is simply a stripped down, differently configured Client. However, both NewClient and NewSimpleClient return a *Client, so all features stay the same.

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 gopkg.in/olivere/elastic.v2 (for Elasticsearch 1.x) or go get gopkg.in/olivere/elastic.v3 (for Elasticsearch 2.x) on the command line, all you need to do is:

// Import net/http and elastic
import (
  "net/http"

  // Use this for Elasticsearch 1.x:
  "gopkg.in/olivere/elastic.v2"

  // Use this for Elasticsearch 2.x:
  "gopkg.in/olivere/elastic.v3"
)

...
// Create a client
client, err := elastic.NewClient()
if err != nil {
  // Handle error
  panic(err)
}
...

Notice: To create a simple client, replace elastic.NewClient() with elastic.NewSimpleClient(). All other features of Elastic are the same since both NewClient and NewSimpleClient both return a Client, just initialized with different options.

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.

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

You might want to visit the page about services

Clone this wiki locally