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 theelasticsearch.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 needsudo
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:
1 | 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.
1 | curl -XGET localhost:9200 |
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:
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 | // 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.
1 2 3 4 5 6 7 8 | 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:
1 2 | // 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:
1 2 3 4 5 6 7 8 9 10 | // 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 theDo
methods and pass the Context object instance you declared earlier to theDo
method.
1 2 3 4 | // 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:
1 2 | 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:
1 | CreateIndex(): &{true true some_index} |
- Remove the index if it exists with the method
DeleteIndex()
.
1 2 3 4 5 6 7 8 9 10 | // 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-methodDo()
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:
1 2 3 4 5 6 7 8 | // 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 HTTPGET
request to do that:
1 | GET some_index |
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.
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | 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!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started