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

Function setupDemoData

_examples/msearchtemplate/default.go:79–131  ·  view source on GitHub ↗
(ctx context.Context, es *elasticsearch.Client)

Source from the content-addressed store, hash-verified

77}
78
79func setupDemoData(ctx context.Context, es *elasticsearch.Client) {
80 deleteRes, err := es.Indices.Delete([]string{demoIndex}, es.Indices.Delete.WithContext(ctx))
81 if err == nil && deleteRes != nil {
82 deleteRes.Body.Close()
83 }
84
85 createRes, err := es.Indices.Create(demoIndex, es.Indices.Create.WithContext(ctx))
86 if err != nil {
87 log.Fatalf("Error creating index: %s", err)
88 }
89 defer createRes.Body.Close()
90 if createRes.IsError() {
91 log.Fatalf("Error creating index [%s]: %s", demoIndex, createRes.String())
92 }
93
94 docs := []string{
95 `{"title":"Go and Elasticsearch","category":"tech"}`,
96 `{"title":"Elasticsearch templates in practice","category":"tech"}`,
97 `{"title":"Travel plans for spring","category":"lifestyle"}`,
98 }
99
100 for i, doc := range docs {
101 id := fmt.Sprintf("%d", i+1)
102 res, err := es.Index(
103 demoIndex,
104 strings.NewReader(doc),
105 es.Index.WithContext(ctx),
106 es.Index.WithDocumentID(id),
107 )
108 if err != nil {
109 log.Fatalf("Error indexing document %s: %s", id, err)
110 }
111 if res.IsError() {
112 res.Body.Close()
113 log.Fatalf("Error indexing document %s: %s", id, res.String())
114 }
115 res.Body.Close()
116 }
117
118 refreshRes, err := es.Indices.Refresh(
119 es.Indices.Refresh.WithContext(ctx),
120 es.Indices.Refresh.WithIndex(demoIndex),
121 )
122 if err != nil {
123 log.Fatalf("Error refreshing index: %s", err)
124 }
125 defer refreshRes.Body.Close()
126 if refreshRes.IsError() {
127 log.Fatalf("Error refreshing index [%s]: %s", demoIndex, refreshRes.String())
128 }
129
130 log.Printf("Prepared demo index [%s] with %d documents", demoIndex, len(docs))
131}
132
133func runEsapiExample(ctx context.Context, es *elasticsearch.Client) {
134 var ndjson strings.Builder

Callers 1

mainFunction · 0.85

Calls 11

IsErrorMethod · 0.80
PrintfMethod · 0.80
CloseMethod · 0.65
DeleteMethod · 0.45
WithContextMethod · 0.45
CreateMethod · 0.45
StringMethod · 0.45
IndexMethod · 0.45
WithDocumentIDMethod · 0.45
RefreshMethod · 0.45
WithIndexMethod · 0.45

Tested by

no test coverage detected