Connect Elasticsearch Golang with Olivere Driver

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

The Olivere’s Elastic package library is a very popular third-party driver for Elasticsearch. This tutorial will explain how to install the Olivere Driver in Golang on a server’s $GOPATH and how to use it to connect to Elasticsearch. The Oliver driver supports multiple versions of Elasticsearch and this tutorial will also cover how to install the Olivere driver for the currently-installed version of Elasticsearch and connect to Elasticsearch with the Golang Olivere driver.

Prerequisites for Using the Olivere driver for Elasticsearch

  • There must be an Elasticsearch cluster running on the server. The default port for Elasticsearch is 9200 and is configured in the cluster’s elasticsearch.yml file.

  • Have sudo access to the Linux server or macOS system. A private key with sudo access is required to remotely access the server through SSH.

  • The Golang program must be installed on the same machine or server running the Elasticsearch cluster. Make sure to export both the $GOROOT and $GOPATH paths after installation. Confirm installation by entering go version into a terminal window.

How to Install the Olivere Package Library

Execute the go get command to install the Olivere package library. Make sure that the Olivere package version matches the major number of the Elasticsearch version running on the server.

How to obtain the current version of Elasticsearch running on the server

Use the following cURL request in a terminal window to receive a cluster response from Elasticsearch to determine what macro version of Elasticsearch is running on the server:

1
curl -XGET localhost:9200

The macro version is represented as the first integer value under the "version" key in the JSON response returned by the Elasticsearch cluster. It should show version 7, 6, or 5.

How to use Golang’s “go get” command to install the Olivere driver for the currently-installed version of Elasticsearch

The number at the end of the “go get” command, such as v7 or v6, must be changed to match the installed version of Elasticsearch.

How to install the Olivere Golang package driver for Elasticsearch v6 and v7

Execute the following command to install the Olivere Golang package driver for Elasticsearch v7 on the system’s $GOPATH:

1
go get gopkg.in/olivere/elastic.v7

Execute the following command to install the Olivere Golang package driver for Elasticsearch v6 on the system’s $GOPATH:

1
go get gopkg.in/olivere/elastic.v6

Terminal screenshot using cURL to get Elasticsearch version and go get to install the Olivere driver for Golang

How to Confirm the Olivere ‘elastic’ Package Installed Correctly

Golang has a built-in command tool called doc that returns documentation about a package. To check if the elastic package installed correctly, and obtain basic information about how it works, execute the following command:

1
go doc elastic

The system will return a response resembling the following, provided the package installed correctly:

1
2
3
package elastic // import "github.com/olivere/elastic"
..
..

Terminal screenshot of the Golang go doc command to return information about the Olivere Elastic package

How to Create a Project Directory and Go Script for the Elasticsearch App

Navigate to the $GOPATH using the cd command in a terminal window on the server and then create a new directory somewhere in that path for the Elasticsearch application with the following script:

1
2
3
4
go version && cd $GOPATH
cd my-go-apps
mkdir elastic-app && cd elastic-app
touch my_app.go

NOTE: Golang conventions for file names typically have lowercase words and underscores _ instead of hyphens -.

Terminal screenshot getting go version and $GOPATH to make an Elasticsearch project directory

How to Edit the Go Script for the Olivere Elasticsearch API Requests

Use a text-based editor, like vim, gedit or nano, and edit the Golang scripts used to connect to Elasticsearch that have the .go file extension. Here is an example:

1
nano my_app.go

How to Declare Package Main and Import Golang Packages for the Elasticsearch Olivere Driver

Execute the following script to import a few native Golang packages, along with the Olivere package library, to help with parsing and printing the Elasticsearch client’s attributes:

1
2
3
4
5
6
7
8
9
10
11
12
package main

import (
"fmt"
"log" // log errors
"reflect" // get client attributes
"strconv" // to convert port int to string
"time" // set timeout for connection

// Import the Olivere Golang driver for Elasticsearch
"github.com/olivere/elastic"
)

How to Connect to Elasticsearch with the Olivere Driver and Return a Client Instance

The following command is the easiest way to connect to Elasticsearch and instantiate a client object:

1
client, err := elastic.NewClient()

How to declare the main() function and print attributes of the Elasticsearch package

Execute the following command:

1
2
3
4
5
func main() {

fmt.Println("Version:", elastic.Version)
fmt.Println("DefaultScheme:", elastic.DefaultScheme)
fmt.Println("DefaultURL:", elastic.DefaultURL)

How to pass variables to the NewClient() method

The following script shows how to pass custom variables to the Elasticsearch library’s NewClient() method and convert the values to strings:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Pass the connection settings as variables instead of hardcoding
port := 9200
var sec time.Duration = 5

// Convert port integer to a string
strPort := strconv.Itoa(port)
timeOut := sec * time.Second

// Concatenate domain string
domainURL := "http://localhost"
setURL := domainURL + ":" + strPort

// Instantiate a client instance of the elastic library
client, err := elastic.NewClient(
elastic.SetSniff(true),
elastic.SetURL(setURL),
elastic.SetHealthcheckInterval(timeOut), // quit trying after 5 seconds
)

How to obtain the Golang attributes of the Elasticsearch client instance

The client instance is a *elastic.Client type object that has over 100 different method attributes for making API calls to the Elasticsearch cluster. A sample script follows:

1
2
3
// Print client information
fmt.Println("client:", client)
fmt.Println("client TYPE:", reflect.TypeOf(client))

How to iterate all of the client object’s attributes

Check if any errors were returned while connecting to Elasticsearch. If no errors occurred, use the reflect library to iterate all of the method attributes of the client instance with the following script:

1
2
3
4
5
6
7
8
9
10
11
12
13
// Check and see if olivere's NewClient() method returned an error
if err != nil {
fmt.Println("elastic.NewClient() ERROR:", err)
log.Fatalf("quitting connection..")
} else {
// Use the reflect library to get all of the client instance's attributes
t := reflect.TypeOf(client)
for i := 0; i - t.NumMethod(); i++ {
method := t.Method(i)
fmt.Println("\nclient method NAME:", i, method.Name)
fmt.Println("method:", method)
}
}

Terminal screenshot of all the Elastic client's attributes in Golang

Conclusion

This tutorial explained how to connect to Elasticsearch using the Olivere driver with Golang. The tutorial covered how to install the Olivere Golang package driver for Elasticsearch versions 6 and 7, how to connect to Elasticsearch with the Olivere driver, how to use Golang’s “go get” command, how to install the Olivere Package Library and how to create a project directory. When connecting Elasticsearch with Golang and when using the Golang driver for Elasticsearch, it is important to remember that the number at the end of the “go get” command must be changed to match the currently-installed version of Elasticsearch.

Just the Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main

import (
"fmt"
"log" // log errors
"reflect" // get client attributes
"strconv" // to convert port int to string
"time" // set timeout for connection

// Import the Olivere Golang driver for Elasticsearch
"github.com/olivere/elastic"
)

func main() {

// Some of the elastic client's attributes
// https://github.com/olivere/elastic/blob/release-branch.v7/client.go
fmt.Println("Version:", elastic.Version)
fmt.Println("DefaultScheme:", elastic.DefaultScheme)
fmt.Println("DefaultURL:", elastic.DefaultURL)

// Pass the connection settings as variables instead of hardcoding
port := 9200
var sec time.Duration = 5

// Convert port integer to a string
strPort := strconv.Itoa(port)
timeOut := sec * time.Second

// Concatenate domain string
domainURL := "http://localhost"
setURL := domainURL + ":" + strPort

// Instantiate a client instance of the elastic library
client, err := elastic.NewClient(
elastic.SetSniff(true),
elastic.SetURL(setURL),
elastic.SetHealthcheckInterval(timeOut), // quit trying after 5 seconds
)
//client, err := elastic.NewClient()

// Print client information
fmt.Println("client:", client)
fmt.Println("client TYPE:", reflect.TypeOf(client))

// Check and see if olivere's NewClient() method returned an error
if err != nil {
fmt.Println("elastic.NewClient() ERROR:", err)
log.Fatalf("quitting connection..")
} else {
// Use the reflect library to get all of the client instance's attributes
t := reflect.TypeOf(client)
for i := 0; i - t.NumMethod(); i++ {
method := t.Method(i)
fmt.Println("\nclient method NAME:", i, method.Name)
fmt.Println("method:", method)
}
}
}

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.