Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG unknown field 'Username' ,'Password' in struct literal of type elasticsearch.Config #68

Closed
tx991020 opened this issue Jul 19, 2019 · 9 comments

Comments

@tx991020
Copy link

tx991020 commented Jul 19, 2019

111/main.go:34:3: unknown field 'Username' in struct literal of type elasticsearch.Config
111/main.go:35:3: unknown field 'Password' in struct literal of type elasticsearch.Config
111/main.go:36:3: unknown field 'CloudID' in struct literal of type elasticsearch.Config
111/main.go:37:3: unknown field 'APIKey' in struct literal of type elasticsearch.Config

func main() {
  config1 := elasticsearch.Config{
  Addresses: []string{
    "http://elasticsearch.aliyuncs.com:9200",
  },
  Username:  "elastic",
  Password:  "elastic1",
  CloudID:   "",
  APIKey:    "",
  Transport: &http.Transport{
    MaxIdleConnsPerHost:   10,
    ResponseHeaderTimeout: time.Second,
    DialContext:           (&net.Dialer{Timeout: time.Second}).DialContext,
    TLSClientConfig: &tls.Config{
      MinVersion: tls.VersionTLS11,
      // ...
    },
  },
    //Logger:    nil,
  }
  
  es, err := elasticsearch.NewClient(config1)
  fmt.Println(es,err)
}
@karmi
Copy link
Contributor

karmi commented Jul 19, 2019

Hello, first of all, why are you using a configuration with so many empty fields, and why do you customize the default transport at all?

Second, what version of Go and the package is this? Ie. can you paste the relevant import lines, and the output of elasticsearch.Version()?

@tx991020
Copy link
Author

I don't understand how go.mod works, it is go.mod not imported properly。now it is ok

@karmi
Copy link
Contributor

karmi commented Jul 19, 2019

Cool, so it's OK to close the issue?

@ghost
Copy link

ghost commented May 5, 2020

Hi karmi, there is something fishy going on here. This is what I ran :

package main
import (
	"github.com/elastic/go-elasticsearch"
	"reflect"
	"fmt"
)

func main() {
	cfg := elasticsearch.Config{}
    e := reflect.ValueOf(&cfg).Elem()
    for i := 0; i < e.NumField(); i++ {
        varName := e.Type().Field(i).Name
        varType := e.Type().Field(i).Type
        varValue := e.Field(i).Interface()
        fmt.Printf("%v %v %v\n", varName,varType,varValue)
    }
}

and this is what I get in the output:

Addresses []string []
Transport http.RoundTripper <nil>

version in go.mod file :

github.com/elastic/go-elasticsearch v0.0.0
github.com/elastic/go-elasticsearch/v8 v8.0.0-20200408073057-6f36a473b19f

Can I get any help ?

@karmi
Copy link
Contributor

karmi commented May 5, 2020

Hey! Can you remove this line from go.mod?

github.com/elastic/go-elasticsearch v0.0.0

Go perhaps resolves this to the initial version of the package, which didn't have the configuration fields. Sometimes IDEs and editor plugins mess up the import declarations.

Use the import with the version suffix — the one you have points to a recent commit on the master branch:

github.com/elastic/go-elasticsearch/v8 v8.0.0-20200408073057-6f36a473b19f

Note: In general, use a major version of the package matching the major version of Elasticsearch. For example, if you're using Elastiscsearch 7.x, use either the 7.x branch, or a specific tag.

@ghost
Copy link

ghost commented May 5, 2020

github.com/elastic/go-elasticsearch v0.0.0
The above line is auto added anyway right ?
The issue still persists..

@karmi
Copy link
Contributor

karmi commented May 5, 2020

As I'm saying, your editor might be adding this, but that line is incorrect — that points to a really old version. You have to remove the line.

@ghost
Copy link

ghost commented May 5, 2020

Got it!, Awesome. Works now after I changed that.
my go.mod file now has:
github.com/elastic/go-elasticsearch/v7 v7.5.1-0.20200429062015-142e0c281dde
Thanks Karmi!

@karmi
Copy link
Contributor

karmi commented May 5, 2020

You're welcome — glad that it's working!


Note: Go resolver is a bit funky, note that it seems to point to version v7.5.1, but if you decipher the date and the commit, it points to a recent commit. When you print elasticsearch.Version, that should tell the truth about the version you're using.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants