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

Function main

_examples/encoding/jsonreader.go:35–82  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

33}
34
35func main() {
36 var (
37 res *esapi.Response
38 err error
39 )
40
41 es, err := elasticsearch.New()
42 if err != nil {
43 log.Fatalf("Error creating the client: %s", err)
44 }
45 defer func() {
46 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
47 defer cancel()
48 if err := es.Close(ctx); err != nil {
49 fmt.Printf("Error closing the client: %s\n", err)
50 }
51 }()
52
53 doc := struct {
54 Title string `json:"title"`
55 }{Title: "Test"}
56
57 res, err = es.Index("test", esutil.NewJSONReader(&doc), es.Index.WithRefresh("true"))
58 if err != nil {
59 log.Fatalf("Error getting response: %s", err)
60 }
61
62 log.Println(res)
63
64 query := map[string]interface{}{
65 "query": map[string]interface{}{
66 "match": map[string]interface{}{
67 "title": "test",
68 },
69 },
70 }
71
72 res, err = es.Search(
73 es.Search.WithIndex("test"),
74 es.Search.WithBody(esutil.NewJSONReader(&query)),
75 es.Search.WithPretty(),
76 )
77 if err != nil {
78 log.Fatalf("Error getting response: %s", err)
79 }
80
81 log.Println(res)
82}

Callers

nothing calls this directly

Calls 10

NewJSONReaderFunction · 0.92
PrintfMethod · 0.80
CloseMethod · 0.65
WithTimeoutMethod · 0.45
IndexMethod · 0.45
WithRefreshMethod · 0.45
SearchMethod · 0.45
WithIndexMethod · 0.45
WithBodyMethod · 0.45
WithPrettyMethod · 0.45

Tested by

no test coverage detected