本文整理汇总了Golang中gopkg/in/olivere/elastic/v3.NewClient函数的典型用法代码示例。如果您正苦于以下问题:Golang NewClient函数的具体用法?Golang NewClient怎么用?Golang NewClient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewClient函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ExampleClient_NewClient_cluster
func ExampleClient_NewClient_cluster() {
// Obtain a client for an Elasticsearch cluster of two nodes,
// running on 10.0.1.1 and 10.0.1.2.
client, err := elastic.NewClient(elastic.SetURL("http://10.0.1.1:9200", "http://10.0.1.2:9200"))
if err != nil {
// Handle error
panic(err)
}
_ = client
}
示例2: InitClient
// InitClient sets up the elastic client. If the client has already been
// initalized it is a noop
func (e ElasticHosts) InitClient() error {
if esClient == nil {
var err error
esClient, err = elastic.NewClient(elastic.SetURL(e...), elastic.SetMaxRetries(10))
if err != nil {
return err
}
}
return nil
}
示例3: InitBackend
func (e *Elastic) InitBackend() error {
var err error
var ec *elastic.Client
if e.simpleClient {
ec, err = elastic.NewSimpleClient(elastic.SetURL(e.urls...))
} else {
ec, err = elastic.NewClient(elastic.SetURL(e.urls...))
}
if err != nil {
return err
}
e.Client = ec
exists, err := e.IndexExists(e.index).Do()
if err != nil {
return err
}
if !exists {
res, err := e.CreateIndex(e.index).Do()
if (res != nil && !res.Acknowledged) || err != nil {
return fmt.Errorf("failed to create elastic mapping (ack: %v): %v", res != nil && res.Acknowledged, err)
}
}
stringNA := map[string]string{
"type": "string",
"index": "not_analyzed",
}
stringA := map[string]string{
"type": "string",
}
date := map[string]string{
"type": "date",
}
p := make(map[string]interface{})
p[annotate.Message] = stringA
p[annotate.StartDate] = date
p[annotate.EndDate] = date
p[annotate.Source] = stringNA
p[annotate.Host] = stringNA
p[annotate.CreationUser] = stringNA
p[annotate.Owner] = stringNA
p[annotate.Category] = stringNA
p[annotate.Url] = stringNA
mapping := make(map[string]interface{})
mapping["properties"] = p
q := e.PutMapping().Index(e.index).Type(docType).BodyJson(mapping)
res, err := q.Do()
if (res != nil && !res.Acknowledged) || err != nil {
return fmt.Errorf("failed to create elastic mapping (ack: %v): %v", res != nil && res.Acknowledged, err)
}
e.initialized = true
return nil
}
示例4: New
func New() (*ElasticWReport, error) {
client, err := el.NewClient()
if err != nil {
return nil, fmt.Errorf("NewClient: %s", err.Error())
}
info, code, err := client.Ping(el.DefaultURL).Do()
if err != nil {
return nil, fmt.Errorf("Ping: %s", err.Error())
}
fmt.Printf("Elasticsearch returned with code %d and version %s", code, info.Version.Number)
return &ElasticWReport{client, "", ""}, nil
}
示例5: ExampleClient_NewClient_default
func ExampleClient_NewClient_default() {
// Obtain a client to the Elasticsearch instance on http://127.0.0.1:9200.
client, err := elastic.NewClient()
if err != nil {
// Handle error
fmt.Printf("connection failed: %v\n", err)
} else {
fmt.Println("connected")
}
_ = client
// Output:
// connected
}
示例6: ExampleGetTemplateService
func ExampleGetTemplateService() {
client, err := elastic.NewClient()
if err != nil {
panic(err)
}
// Get template stored under "my-search-template"
resp, err := client.GetTemplate().Id("my-search-template").Do()
if err != nil {
panic(err)
}
fmt.Printf("search template is: %q\n", resp.Template)
}
示例7: ExampleClusterStateService
func ExampleClusterStateService() {
client, err := elastic.NewClient()
if err != nil {
panic(err)
}
// Get cluster state
res, err := client.ClusterState().Metric("version").Do()
if err != nil {
panic(err)
}
fmt.Printf("Cluster %q has version %d", res.ClusterName, res.Version)
}
示例8: ExampleSearchResult
func ExampleSearchResult() {
client, err := elastic.NewClient()
if err != nil {
panic(err)
}
// Do a search
searchResult, err := client.Search().Index("twitter").Query(elastic.NewMatchAllQuery()).Do()
if err != nil {
panic(err)
}
// searchResult is of type SearchResult and returns hits, suggestions,
// and all kinds of other information from Elasticsearch.
fmt.Printf("Query took %d milliseconds\n", searchResult.TookInMillis)
// Each is a utility function that iterates over hits in a search result.
// It makes sure you don't need to check for nil values in the response.
// However, it ignores errors in serialization. If you want full control
// over iterating the hits, see below.
var ttyp Tweet
for _, item := range searchResult.Each(reflect.TypeOf(ttyp)) {
t := item.(Tweet)
fmt.Printf("Tweet by %s: %s\n", t.User, t.Message)
}
fmt.Printf("Found a total of %d tweets\n", searchResult.TotalHits())
// Here's how you iterate hits with full control.
if searchResult.Hits.TotalHits > 0 {
fmt.Printf("Found a total of %d tweets\n", searchResult.Hits.TotalHits)
// Iterate through results
for _, hit := range searchResult.Hits.Hits {
// hit.Index contains the name of the index
// Deserialize hit.Source into a Tweet (could also be just a map[string]interface{}).
var t Tweet
err := json.Unmarshal(*hit.Source, &t)
if err != nil {
// Deserialization failed
}
// Work with tweet
fmt.Printf("Tweet by %s: %s\n", t.User, t.Message)
}
} else {
// No hits
fmt.Print("Found no tweets\n")
}
}
示例9: ExampleSearchService
func ExampleSearchService() {
// Get a client to the local Elasticsearch instance.
client, err := elastic.NewClient()
if err != nil {
// Handle error
panic(err)
}
// Search with a term query
termQuery := elastic.NewTermQuery("user", "olivere")
searchResult, err := client.Search().
Index("twitter"). // search in index "twitter"
Query(termQuery). // specify the query
Sort("user", true). // sort by "user" field, ascending
From(0).Size(10). // take documents 0-9
Pretty(true). // pretty print request and response JSON
Do() // execute
if err != nil {
// Handle error
panic(err)
}
// searchResult is of type SearchResult and returns hits, suggestions,
// and all kinds of other information from Elasticsearch.
fmt.Printf("Query took %d milliseconds\n", searchResult.TookInMillis)
// Number of hits
if searchResult.Hits.TotalHits > 0 {
fmt.Printf("Found a total of %d tweets\n", searchResult.Hits.TotalHits)
// Iterate through results
for _, hit := range searchResult.Hits.Hits {
// hit.Index contains the name of the index
// Deserialize hit.Source into a Tweet (could also be just a map[string]interface{}).
var t Tweet
err := json.Unmarshal(*hit.Source, &t)
if err != nil {
// Deserialization failed
}
// Work with tweet
fmt.Printf("Tweet by %s: %s\n", t.User, t.Message)
}
} else {
// No hits
fmt.Print("Found no tweets\n")
}
}
示例10: ExampleDeleteTemplateService
func ExampleDeleteTemplateService() {
client, err := elastic.NewClient()
if err != nil {
panic(err)
}
// Delete template
resp, err := client.DeleteTemplate().Id("my-search-template").Do()
if err != nil {
panic(err)
}
if resp != nil && resp.Found {
fmt.Println("template deleted")
}
}
示例11: ExampleClusterHealthService
func ExampleClusterHealthService() {
client, err := elastic.NewClient()
if err != nil {
panic(err)
}
// Get cluster health
res, err := client.ClusterHealth().Index("twitter").Do()
if err != nil {
panic(err)
}
if res == nil {
panic(err)
}
fmt.Printf("Cluster status is %q\n", res.Status)
}
示例12: ExampleIndexExistsService
func ExampleIndexExistsService() {
// Get a client to the local Elasticsearch instance.
client, err := elastic.NewClient()
if err != nil {
// Handle error
panic(err)
}
// Use the IndexExists service to check if the index "twitter" exists.
exists, err := client.IndexExists("twitter").Do()
if err != nil {
// Handle error
panic(err)
}
if exists {
// ...
}
}
示例13: ExampleClusterHealthService_WaitForGreen
func ExampleClusterHealthService_WaitForGreen() {
client, err := elastic.NewClient()
if err != nil {
panic(err)
}
// Wait for status green
res, err := client.ClusterHealth().WaitForStatus("green").Timeout("15s").Do()
if err != nil {
panic(err)
}
if res.TimedOut {
fmt.Printf("time out waiting for cluster status %q\n", "green")
} else {
fmt.Printf("cluster status is %q\n", res.Status)
}
}
示例14: ExampleDeleteIndexService
func ExampleDeleteIndexService() {
// Get a client to the local Elasticsearch instance.
client, err := elastic.NewClient()
if err != nil {
// Handle error
panic(err)
}
// Delete an index.
deleteIndex, err := client.DeleteIndex("twitter").Do()
if err != nil {
// Handle error
panic(err)
}
if !deleteIndex.Acknowledged {
// Not acknowledged
}
}
示例15: ExampleAggregations
func ExampleAggregations() {
// Get a client to the local Elasticsearch instance.
client, err := elastic.NewClient()
if err != nil {
// Handle error
panic(err)
}
// Create an aggregation for users and a sub-aggregation for a date histogram of tweets (per year).
timeline := elastic.NewTermsAggregation().Field("user").Size(10).OrderByCountDesc()
histogram := elastic.NewDateHistogramAggregation().Field("created").Interval("year")
timeline = timeline.SubAggregation("history", histogram)
// Search with a term query
searchResult, err := client.Search().
Index("twitter"). // search in index "twitter"
Query(elastic.NewMatchAllQuery()). // return all results, but ...
SearchType("count"). // ... do not return hits, just the count
Aggregation("timeline", timeline). // add our aggregation to the query
Pretty(true). // pretty print request and response JSON
Do() // execute
if err != nil {
// Handle error
panic(err)
}
// Access "timeline" aggregate in search result.
agg, found := searchResult.Aggregations.Terms("timeline")
if !found {
log.Fatalf("we sould have a terms aggregation called %q", "timeline")
}
for _, userBucket := range agg.Buckets {
// Every bucket should have the user field as key.
user := userBucket.Key
// The sub-aggregation history should have the number of tweets per year.
histogram, found := userBucket.DateHistogram("history")
if found {
for _, year := range histogram.Buckets {
fmt.Printf("user %q has %d tweets in %q\n", user, year.DocCount, year.KeyAsString)
}
}
}
}