MCPcopy
hub / github.com/elastic/go-elasticsearch / main

Function main

_examples/main.go:39–189  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

37)
38
39func main() {
40 log.SetFlags(0)
41
42 var (
43 r map[string]interface{}
44 wg sync.WaitGroup
45 )
46
47 // Initialize a client with the default settings.
48 //
49 // An `ELASTICSEARCH_URL` environment variable will be used when exported.
50 //
51 es, err := elasticsearch.New()
52 if err != nil {
53 log.Fatalf("Error creating the client: %s", err)
54 }
55 defer func() {
56 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
57 defer cancel()
58 if err := es.Close(ctx); err != nil {
59 log.Fatalf("Error closing the client: %s\n", err)
60 }
61 }()
62 // 1. Get cluster info
63 //
64 res, err := es.Info()
65 if err != nil {
66 log.Fatalf("Error getting response: %s", err)
67 }
68 defer res.Body.Close()
69 // Check response status
70 if res.IsError() {
71 log.Fatalf("Error: %s", res.String())
72 }
73 // Deserialize the response into a map.
74 if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
75 log.Fatalf("Error parsing the response body: %s", err)
76 }
77 // Print client and server version numbers.
78 log.Printf("Client: %s", elasticsearch.Version)
79 log.Printf("Server: %s", r["version"].(map[string]interface{})["number"])
80 log.Println(strings.Repeat("~", 37))
81
82 // 2. Index documents concurrently
83 //
84 for i, title := range []string{"Test One", "Test Two"} {
85 wg.Add(1)
86
87 go func(i int, title string) {
88 defer wg.Done()
89
90 // Build the request body.
91 data, err := json.Marshal(struct {
92 Title string `json:"title"`
93 }{Title: title})
94 if err != nil {
95 log.Fatalf("Error marshaling document: %s", err)
96 }

Callers

nothing calls this directly

Calls 15

DoMethod · 0.95
IsErrorMethod · 0.80
PrintfMethod · 0.80
CloseMethod · 0.65
AddMethod · 0.65
WithTimeoutMethod · 0.45
InfoMethod · 0.45
StringMethod · 0.45
StatusMethod · 0.45
SearchMethod · 0.45
WithContextMethod · 0.45
WithIndexMethod · 0.45

Tested by

no test coverage detected