Created
July 27, 2015 09:45
-
-
Save olivere/eaee7a20524ab2beba71 to your computer and use it in GitHub Desktop.
Elastic: Sharing a client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"sync" | |
elastic "gopkg.in/olivere/elastic.v2" | |
) | |
var ( | |
clientInit sync.Once | |
client *elastic.Client | |
) | |
func Client() *elastic.Client { | |
clientInit.Do(func() { | |
c, err := elastic.NewClient() | |
if err != nil { | |
panic(err) | |
} | |
client = c | |
}) | |
return client | |
} | |
func main() { | |
exists, err := Client().IndexExists("name").Do() | |
if err != nil { | |
log.Fatal(err) | |
} | |
if !exists { | |
log.Print("index does not exist") | |
} else { | |
log.Print("index already exists") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment