github.com/olivere/elastic/v7库无法连接带账号密码的ES库

我使用go的github.com/olivere/elastic/v7库连接es,但是无法连接带密码的es库,代码如下:
`

client, err = elastic.NewClient(
    // elastic.SetSniff(false),
    elastic.SetHealthcheckInterval(10*time.Second),
    elastic.SetMaxRetries(3),
    elastic.SetURL("http://xx:9200"),
    elastic.SetBasicAuth("username", "password"),
)

`
报错是:health check timeout: no Elasticsearch node available

请问是我初始化有问题还是这个库不支持带密码的es?
谢谢!!

阅读 9.3k
3 个回答

问题已解决!!
原因是,es集群上根据不同索引创建了不同的用户,官方库可以使用小权限用户连接集群,而olivere需要使用最大权限账户才可以连接。

olivere可以使用小权限的账户,但是需要在连接的时候指定index,并且指定index后,查询时就不能再指定index,比如:

    client, err := elastic.NewClient(
        elastic.SetBasicAuth("user", "password"),
        elastic.SetURL("http://xxx:9200/index1"), //指定index授权
        elastic.SetSniff(false),
        elastic.SetHealthcheckInterval(10*time.Second),
        elastic.SetMaxRetries(5),
        elastic.SetErrorLog(log.New(os.Stderr, "ELASTIC ", log.LstdFlags)),
        elastic.SetInfoLog(log.New(os.Stdout, "", log.LstdFlags)),
    )
    if err != nil {
        // Handle error
        panic(err)
    }

    ctx := context.Background()
    searchResult, err := client.Search().
        //Index("index1").  // 此时不可再指定index
        //Query(termQuery). // 指定查询条件
        //Sort("id", true). // 按id升序排序
        //From(0).Size(10). // 拿前10个结果
        Pretty(true).
        Do(ctx) // 执行
    if err != nil {
        panic(err)
    }
    total := searchResult.TotalHits()
    fmt.Printf("Found %d subjects\n", total)
    if total > 0 {
        for _, item := range searchResult.Each(reflect.TypeOf(ESObject)) {
            if t, ok := item.(ESObject); ok {
                fmt.Printf("Found: ESObject(id=%d)\n", t.id)
            }
        }

    } else {
        fmt.Println("Not found!")
    }
新手上路,请多包涵

olivere能使用小权限的账户吗,现在只有小权限账户怎么解决

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题