Elasticsearch CreateIndex() 缺少参数。

huangapple go评论86阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2016年11月15日 21:14:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/40610757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定