I am trying to use Elasticsearch for GO with this well-known repo
However, when I am trying to create an index
(docs, and also given as an example here):
// Define an elastic client
client, err := elastic.NewClient(elastic.SetURL("host1"))
if err != nil {
client, err := elastic.NewClient(elastic.SetURL("host2"))
if err != nil {
fmt.Println("Error when connecting Elasticsearch host");
}
}
// Create an index
_, err = client.CreateIndex("events").Do()
if err != nil {
fmt.Println("Error when creating Elasticsearch index");
panic(err)
}
I got the following error, which I do not understand:
not enough arguments in call to client.CreateIndex("events").Do
Why is that? What do I miss here?
The IndicesCreateService.Do()
function expects a context.Context
to be passed.
So, you need to import "golang.org/x/net/context"
and then change your call to this:
import (
... your other imports...
"golang.org/x/net/context"
)
...
_, err := client.CreateIndex("events").Do(context.TODO())
^
|
add this
You can also check the indices_create_test.go
test case in order to see how it's done.