func esClient() (*es.Client, error) { urls := strings.Split(*esURL, ",") options := []es.ClientOptionFunc{ es.SetURL(urls...), es.SetSniff(false), } if *esUser != "" { options = append(options, es.SetBasicAuth(*esUser, *esPass)) } if *esDebug { l := logger{} options = append(options, es.SetErrorLog(l), es.SetInfoLog(l), es.SetTraceLog(l), ) } client, err := es.NewClient(options...) if err != nil { return nil, err } exists, err := client.IndexExists(*esIndex).Do() if err != nil { return nil, err } if !exists { resp, err := client.CreateIndex(*esIndex).Body(mapping).Do() if err != nil { return nil, err } if !resp.Acknowledged { return nil, errors.New("index creation was not acknowledged") } logrus.Info("index created") } return client, nil }
func initElastic(www string) (*elastic.Client, error) { username, password, host := parseBonsaiURL(www) log.Infof("Initializing ES: %v.", host) client, err := elastic.NewClient(elastic.SetURL(host), elastic.SetMaxRetries(10), elastic.SetBasicAuth(username, password), elastic.SetSniff(false)) if err != nil { log.Fatalf("Error while connecting to ElasticSearch: %s", err) return nil, err } log.Info("Initializing Indices.") err = initIndices(client) if err != nil { log.Fatalf("Error while creating ElasticSearch Indices: %s", err) return nil, err } return client, err }