Check If An Elasticsearch Index Exists Using The Golang Olivere Driver
Introduction
The Go language (Golang) Olivere driver is the power source behind intricate querying in Elasticsearch and speedy responses. If you learn that an Elasticsearch Index doesn’t exist after you’ve tried to add or change the data in a document, an error will be returned. While scripting isn’t error-free, there is something you can do to try to reduce inaccuracies. Streamlining your query experiences is possible when you use Golang’s Olivere driver to find out if an Elasticsearch index exists.
You can bypass this tutorial and go to Just the Code if you already know how to complete the steps for determining if an Elasticsearch index exists Golang Olivere.
Prerequisites
Download, install, and run Elasticsearch.
Download, install, and run Go language Olivere driver Golang package. Be sure to install it on the same server or machine where Elasticsearch is located.
NOTE: Check that you set the
$GOPATH
because the Gopath is to contain your example script for Golang from this tutorial and the app.
Make a test index
- In a terminal window, use the library
cURL
to make a basic HTTP request to test the API. That’s the easiest way to make an index on the cluster in Elasticsearch.
1 2 3 4 5 6 7 | curl -XPUT 'http://localhost:9200/some_index' -H 'Content-Type: application/json' -d ' { "settings" : { "number_of_shards" : 3, "number_of_replicas" : 2 } }' |
The index sample above shows that the index some_index
doesn’t exist. However, if resource_already_exists_exception
was returned from the cluster in Elasticsearch, it would indicate that it currently exists.
Find out if an Elasticsearch index is present
- Make a declaration and do a package importation to determine if an index in Elasticsearch is there.
1 2 3 4 5 6 7 8 9 10 11 12 | package main import ( "context" "fmt" "log" // log errors "reflect" // get client attributes "time" // set timeout for connection // Import the Olivere Golang driver for Elasticsearch "github.com/olivere/elastic/v7" ) |
Use the function main() to make a cluster connection in Elasticsearch
1 2 3 4 5 6 7 8 9 | func main() { // Allow for custom formatting of log output log. SetFlags(0) // Use the Olivere package to get the Elasticsearch version number fmt. Println("Version:", elastic. Version) // Create a context object for the API calls ctx := context. Background() |
Limit the time for API call connections with the package ‘time’
When you make API calls to the Elasticsearch cluster, you can limit the time the connection lasts with the package
time
.Set the actual time you want to pass before the connection closes with the attribute
Second
.
1 2 3 4 5 6 | // Declare a client instance of the Olivere driver client, err := elastic. NewClient( elastic. SetSniff(true), elastic. SetURL("http://localhost:9200"), elastic. SetHealthcheckInterval(5*time. Second), // quit trying after 5 seconds ) |
NOTE: Check that when you use the SetURL()
method to pass the string to it, that your settings for your server match the settings for your string’s port, domain, and schema.
During the Olivere driver connection check if errors were returned from Elasticsearch
- The method
NewClient()
returned the objecterr
. Analyze it for errors. If the Elasticsearch cluster returned zero errors, it will display thenil
value.
1 2 3 4 5 6 7 8 9 | // Check and see if olivere's NewClient() method returned an error if err != nil { fmt. Println("elastic. NewClient() ERROR:", err) log. Fatalf("quiting connection..") } else { // Print client information fmt. Println("client:", client) fmt. Println("client TYPE:", reflect. TypeOf(client)) } |
Confirm your indexes in Elasticsearch with a Go slice
- Put all the strings of the index names that you want to verify in the string of the Go slice. You’ll pass the Go slice to the object
IndicesExistsService()
.
NOTE: Even if you have one index name, declare it.
1 2 3 4 5 | // Declare index name as string indexName := "some_index" // Declare a slice of strings for the index names to be checked indices := []string{indexName} |
Verify the existence of numerous indexes in Golang
- If you have more than one index to check if they are present in Golang, they must be passed in the same
[]string
slice.
1 2 | // Declare a slice of multiple Elasticsearch index name strings indices := []string{indexName, "another_index"} |
NOTE: You have the option of making several API calls separately or all at once by adding them to the slice to check if Elasticsearch index exists Golang Olivere. Keep in mind, if you pass them on the slice and if just one isn’t on the cluster in Elasticsearch, you’ll get a
false
response fromIndicesExistsService
function.
Use the Golang function append to modify the slice
Make an index name declaration.
Next, make a strings slice declaration to verify the index names.
The example below shows the Golang function append()
in action:
1 2 3 4 5 6 7 8 9 10 | // Declare index name as string indexName := "some_index" // Declare a slice of strings for the index names to be checked indices := []string{} // Use the append() function to add more index names to slice indices = append(indices, indexName) indices = append(indices, "second_index") indices = append(indices, "last_index") |
- The response should look similar to this one below:
1 2 | indices to check: [some_index second_index last_index] indices length: 3 |
- To verify the index names slice in Golang, use a
for
loop when you make the API request.
Confirm indexes by creating an object NewIndicesExistsService()
Prepare for making an Elasticsearch API call by creating an object
NewIndicesExistsService()
first.Use the
NewIndicesExistsService()
method to confirm indexes. The object type called*elastic.IndicesExistsService
will be returned.
1 2 3 | // Instantiate a new *elastic. IndicesExistsService existService := elastic. NewIndicesExistsService(client) fmt. Println("existService TYPE:", reflect. TypeOf(existService)) |
Do an iteration over the attributes of the IndicesExistsService object
- Use the package
reflect
method call for the objectIndicesExistsService
iteration.
1 2 3 4 5 6 7 | // Iterate over the IndicesExistsService object's methods t := reflect. TypeOf(existService) for i := 0; i < t. NumMethod(); i++ { method := t. Method(i) fmt. Println("nexist METHOD NAME:", i, method. Name) fmt. Println("method:", method) } |
Verify if an index name in Elasticsearch is present by making an API request
- Do an API call over the index name strings to check if the index names on the cluster of Elasticsearch exists.
NOTE: First, make the slice contains the index names. Second, create the
*elastic.IndicesExistsService
instance. After that, make the API call.
Do an iteration over the string names slice
- Run through the length of the slice of index names using the
for
Golang function:
1 2 | // Iterate over the slice of Elasticsearch documents for _, index := range indices { |
Create an additional slice for the index string in Elasticsearch
- The method
Do()
for theIndicesExistsService
object needs a slice for individual index names passed to it, so create the slice first.
1 | indexSlice := []string{index} |
The object ‘IndicesExistsService’ method is to where you pass the slice
Make the API call and use the method
Index()
.Define either remote/master node or local node. Use the method
Local()
boolean option to pass the value.
1 2 3 4 5 | // Pass the string slice with the index name to the Index() method existService. Index(indexSlice) // Bool option for checking just local node or master node as well existService. Local(true) |
Pass the instantiated Golang Context object
- For the API call, the method
Do()
is to where you pass the object Golang Context to get Elasticsearch to return any errors in its response.
1 2 | // Have Do() return an API response by passing the Context object to the method call exist, err := existService. Do(ctx) |
Analyze and print the results from the API call
Review the results by reading the printout of errors the Elasticsearch API call returned.
Continue to the next iteration.
1 2 3 4 5 6 7 8 9 10 | // Check if the IndicesExistsService. Do() method returned any errors if err != nil { log. Fatalf("IndicesExistsService. Do() ERROR:", err) // Print the index exists boolean response } else { fmt. Println("nIndicesExistsService. Do():", index, "exists:", exist) } } } |
Confirm that several indexes exist
- Open a terminal window and run the script to verify that more than one Elasticsearch index exists Golang Olivere. The command
go run
in Golang is what you use to put the script together and run it. The result should look something like this:
1 2 3 4 5 | IndicesExistsService. Do(): some_index exists: true IndicesExistsService. Do(): second_index exists: false IndicesExistsService. Do(): last_index exists: false |
Conclusion
In this tutorial, you learned how to determine if Elasticsearch index exists Golang Olivere. Use the Golang Olivere driver for Elasticsearch to help you increase productivity. Verifying if indexes are already present must be accomplished before you attempt to change an index or you can waste precious time in creating detailed queries, receiving the responses you need, and modifying indexes.
Just the Code
Here’s the entire sample code to check Elasticsearch index exists Golang Olivere driver.
`
go
package main
import ( “context” “fmt” “log” // log errors “reflect” // get client attributes “time” // set timeout for connection
// Import the Olivere Golang driver for Elasticsearch “github.com/olivere/elastic/v7” )
func main() {
// Allow for custom formatting of log output log. SetFlags(0)
// Use the Olivere package to get the Elasticsearch version number fmt. Println(“Version:”, elastic. Version)
// Create a context object for the API calls ctx := context. Background()
// Declare a client instance of the Olivere driver client, err := elastic. NewClient( elastic. SetSniff(true), elastic. SetURL(“http://localhost:9200”), elastic. SetHealthcheckInterval(5*time. Second), // quit trying after 5 seconds )
// Check and see if olivere’s NewClient() method returned an error if err != nil { fmt. Println(“elastic. NewClient() ERROR:”, err) log. Fatalf(“quiting connection..”) } else { // Print client information fmt. Println(“client:”, client) fmt. Println(“client TYPE:”, reflect. TypeOf(client)) }
// Declare index name as string indexName := “some_index”
// Declare a slice of strings for the index names to be checked indices := []string{}
// Use the append() function to add more index names to the slice indices = append(indices, indexName) indices = append(indices, “second_index”) indices = append(indices, “last_index”)
// Instantiate a new *elastic. IndicesExistsService object existService := elastic. NewIndicesExistsService(client) fmt. Println(“existService TYPE:”, reflect. TypeOf(existService))
// Iterate over the IndicesExistsService object’s methods t := reflect. TypeOf(existService) for i := 0; i < t. NumMethod(); i++ { method := t. Method(i) fmt. Println(“nexist METHOD NAME:”, i, method. Name) fmt. Println(“method:”, method) }
// Iterate over the slice of Elasticsearch documents for _, index := range indices {
indexSlice := []string{index}
// Pass the string slice with the index name to the Index() method existService. Index(indexSlice)
// Bool option for checking just local node or master node as well existService. Local(true)
// Have Do() return an API response by passing the Context object to the method call exist, err := existService. Do(ctx)
// Check if the IndicesExistsService. Do() method returned any errors if err != nil { log. Fatalf(“IndicesExistsService. Do() ERROR:”, err)
// Print the index exists boolean response
} else {
fmt. Println(“nIndicesExistsService. Do():”, index, “exists:”, exist)
}
}
}
`
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started