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

Function main

_examples/fasthttp/cmd/main.go:52–121  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

50}
51
52func main() {
53 log.SetFlags(0)
54
55 es, err := elasticsearch.New(
56 elasticsearch.WithTransportOptions(elastictransport.WithTransport(&fasthttp.Transport{})),
57 )
58 if err != nil {
59 log.Fatalf("Error creating the client: %s", err)
60 }
61 defer func() {
62 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
63 defer cancel()
64 if err := es.Close(ctx); err != nil {
65 fmt.Printf("Error closing the client: %s\n", err)
66 }
67 }()
68
69 // Test sending the body as POST
70 //
71 ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
72 defer cancel()
73
74 go func() {
75 res, err := es.Search(
76 es.Search.WithBody(strings.NewReader(`{"query":{"match":{"title":"foo"}}}`)),
77 es.Search.WithPretty(),
78 )
79 cancel()
80 if err != nil {
81 log.Fatalf("Error getting response: %s", err)
82 }
83 defer res.Body.Close()
84 if res.IsError() {
85 log.Fatalf("Error response: %s", res)
86 }
87 }()
88
89 select {
90 case <-ctx.Done():
91 if ctx.Err() != context.Canceled {
92 log.Fatalf("Timeout: %s", ctx.Err())
93 }
94 }
95
96 // Run benchmark
97 //
98 t := time.Now()
99 for i := 0; i < count; i++ {
100 t0 := time.Now()
101 res, err := es.Info()
102 durations = append(durations, time.Now().Sub(t0))
103 if err != nil {
104 log.Fatalf("Error: %s", err)
105 }
106 res.Body.Close()
107 }
108
109 sorted := append(make([]time.Duration, 0), durations...)

Callers

nothing calls this directly

Calls 9

PrintfMethod · 0.80
IsErrorMethod · 0.80
CloseMethod · 0.65
WithTimeoutMethod · 0.45
SearchMethod · 0.45
WithBodyMethod · 0.45
WithPrettyMethod · 0.45
InfoMethod · 0.45
SliceMethod · 0.45

Tested by

no test coverage detected