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

Method setup

_examples/bulk/benchmarks/runner/runner.go:136–183  ·  view source on GitHub ↗

setup re-creates the index for a benchmark run.

()

Source from the content-addressed store, hash-verified

134
135// setup re-creates the index for a benchmark run.
136func (r *Runner) setup() error {
137 fm, err := os.Open(filepath.Join("data", r.config.DatasetName, "mapping.json"))
138 if err != nil {
139 return fmt.Errorf("setup: reading mapping: %s", err)
140 }
141
142 var mappingEnvelope map[string]interface{}
143 json.NewDecoder(fm).Decode(&mappingEnvelope)
144 mapping, err := json.Marshal(mappingEnvelope["mappings"])
145 if err != nil {
146 return fmt.Errorf("setup: encoding mapping: %s", err)
147 }
148
149 r.indexSettings.WriteString(`{ "settings": `)
150 fmt.Fprintf(&r.indexSettings, `{"number_of_shards": %d, "number_of_replicas": %d, "refresh_interval":"5s"}`, r.config.NumShards, r.config.NumReplicas)
151 r.indexSettings.WriteString(`, "mappings":`)
152 r.indexSettings.Write(mapping)
153 r.indexSettings.WriteString(`}`)
154
155 f, err := os.Open(filepath.Join("data", r.config.DatasetName, "document.json"))
156 if err != nil {
157 return fmt.Errorf("setup: reading document: %s", err)
158 }
159 var m map[string]interface{}
160 json.NewDecoder(f).Decode(&m)
161 doc, err := json.Marshal(m)
162 if err != nil {
163 return fmt.Errorf("setup: encoding document: %s", err)
164 }
165 r.doc = doc
166
167 r.config.Client.Indices.Delete(
168 []string{r.config.IndexName},
169 r.config.Client.Indices.Delete.WithIgnoreUnavailable(true))
170 res, err := r.config.Client.Indices.Create(
171 r.config.IndexName,
172 r.config.Client.Indices.Create.WithBody(strings.NewReader(r.indexSettings.String())),
173 r.config.Client.Indices.Create.WithWaitForActiveShards("1"))
174 if err != nil {
175 return fmt.Errorf("setup: encoding document: %s", err)
176 }
177 res.Body.Close()
178 if res.IsError() {
179 return fmt.Errorf("setup: encoding document: %s", res.String())
180 }
181
182 return nil
183}
184
185// run executes a single benchmark run n, recording stats when measure is true.
186func (r *Runner) run(n int, measure bool) error {

Callers 1

runMethod · 0.95

Calls 11

OpenMethod · 0.80
JoinMethod · 0.80
IsErrorMethod · 0.80
CloseMethod · 0.65
WriteMethod · 0.45
DeleteMethod · 0.45
WithIgnoreUnavailableMethod · 0.45
CreateMethod · 0.45
WithBodyMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected