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

Function main

_examples/encoding/easyjson.go:54–167  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

52}
53
54func main() {
55 es, err := elasticsearch.New()
56 if err != nil {
57 fmt.Printf("Error creating the client: %s\n", err)
58 os.Exit(2)
59 }
60 defer func() {
61 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
62 defer cancel()
63 if err := es.Close(ctx); err != nil {
64 fmt.Printf("Error closing the client: %s\n", err)
65 }
66 }()
67
68 fnames = []string{"Alice", "John", "Mary"}
69
70 for i, title := range []string{"One", "Two", "Three", "Four", "Five"} {
71 articles = append(articles,
72 model.Article{
73 ID: uint(i + 1),
74 Title: "Test " + title,
75 Body: "Lorem ipsum dolor sit amet, consectetur adipisicing elit",
76 Published: time.Now().AddDate(i, 0, 0),
77 Author: &model.Author{
78 FirstName: fnames[rand.Intn(len(fnames))],
79 LastName: "Smith",
80 },
81 })
82 }
83
84 faint.Println("Indexing articles...")
85 faint.Println(strings.Repeat("━", 80))
86
87 var b bytes.Buffer
88 for _, a := range articles {
89 b.Reset()
90
91 // Encode article to JSON
92 if _, err := easyjson.MarshalToWriter(a, &b); err != nil {
93 red.Println("Error decoding response", err)
94 continue
95 }
96
97 res, err := es.Index(
98 "articles",
99 bytes.NewReader(b.Bytes()),
100 es.Index.WithDocumentID(strconv.Itoa(int(a.ID))),
101 // es.Index.WithVersion(-1), // <-- Uncomment to trigger error response
102 )
103 if err != nil {
104 red.Printf("Error indexing article: %s\n", err)
105 continue
106 }
107 defer res.Body.Close()
108
109 if res.IsError() {
110 printErrorResponse(res)
111 continue

Callers

nothing calls this directly

Calls 15

printErrorResponseFunction · 0.85
PrintfMethod · 0.80
IsErrorMethod · 0.80
JoinMethod · 0.80
CloseMethod · 0.65
UnmarshalFromReaderMethod · 0.65
WithTimeoutMethod · 0.45
IndexMethod · 0.45
BytesMethod · 0.45
WithDocumentIDMethod · 0.45
StatusMethod · 0.45
RefreshMethod · 0.45

Tested by

no test coverage detected