英文:
Elasticsearch CreateIndex() not enough arguments
问题
我正在尝试使用这个著名的仓库中的Elasticsearch for GO。
然而,当我尝试创建一个index
(文档,也可以在这里找到示例)时,我遇到了以下我不理解的错误:
> 调用client.CreateIndex("events")时参数不足。
为什么会这样?我在这里漏掉了什么?
英文:
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?
答案1
得分: 4
IndicesCreateService.Do()
函数 需要传入一个 context.Context
。
所以,你需要导入 "golang.org/x/net/context"
,然后将你的调用改为以下形式:
import (
... 你的其他导入...
"golang.org/x/net/context"
)
...
_, err := client.CreateIndex("events").Do(context.TODO())
你还可以查看 indices_create_test.go
中的测试用例,以了解如何使用它。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论