Debian Codegraph @

example_test.go

raw
1 // Copyright 2012-present Oliver Eilhard. All rights reserved.
2 // Use of this source code is governed by a MIT-license.
3 // See http://olivere.mit-license.org/license.txt for details.
4
5 package elastic_test
6
7 import (
8 "context"
9 "encoding/json"
10 "fmt"
11 "log"
12 "os"
13 "reflect"
14 "time"
15
16 elastic "gopkg.in/olivere/elastic.v5"
17 )
18
19 type Tweet struct {
20 User string `json:"user"`
21 Message string `json:"message"`
22 Retweets int `json:"retweets"`
23 Image string `json:"image,omitempty"`
24 Created time.Time `json:"created,omitempty"`
25 Tags []string `json:"tags,omitempty"`
26 Location string `json:"location,omitempty"`
27 Suggest *elastic.SuggestField `json:"suggest_field,omitempty"`
28 }
29
30 func Example() {
31 errorlog := log.New(os.Stdout, "APP ", log.LstdFlags)
32
33 // Obtain a client. You can also provide your own HTTP client here.
34 client, err := elastic.NewClient(elastic.SetErrorLog(errorlog))
35 if err != nil {
36 // Handle error
37 panic(err)
38 }
39
40 // Trace request and response details like this
41 //client.SetTracer(log.New(os.Stdout, "", 0))
42
43 // Ping the Elasticsearch server to get e.g. the version number
44 info, code, err := client.Ping("http://127.0.0.1:9200").Do(context.Background())
45 if err != nil {
46 // Handle error
47 panic(err)
48 }
49 fmt.Printf("Elasticsearch returned with code %d and version %s\n", code, info.Version.Number)
50
51 // Getting the ES version number is quite common, so there's a shortcut
52 esversion, err := client.ElasticsearchVersion("http://127.0.0.1:9200")
53 if err != nil {
54 // Handle error
55 panic(err)
56 }
57 fmt.Printf("Elasticsearch version %s\n", esversion)
58
59 // Use the IndexExists service to check if a specified index exists.
60 exists, err := client.IndexExists("twitter").Do(context.Background())
61 if err != nil {
62 // Handle error
63 panic(err)
64 }
65 if !exists {
66 // Create a new index.
67 mapping := `
68 {
69 "settings":{
70 "number_of_shards":1,
71 "number_of_replicas":0
72 },
73 "mappings":{
74 "_default_": {
75 "_all": {
76 "enabled": true
77 }
78 },
79 "tweet":{
80 "properties":{
81 "user":{
82 "type":"keyword"
83 },
84 "message":{
85 "type":"text",
86 "store": true,
87 "fielddata": true
88 },
89 "retweets":{
90 "type":"long"
91 },
92 "tags":{
93 "type":"keyword"
94 },
95 "location":{
96 "type":"geo_point"
97 },
98 "suggest_field":{
99 "type":"completion"
100 }
101 }
102 }
103 }
104 }
105 `
106 createIndex, err := client.CreateIndex("twitter").Body(mapping).Do(context.Background())
107 if err != nil {
108 // Handle error
109 panic(err)
110 }
111 if !createIndex.Acknowledged {
112 // Not acknowledged
113 }
114 }
115
116 // Index a tweet (using JSON serialization)
117 tweet1 := Tweet{User: "olivere", Message: "Take Five", Retweets: 0}
118 put1, err := client.Index().
119 Index("twitter").
120 Type("tweet").
121 Id("1").
122 BodyJson(tweet1).
123 Do(context.Background())
124 if err != nil {
125 // Handle error
126 panic(err)
127 }
128 fmt.Printf("Indexed tweet %s to index %s, type %s\n", put1.Id, put1.Index, put1.Type)
129
130 // Index a second tweet (by string)
131 tweet2 := `{"user" : "olivere", "message" : "It's a Raggy Waltz"}`
132 put2, err := client.Index().
133 Index("twitter").
134 Type("tweet").
135 Id("2").
136 BodyString(tweet2).
137 Do(context.Background())
138 if err != nil {
139 // Handle error
140 panic(err)
141 }
142 fmt.Printf("Indexed tweet %s to index %s, type %s\n", put2.Id, put2.Index, put2.Type)
143
144 // Get tweet with specified ID
145 get1, err := client.Get().
146 Index("twitter").
147 Type("tweet").
148 Id("1").
149 Do(context.Background())
150 if err != nil {
151 // Handle error
152 panic(err)
153 }
154 if get1.Found {
155 fmt.Printf("Got document %s in version %d from index %s, type %s\n", get1.Id, get1.Version, get1.Index, get1.Type)
156 }
157
158 // Flush to make sure the documents got written.
159 _, err = client.Flush().Index("twitter").Do(context.Background())
160 if err != nil {
161 panic(err)
162 }
163
164 // Search with a term query
165 termQuery := elastic.NewTermQuery("user", "olivere")
166 searchResult, err := client.Search().
167 Index("twitter"). // search in index "twitter"
168 Query(termQuery). // specify the query
169 Sort("user", true). // sort by "user" field, ascending
170 From(0).Size(10). // take documents 0-9
171 Pretty(true). // pretty print request and response JSON
172 Do(context.Background()) // execute
173 if err != nil {
174 // Handle error
175 panic(err)
176 }
177
178 // searchResult is of type SearchResult and returns hits, suggestions,
179 // and all kinds of other information from Elasticsearch.
180 fmt.Printf("Query took %d milliseconds\n", searchResult.TookInMillis)
181
182 // Each is a convenience function that iterates over hits in a search result.
183 // It makes sure you don't need to check for nil values in the response.
184 // However, it ignores errors in serialization. If you want full control
185 // over iterating the hits, see below.
186 var ttyp Tweet
187 for _, item := range searchResult.Each(reflect.TypeOf(ttyp)) {
188 t := item.(Tweet)
189 fmt.Printf("Tweet by %s: %s\n", t.User, t.Message)
190 }
191 // TotalHits is another convenience function that works even when something goes wrong.
192 fmt.Printf("Found a total of %d tweets\n", searchResult.TotalHits())
193
194 // Here's how you iterate through results with full control over each step.
195 if searchResult.Hits.TotalHits > 0 {
196 fmt.Printf("Found a total of %d tweets\n", searchResult.Hits.TotalHits)
197
198 // Iterate through results
199 for _, hit := range searchResult.Hits.Hits {
200 // hit.Index contains the name of the index
201
202 // Deserialize hit.Source into a Tweet (could also be just a map[string]interface{}).
203 var t Tweet
204 err := json.Unmarshal(*hit.Source, &t)
205 if err != nil {
206 // Deserialization failed
207 }
208
209 // Work with tweet
210 fmt.Printf("Tweet by %s: %s\n", t.User, t.Message)
211 }
212 } else {
213 // No hits
214 fmt.Print("Found no tweets\n")
215 }
216
217 // Update a tweet by the update API of Elasticsearch.
218 // We just increment the number of retweets.
219 script := elastic.NewScript("ctx._source.retweets += params.num").Param("num", 1)
220 update, err := client.Update().Index("twitter").Type("tweet").Id("1").
221 Script(script).
222 Upsert(map[string]interface{}{"retweets": 0}).
223 Do(context.Background())
224 if err != nil {
225 // Handle error
226 panic(err)
227 }
228 fmt.Printf("New version of tweet %q is now %d", update.Id, update.Version)
229
230 // ...
231
232 // Delete an index.
233 deleteIndex, err := client.DeleteIndex("twitter").Do(context.Background())
234 if err != nil {
235 // Handle error
236 panic(err)
237 }
238 if !deleteIndex.Acknowledged {
239 // Not acknowledged
240 }
241 }
242
243 func ExampleClient_NewClient_default() {
244 // Obtain a client to the Elasticsearch instance on http://127.0.0.1:9200.
245 client, err := elastic.NewClient()
246 if err != nil {
247 // Handle error
248 fmt.Printf("connection failed: %v\n", err)
249 } else {
250 fmt.Println("connected")
251 }
252 _ = client
253 // Output:
254 // connected
255 }
256
257 func ExampleClient_NewClient_cluster() {
258 // Obtain a client for an Elasticsearch cluster of two nodes,
259 // running on 10.0.1.1 and 10.0.1.2.
260 client, err := elastic.NewClient(elastic.SetURL("http://10.0.1.1:9200", "http://10.0.1.2:9200"))
261 if err != nil {
262 // Handle error
263 panic(err)
264 }
265 _ = client
266 }
267
268 func ExampleClient_NewClient_manyOptions() {
269 // Obtain a client for an Elasticsearch cluster of two nodes,
270 // running on 10.0.1.1 and 10.0.1.2. Do not run the sniffer.
271 // Set the healthcheck interval to 10s. When requests fail,
272 // retry 5 times. Print error messages to os.Stderr and informational
273 // messages to os.Stdout.
274 client, err := elastic.NewClient(
275 elastic.SetURL("http://10.0.1.1:9200", "http://10.0.1.2:9200"),
276 elastic.SetSniff(false),
277 elastic.SetHealthcheckInterval(10*time.Second),
278 elastic.SetMaxRetries(5),
279 elastic.SetErrorLog(log.New(os.Stderr, "ELASTIC ", log.LstdFlags)),
280 elastic.SetInfoLog(log.New(os.Stdout, "", log.LstdFlags)))
281 if err != nil {
282 // Handle error
283 panic(err)
284 }
285 _ = client
286 }
287
288 func ExampleIndexExistsService() {
289 // Get a client to the local Elasticsearch instance.
290 client, err := elastic.NewClient()
291 if err != nil {
292 // Handle error
293 panic(err)
294 }
295 // Use the IndexExists service to check if the index "twitter" exists.
296 exists, err := client.IndexExists("twitter").Do(context.Background())
297 if err != nil {
298 // Handle error
299 panic(err)
300 }
301 if exists {
302 // ...
303 }
304 }
305
306 func ExampleCreateIndexService() {
307 // Get a client to the local Elasticsearch instance.
308 client, err := elastic.NewClient()
309 if err != nil {
310 // Handle error
311 panic(err)
312 }
313 // Create a new index.
314 createIndex, err := client.CreateIndex("twitter").Do(context.Background())
315 if err != nil {
316 // Handle error
317 panic(err)
318 }
319 if !createIndex.Acknowledged {
320 // Not acknowledged
321 }
322 }
323
324 func ExampleDeleteIndexService() {
325 // Get a client to the local Elasticsearch instance.
326 client, err := elastic.NewClient()
327 if err != nil {
328 // Handle error
329 panic(err)
330 }
331 // Delete an index.
332 deleteIndex, err := client.DeleteIndex("twitter").Do(context.Background())
333 if err != nil {
334 // Handle error
335 panic(err)
336 }
337 if !deleteIndex.Acknowledged {
338 // Not acknowledged
339 }
340 }
341
342 func ExampleSearchService() {
343 // Get a client to the local Elasticsearch instance.
344 client, err := elastic.NewClient()
345 if err != nil {
346 // Handle error
347 panic(err)
348 }
349
350 // Search with a term query
351 termQuery := elastic.NewTermQuery("user", "olivere")
352 searchResult, err := client.Search().
353 Index("twitter"). // search in index "twitter"
354 Query(termQuery). // specify the query
355 Sort("user", true). // sort by "user" field, ascending
356 From(0).Size(10). // take documents 0-9
357 Pretty(true). // pretty print request and response JSON
358 Do(context.Background()) // execute
359 if err != nil {
360 // Handle error
361 panic(err)
362 }
363
364 // searchResult is of type SearchResult and returns hits, suggestions,
365 // and all kinds of other information from Elasticsearch.
366 fmt.Printf("Query took %d milliseconds\n", searchResult.TookInMillis)
367
368 // Number of hits
369 if searchResult.Hits.TotalHits > 0 {
370 fmt.Printf("Found a total of %d tweets\n", searchResult.Hits.TotalHits)
371
372 // Iterate through results
373 for _, hit := range searchResult.Hits.Hits {
374 // hit.Index contains the name of the index
375
376 // Deserialize hit.Source into a Tweet (could also be just a map[string]interface{}).
377 var t Tweet
378 err := json.Unmarshal(*hit.Source, &t)
379 if err != nil {
380 // Deserialization failed
381 }
382
383 // Work with tweet
384 fmt.Printf("Tweet by %s: %s\n", t.User, t.Message)
385 }
386 } else {
387 // No hits
388 fmt.Print("Found no tweets\n")
389 }
390 }
391
392 func ExampleAggregations() {
393 // Get a client to the local Elasticsearch instance.
394 client, err := elastic.NewClient()
395 if err != nil {
396 // Handle error
397 panic(err)
398 }
399
400 // Create an aggregation for users and a sub-aggregation for a date histogram of tweets (per year).
401 timeline := elastic.NewTermsAggregation().Field("user").Size(10).OrderByCountDesc()
402 histogram := elastic.NewDateHistogramAggregation().Field("created").Interval("year")
403 timeline = timeline.SubAggregation("history", histogram)
404
405 // Search with a term query
406 searchResult, err := client.Search().
407 Index("twitter"). // search in index "twitter"
408 Query(elastic.NewMatchAllQuery()). // return all results, but ...
409 SearchType("count"). // ... do not return hits, just the count
410 Aggregation("timeline", timeline). // add our aggregation to the query
411 Pretty(true). // pretty print request and response JSON
412 Do(context.Background()) // execute
413 if err != nil {
414 // Handle error
415 panic(err)
416 }
417
418 // Access "timeline" aggregate in search result.
419 agg, found := searchResult.Aggregations.Terms("timeline")
420 if !found {
421 log.Fatalf("we should have a terms aggregation called %q", "timeline")
422 }
423 for _, userBucket := range agg.Buckets {
424 // Every bucket should have the user field as key.
425 user := userBucket.Key
426
427 // The sub-aggregation history should have the number of tweets per year.
428 histogram, found := userBucket.DateHistogram("history")
429 if found {
430 for _, year := range histogram.Buckets {
431 var key string
432 if v := year.KeyAsString; v != nil {
433 key = *v
434 }
435 fmt.Printf("user %q has %d tweets in %q\n", user, year.DocCount, key)
436 }
437 }
438 }
439 }
440
441 func ExampleSearchResult() {
442 client, err := elastic.NewClient()
443 if err != nil {
444 panic(err)
445 }
446
447 // Do a search
448 searchResult, err := client.Search().Index("twitter").Query(elastic.NewMatchAllQuery()).Do(context.Background())
449 if err != nil {
450 panic(err)
451 }
452
453 // searchResult is of type SearchResult and returns hits, suggestions,
454 // and all kinds of other information from Elasticsearch.
455 fmt.Printf("Query took %d milliseconds\n", searchResult.TookInMillis)
456
457 // Each is a utility function that iterates over hits in a search result.
458 // It makes sure you don't need to check for nil values in the response.
459 // However, it ignores errors in serialization. If you want full control
460 // over iterating the hits, see below.
461 var ttyp Tweet
462 for _, item := range searchResult.Each(reflect.TypeOf(ttyp)) {
463 t := item.(Tweet)
464 fmt.Printf("Tweet by %s: %s\n", t.User, t.Message)
465 }
466 fmt.Printf("Found a total of %d tweets\n", searchResult.TotalHits())
467
468 // Here's how you iterate hits with full control.
469 if searchResult.Hits.TotalHits > 0 {
470 fmt.Printf("Found a total of %d tweets\n", searchResult.Hits.TotalHits)
471
472 // Iterate through results
473 for _, hit := range searchResult.Hits.Hits {
474 // hit.Index contains the name of the index
475
476 // Deserialize hit.Source into a Tweet (could also be just a map[string]interface{}).
477 var t Tweet
478 err := json.Unmarshal(*hit.Source, &t)
479 if err != nil {
480 // Deserialization failed
481 }
482
483 // Work with tweet
484 fmt.Printf("Tweet by %s: %s\n", t.User, t.Message)
485 }
486 } else {
487 // No hits
488 fmt.Print("Found no tweets\n")
489 }
490 }
491
492 func ExamplePutTemplateService() {
493 client, err := elastic.NewClient()
494 if err != nil {
495 panic(err)
496 }
497
498 // Create search template
499 tmpl := `{"template":{"query":{"match":{"title":"{{query_string}}"}}}}`
500
501 // Create template
502 resp, err := client.PutTemplate().
503 Id("my-search-template"). // Name of the template
504 BodyString(tmpl). // Search template itself
505 Do(context.Background()) // Execute
506 if err != nil {
507 panic(err)
508 }
509 if resp.Acknowledged {
510 fmt.Println("search template creation acknowledged")
511 }
512 }
513
514 func ExampleGetTemplateService() {
515 client, err := elastic.NewClient()
516 if err != nil {
517 panic(err)
518 }
519
520 // Get template stored under "my-search-template"
521 resp, err := client.GetTemplate().Id("my-search-template").Do(context.Background())
522 if err != nil {
523 panic(err)
524 }
525 fmt.Printf("search template is: %q\n", resp.Template)
526 }
527
528 func ExampleDeleteTemplateService() {
529 client, err := elastic.NewClient()
530 if err != nil {
531 panic(err)
532 }
533
534 // Delete template
535 resp, err := client.DeleteTemplate().Id("my-search-template").Do(context.Background())
536 if err != nil {
537 panic(err)
538 }
539 if resp != nil && resp.Acknowledged {
540 fmt.Println("template deleted")
541 }
542 }
543
544 func ExampleClusterHealthService() {
545 client, err := elastic.NewClient()
546 if err != nil {
547 panic(err)
548 }
549
550 // Get cluster health
551 res, err := client.ClusterHealth().Index("twitter").Do(context.Background())
552 if err != nil {
553 panic(err)
554 }
555 if res == nil {
556 panic(err)
557 }
558 fmt.Printf("Cluster status is %q\n", res.Status)
559 }
560
561 func ExampleClusterHealthService_WaitForGreen() {
562 client, err := elastic.NewClient()
563 if err != nil {
564 panic(err)
565 }
566
567 // Wait for status green
568 res, err := client.ClusterHealth().WaitForStatus("green").Timeout("15s").Do(context.Background())
569 if err != nil {
570 panic(err)
571 }
572 if res.TimedOut {
573 fmt.Printf("time out waiting for cluster status %q\n", "green")
574 } else {
575 fmt.Printf("cluster status is %q\n", res.Status)
576 }
577 }
578
579 func ExampleClusterStateService() {
580 client, err := elastic.NewClient()
581 if err != nil {
582 panic(err)
583 }
584
585 // Get cluster state
586 res, err := client.ClusterState().Metric("version").Do(context.Background())
587 if err != nil {
588 panic(err)
589 }
590 fmt.Printf("Cluster %q has version %d", res.ClusterName, res.Version)
591 }