Skip to content

Instantly share code, notes, and snippets.

@olivere
Created February 12, 2020 14:01
Show Gist options
  • Save olivere/4a18aaf6ea4d503ce1e575e11a815b6e to your computer and use it in GitHub Desktop.
Save olivere/4a18aaf6ea4d503ce1e575e11a815b6e to your computer and use it in GitHub Desktop.
Connect to ES6
module example.com/es6
go 1.13
require (
github.com/mailru/easyjson v0.7.0 // indirect
github.com/olivere/elastic v6.2.27+incompatible
github.com/pkg/errors v0.9.1 // indirect
)
github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM=
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
github.com/olivere/elastic v6.2.27+incompatible h1:c57kY8PF/J6Iz2ATxHQkWFNkYyKDlEZr6hl/O5ZFNvQ=
github.com/olivere/elastic v6.2.27+incompatible/go.mod h1:J+q1zQJTgAz9woqsbVRqGeB5G1iqDKVBWLNSYW8yfJ8=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
// Connect simply connects to Elasticsearch.
//
// Example
//
//
// connect -url=http://127.0.0.1:9200 -sniff=false
//
package main
import (
"flag"
"fmt"
"log"
"github.com/olivere/elastic"
)
func main() {
var (
url = flag.String("url", "http://localhost:9200", "Elasticsearch URL")
sniff = flag.Bool("sniff", true, "Enable or disable sniffing")
)
flag.Parse()
log.SetFlags(0)
if *url == "" {
*url = "http://127.0.0.1:9200"
}
// Create an Elasticsearch client
client, err := elastic.NewClient(elastic.SetURL(*url), elastic.SetSniff(*sniff))
if err != nil {
log.Fatal(err)
}
_ = client
// Just a status message
fmt.Println("Connection succeeded")
}
$ go mod init example.com/es6
go: creating new go.mod: module example.com/es6
$ go get
$ go build
$ ls -al
total 15648
drwxr-xr-x 6 oliver staff 192 Feb 12 14:58 ./
drwxr-xr-x 34 oliver staff 1088 Feb 12 14:56 ../
-rwxr-xr-x 1 oliver staff 7997628 Feb 12 14:58 es6*
-rw-r--r-- 1 oliver staff 183 Feb 12 14:58 go.mod
-rw-r--r-- 1 oliver staff 531 Feb 12 14:58 go.sum
-rw-r--r-- 1 oliver staff 868 Feb 12 14:57 main.go
$ ./es6 -url=http://127.0.0.1:9200 -sniff=false
Connection succeeded
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment