dongyi9082
2016-11-15 13:14
浏览 149

Elasticsearch CreateIndex()参数不足

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?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将Elasticsearch for GO与该著名的存储库

但是,当我尝试创建 index 文档,并以此处):

  //定义一个弹性客户端
客户端,err:= elastic  .NewClient(elastic.SetURL(“ host1”))
如果错误!=无{
客户端,错误:= elastic.NewClient(elastic.SetURL(“ host2”)))
如果错误!=无{
  fmt.Println(“连接Elasticsearch主机时出错”);  
} 
} 
 
 //创建索引
 _,err = client.CreateIndex(“ events”)。Do()
 if err!= nil {
 fmt.Println(“ 创建Elasticsearch索引”);  
 panic(err)
} 
   
 
 

我遇到了以下错误,我不明白:

\ n

调用client.CreateIndex(“ events”)时参数不足。

为什么? 我在这里想念什么?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyanzhou1450 2016-11-15 13:22
    已采纳

    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.

    点赞 打赏 评论

相关推荐 更多相似问题