github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.stats.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // 3 // The OpenSearch Contributors require contributions made to 4 // this file be licensed under the Apache-2.0 license or a 5 // compatible open source license. 6 // 7 // Modifications Copyright OpenSearch Contributors. See 8 // GitHub history for details. 9 10 // Licensed to Elasticsearch B.V. under one or more contributor 11 // license agreements. See the NOTICE file distributed with 12 // this work for additional information regarding copyright 13 // ownership. Elasticsearch B.V. licenses this file to you under 14 // the Apache License, Version 2.0 (the "License"); you may 15 // not use this file except in compliance with the License. 16 // You may obtain a copy of the License at 17 // 18 // http://www.apache.org/licenses/LICENSE-2.0 19 // 20 // Unless required by applicable law or agreed to in writing, 21 // software distributed under the License is distributed on an 22 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 23 // KIND, either express or implied. See the License for the 24 // specific language governing permissions and limitations 25 // under the License. 26 27 package opensearchapi 28 29 import ( 30 "context" 31 "net/http" 32 "strconv" 33 "strings" 34 ) 35 36 func newIndicesStatsFunc(t Transport) IndicesStats { 37 return func(o ...func(*IndicesStatsRequest)) (*Response, error) { 38 var r = IndicesStatsRequest{} 39 for _, f := range o { 40 f(&r) 41 } 42 return r.Do(r.ctx, t) 43 } 44 } 45 46 // ----- API Definition ------------------------------------------------------- 47 48 // IndicesStats provides statistics on operations happening in an index. 49 // 50 // 51 type IndicesStats func(o ...func(*IndicesStatsRequest)) (*Response, error) 52 53 // IndicesStatsRequest configures the Indices Stats API request. 54 // 55 type IndicesStatsRequest struct { 56 Index []string 57 58 Metric []string 59 60 CompletionFields []string 61 ExpandWildcards string 62 FielddataFields []string 63 Fields []string 64 ForbidClosedIndices *bool 65 Groups []string 66 IncludeSegmentFileSizes *bool 67 IncludeUnloadedSegments *bool 68 Level string 69 Types []string 70 71 Pretty bool 72 Human bool 73 ErrorTrace bool 74 FilterPath []string 75 76 Header http.Header 77 78 ctx context.Context 79 } 80 81 // Do executes the request and returns response or error. 82 // 83 func (r IndicesStatsRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 84 var ( 85 method string 86 path strings.Builder 87 params map[string]string 88 ) 89 90 method = "GET" 91 92 path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len("_stats") + 1 + len(strings.Join(r.Metric, ","))) 93 if len(r.Index) > 0 { 94 path.WriteString("/") 95 path.WriteString(strings.Join(r.Index, ",")) 96 } 97 path.WriteString("/") 98 path.WriteString("_stats") 99 if len(r.Metric) > 0 { 100 path.WriteString("/") 101 path.WriteString(strings.Join(r.Metric, ",")) 102 } 103 104 params = make(map[string]string) 105 106 if len(r.CompletionFields) > 0 { 107 params["completion_fields"] = strings.Join(r.CompletionFields, ",") 108 } 109 110 if r.ExpandWildcards != "" { 111 params["expand_wildcards"] = r.ExpandWildcards 112 } 113 114 if len(r.FielddataFields) > 0 { 115 params["fielddata_fields"] = strings.Join(r.FielddataFields, ",") 116 } 117 118 if len(r.Fields) > 0 { 119 params["fields"] = strings.Join(r.Fields, ",") 120 } 121 122 if r.ForbidClosedIndices != nil { 123 params["forbid_closed_indices"] = strconv.FormatBool(*r.ForbidClosedIndices) 124 } 125 126 if len(r.Groups) > 0 { 127 params["groups"] = strings.Join(r.Groups, ",") 128 } 129 130 if r.IncludeSegmentFileSizes != nil { 131 params["include_segment_file_sizes"] = strconv.FormatBool(*r.IncludeSegmentFileSizes) 132 } 133 134 if r.IncludeUnloadedSegments != nil { 135 params["include_unloaded_segments"] = strconv.FormatBool(*r.IncludeUnloadedSegments) 136 } 137 138 if r.Level != "" { 139 params["level"] = r.Level 140 } 141 142 if len(r.Types) > 0 { 143 params["types"] = strings.Join(r.Types, ",") 144 } 145 146 if r.Pretty { 147 params["pretty"] = "true" 148 } 149 150 if r.Human { 151 params["human"] = "true" 152 } 153 154 if r.ErrorTrace { 155 params["error_trace"] = "true" 156 } 157 158 if len(r.FilterPath) > 0 { 159 params["filter_path"] = strings.Join(r.FilterPath, ",") 160 } 161 162 req, err := newRequest(method, path.String(), nil) 163 if err != nil { 164 return nil, err 165 } 166 167 if len(params) > 0 { 168 q := req.URL.Query() 169 for k, v := range params { 170 q.Set(k, v) 171 } 172 req.URL.RawQuery = q.Encode() 173 } 174 175 if len(r.Header) > 0 { 176 if len(req.Header) == 0 { 177 req.Header = r.Header 178 } else { 179 for k, vv := range r.Header { 180 for _, v := range vv { 181 req.Header.Add(k, v) 182 } 183 } 184 } 185 } 186 187 if ctx != nil { 188 req = req.WithContext(ctx) 189 } 190 191 res, err := transport.Perform(req) 192 if err != nil { 193 return nil, err 194 } 195 196 response := Response{ 197 StatusCode: res.StatusCode, 198 Body: res.Body, 199 Header: res.Header, 200 } 201 202 return &response, nil 203 } 204 205 // WithContext sets the request context. 206 // 207 func (f IndicesStats) WithContext(v context.Context) func(*IndicesStatsRequest) { 208 return func(r *IndicesStatsRequest) { 209 r.ctx = v 210 } 211 } 212 213 // WithIndex - a list of index names; use _all to perform the operation on all indices. 214 // 215 func (f IndicesStats) WithIndex(v ...string) func(*IndicesStatsRequest) { 216 return func(r *IndicesStatsRequest) { 217 r.Index = v 218 } 219 } 220 221 // WithMetric - limit the information returned the specific metrics.. 222 // 223 func (f IndicesStats) WithMetric(v ...string) func(*IndicesStatsRequest) { 224 return func(r *IndicesStatsRequest) { 225 r.Metric = v 226 } 227 } 228 229 // WithCompletionFields - a list of fields for `fielddata` and `suggest` index metric (supports wildcards). 230 // 231 func (f IndicesStats) WithCompletionFields(v ...string) func(*IndicesStatsRequest) { 232 return func(r *IndicesStatsRequest) { 233 r.CompletionFields = v 234 } 235 } 236 237 // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both.. 238 // 239 func (f IndicesStats) WithExpandWildcards(v string) func(*IndicesStatsRequest) { 240 return func(r *IndicesStatsRequest) { 241 r.ExpandWildcards = v 242 } 243 } 244 245 // WithFielddataFields - a list of fields for `fielddata` index metric (supports wildcards). 246 // 247 func (f IndicesStats) WithFielddataFields(v ...string) func(*IndicesStatsRequest) { 248 return func(r *IndicesStatsRequest) { 249 r.FielddataFields = v 250 } 251 } 252 253 // WithFields - a list of fields for `fielddata` and `completion` index metric (supports wildcards). 254 // 255 func (f IndicesStats) WithFields(v ...string) func(*IndicesStatsRequest) { 256 return func(r *IndicesStatsRequest) { 257 r.Fields = v 258 } 259 } 260 261 // WithForbidClosedIndices - if set to false stats will also collected from closed indices if explicitly specified or if expand_wildcards expands to closed indices. 262 // 263 func (f IndicesStats) WithForbidClosedIndices(v bool) func(*IndicesStatsRequest) { 264 return func(r *IndicesStatsRequest) { 265 r.ForbidClosedIndices = &v 266 } 267 } 268 269 // WithGroups - a list of search groups for `search` index metric. 270 // 271 func (f IndicesStats) WithGroups(v ...string) func(*IndicesStatsRequest) { 272 return func(r *IndicesStatsRequest) { 273 r.Groups = v 274 } 275 } 276 277 // WithIncludeSegmentFileSizes - whether to report the aggregated disk usage of each one of the lucene index files (only applies if segment stats are requested). 278 // 279 func (f IndicesStats) WithIncludeSegmentFileSizes(v bool) func(*IndicesStatsRequest) { 280 return func(r *IndicesStatsRequest) { 281 r.IncludeSegmentFileSizes = &v 282 } 283 } 284 285 // WithIncludeUnloadedSegments - if set to true segment stats will include stats for segments that are not currently loaded into memory. 286 // 287 func (f IndicesStats) WithIncludeUnloadedSegments(v bool) func(*IndicesStatsRequest) { 288 return func(r *IndicesStatsRequest) { 289 r.IncludeUnloadedSegments = &v 290 } 291 } 292 293 // WithLevel - return stats aggregated at cluster, index or shard level. 294 // 295 func (f IndicesStats) WithLevel(v string) func(*IndicesStatsRequest) { 296 return func(r *IndicesStatsRequest) { 297 r.Level = v 298 } 299 } 300 301 // WithTypes - a list of document types for the `indexing` index metric. 302 // 303 func (f IndicesStats) WithTypes(v ...string) func(*IndicesStatsRequest) { 304 return func(r *IndicesStatsRequest) { 305 r.Types = v 306 } 307 } 308 309 // WithPretty makes the response body pretty-printed. 310 // 311 func (f IndicesStats) WithPretty() func(*IndicesStatsRequest) { 312 return func(r *IndicesStatsRequest) { 313 r.Pretty = true 314 } 315 } 316 317 // WithHuman makes statistical values human-readable. 318 // 319 func (f IndicesStats) WithHuman() func(*IndicesStatsRequest) { 320 return func(r *IndicesStatsRequest) { 321 r.Human = true 322 } 323 } 324 325 // WithErrorTrace includes the stack trace for errors in the response body. 326 // 327 func (f IndicesStats) WithErrorTrace() func(*IndicesStatsRequest) { 328 return func(r *IndicesStatsRequest) { 329 r.ErrorTrace = true 330 } 331 } 332 333 // WithFilterPath filters the properties of the response body. 334 // 335 func (f IndicesStats) WithFilterPath(v ...string) func(*IndicesStatsRequest) { 336 return func(r *IndicesStatsRequest) { 337 r.FilterPath = v 338 } 339 } 340 341 // WithHeader adds the headers to the HTTP request. 342 // 343 func (f IndicesStats) WithHeader(h map[string]string) func(*IndicesStatsRequest) { 344 return func(r *IndicesStatsRequest) { 345 if r.Header == nil { 346 r.Header = make(http.Header) 347 } 348 for k, v := range h { 349 r.Header.Add(k, v) 350 } 351 } 352 } 353 354 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 355 // 356 func (f IndicesStats) WithOpaqueID(s string) func(*IndicesStatsRequest) { 357 return func(r *IndicesStatsRequest) { 358 if r.Header == nil { 359 r.Header = make(http.Header) 360 } 361 r.Header.Set("X-Opaque-Id", s) 362 } 363 }