esclient.go 674 B

12345678910111213141516171819202122232425262728293031
  1. package es
  2. import (
  3. "crypto/tls"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "time"
  8. "github.com/elastic/go-elasticsearch/v8"
  9. )
  10. func GetEsClient(esHost string, esPort int64, esUsername, esPassword string) (*elasticsearch.Client, error) {
  11. cfg := elasticsearch.Config{
  12. Addresses: []string{
  13. fmt.Sprintf("http://%s:%d", esHost, esPort),
  14. },
  15. Username: esUsername,
  16. Password: esPassword,
  17. Transport: &http.Transport{
  18. MaxIdleConnsPerHost: 10,
  19. ResponseHeaderTimeout: time.Second,
  20. DialContext: (&net.Dialer{Timeout: time.Second}).DialContext,
  21. TLSClientConfig: &tls.Config{
  22. MinVersion: tls.VersionTLS12,
  23. },
  24. },
  25. }
  26. return elasticsearch.NewClient(cfg)
  27. }