How To Create An Elasticsearch Index Using The Olivere Driver In Golang

Introduction

The Olivere driver in the Go language (Golang) is excellent for quickly building simple or sophisticated queries for your Elasticsearch index. Basic queries are easy to do, but the Olivere driver also has the capability to deliver accurate results when you create highly complex queries. It’s great that connecting the Olivere driver to Elasticsearch is a straightforward process. Discover how to do this now with this tutorial that explains create an Elasticsearch index using the Olivere driver in Golang. Learn the hassle-free way to query and reap the benefits today.

If you’re familiar with how to accomplish this and want to skip the detailed steps in this tutorial, go to Just the Code to see the entire script example.

Prerequisites

  • Elasticsearch – Install the newest version that’s appropriate for your OS, and then run it.

  • When finished installing Elasticsearch, create an Elasticsearch cluster on port 9200, the default. If you haven’t already configured it, do so in the elasticsearch.yml file of your cluster.

  • Go language – On the same server or machine that you’re running the Elasticsearch cluster, install the newest binary release version of Golang created for your OS.

  • After you install Golang, export paths $GOPATH and $GOROOT.

  • On your server or computer, gain user permissions to access using sudo. For Secure Shell (SSH) remote server access, you’ll need sudo access with a private key.

  • In a new terminal window, use the go version command to verify the installation and view which version of Golang is on your computer or server.

  • Kibana – Install Kibana, and then run the service if you’d like to use it in the last step of this tutorial to verify that the Elasticsearch is running.

Elasticsearch is ready for you to install the Olivere Go driver

  • On Elasticsearch, install the Olivere driver Golang package.

  • The example below shows how the Olivere driver Golang package is installed on v7 of Elasticsearch:

go get gopkg.in/olivere/elastic.v7

>NOTE: If you have an Elasticsearch version that’s older than v7, use the command get to automatically find out which Elasticsearch version you have and match it up. Alternatively, use the cURL request for port 9200.

  • Here’s an example of using the cURL request to find out which version of Elasticsearch is installed.
curl -XGET localhost:9200

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

Make an Elasticsearch declaration for string mapping and the index name

  • Declare the constants for the Elasticsearch mapping strings and the index name. Do this by using the keyword const for string constants mapping:
// Mapping and index name. Elasticsearch index doctypes now deprecated
const (
index = "some_index"
mappings = `
{
"settings":{
"number_of_shards":2,
"number_of_replicas":1
},
"mappings":{
"properties":{
"field str":{
"type":"text"
},
"field int":{
"type":"integer"
},
"field bool":{
"type":"boolean"
}
}
}
}
`

)

Connect Golang and Elasticsearch

  • Use the function main() for the declaration.

  • Next, call the method NewClient(), use it to create a client instance. There’s no need to use parameters when calling the method. Here’s an example "http://127.0.0.1:9200". Notice that the domain (the default one) and then the port string follows.

An example of a Golang Olivere driver and Elasticsearch connection

  • Below shows an example of the method NewClient() and how to use method chaining, that is to pass as parameters by way of nested method calls to have a client instance returned.
func main() {

// Instantiate a client instance of the elastic library
client, err := elastic.NewClient(
elastic.SetSniff(true),
elastic.SetURL("http://localhost:9200"),
elastic.SetHealthcheckInterval(5*time.Second), // quit trying after 5 seconds
)

Declare a Context object instantiation for API calls

To make API calls, you’ll need to make a Context object instantiation.

>NOTE: If you want to establish a timeout limit for API calls to the Elasticsearch cluster, use the WithTimeout() method available within the Context Management Library.

  • Use the example below to instantiate a Context object:
// Declare a Context object for the API calls
ctx := context.Background()

Test the Golang Olivere driver and Elasticsearch connection

  • Determine if Elasticsearch returned any errors:
// Check and see if olivere's NewClient() method returned an error
if err != nil {
// (Bad Request): Failed to parse content to map if mapping bad
fmt.Println("elastic.NewClient() ERROR: %v", err)
log.Fatalf("quiting connection..")
} else {
// Print client information
fmt.Println("client:", client)
fmt.Println("client TYPE:", reflect.TypeOf(client), "\n")
}

Find out if the Elasticsearch cluster already has the index name there

Olivere’s IndexExists() in the Client library is the method you use to tell if an index name is already on the Elasticsearch cluster.

  • Use the IndexExists() and the Do methods and pass the Context object instance you declared earlier to the Do method.
// Check if the Elasticsearch index already exists
exist, err := client.IndexExists(index).Do(ctx)
if err != nil {
log.Fatalf("IndexExists() ERROR: %v", err)

Analyze the response from the Elasticsearch API

  • Here’s what the response should look something like if the index is there:
The index some_index already exists.
CreateIndex(): &{true true some_index}

>NOTE:__ Notice the first sentence in the response stating that the index name is already present.

  • If the index is not there, the response should be similar to this:
CreateIndex(): &{true true some_index}
  • Remove the index if it exists with the method DeleteIndex().
// If the index exists..
} else if exist {
fmt.Println("The index " + index + " already exists.")
_, err = client.DeleteIndex(index).Do(ctx)

// Check for DeleteIndex() errors
if err != nil {
log.Fatalf("client.DeleteIndex() ERROR: %v", err)
}
}

Make a new index of Elasticsearch

  • After you’ve deleted the Elastersearch cluster’s old index, use the method CreateIndex() to make a new one in Golang.

  • Within the script of, call the sub-method Body() and the sub-method Do()

  • Pass the name of the index.

  • Pass the string of Context mappings.

  • Finally, pass the Context object instance.

  • Your result should look similar to the example given below:

// Create a new index and pass the mappings string to the body
create, err := client.CreateIndex(index).Body(mappings).Do(ctx)
if err != nil {
log.Fatalf("CreateIndex() ERROR: %v", err)
} else {
fmt.Println("CreateIndex():", create)
}
}

Use Kibana to check the creation of the Elasticsearch index

  • Run Kibana if it’s not already activated under port 5601, its default. Under Dev Tools, go to the Kibana Console User Interface. You’ll want to check the index, so make an HTTP GET request to do that:
GET some_index

Screenshot of Kibana getting an Elasticsearch index created in Golang with Olivere

Conclusion

This tutorial showed you how to use the Elasticsearch index Olivere driver in Golang. Now you can create basic and intricate queries effortlessly. That’s because the Olivere driver enables you to obtain the results you need when swiftness counts. When it relates to retrieving and understanding your business’s data, rapidity is important every day.

Just the Code

Here’s the complete sample script for how to create Elasticsearch index Olivere driver in Golang.

package main

import (
"context" // Context object for Do() methods
"fmt" // Format and print cluster data
"log" // Log errors and quit
"reflect" // Get object methods and attributes
"time" // Set a timeout for the connection

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

// Mapping and index name. Elasticsearch index doctypes now deprecated
const (
index = "some_index"
mappings = `
{
"settings":{
"number_of_shards":2,
"number_of_replicas":1
},
"mappings":{
"properties":{
"field str":{
"type":"text"
},
"field int":{
"type":"integer"
},
"field bool":{
"type":"boolean"
}
}
}
}
`

)

func main() {

// Instantiate a client instance of the elastic library
client, err := elastic.NewClient(
elastic.SetSniff(true),
elastic.SetURL("http://localhost:9200"),
elastic.SetHealthcheckInterval(5*time.Second), // quit trying after 5 seconds
)

// Declare a Context object for the API calls
ctx := context.Background()

// Check and see if olivere's NewClient() method returned an error
if err != nil {
// (Bad Request): Failed to parse content to map if mapping bad
fmt.Println("elastic.NewClient() ERROR: %v", err)
log.Fatalf("quiting connection..")
} else {
// Print client information
fmt.Println("client:", client)
fmt.Println("client TYPE:", reflect.TypeOf(client), "\n")
}

// Check if the Elasticsearch index already exists
exist, err := client.IndexExists(index).Do(ctx)
if err != nil {
log.Fatalf("IndexExists() ERROR: %v", err)

// If the index exists..
} else if exist {
fmt.Println("The index " + index + " already exists.")
_, err = client.DeleteIndex(index).Do(ctx)

// Check for DeleteIndex() errors
if err != nil {
log.Fatalf("client.DeleteIndex() ERROR: %v", err)
}
}

// Create a new index and pass the mappings string to the body
create, err := client.CreateIndex(index).Body(mappings).Do(ctx)
if err != nil {
log.Fatalf("CreateIndex() ERROR: %v", err)
} else {
fmt.Println("CreateIndex():", create)
}
}

Pilot the ObjectRocket platform free for 30 Days

It's easy to get started. Imagine the time you'll save by not worrying about database management. Let's do this!

PILOT FREE FOR 30 DAYS

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.