github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.field_usage_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 newIndicesFieldUsageStatsFunc(t Transport) IndicesFieldUsageStats { 37 return func(index string, o ...func(*IndicesFieldUsageStatsRequest)) (*Response, error) { 38 var r = IndicesFieldUsageStatsRequest{Index: index} 39 for _, f := range o { 40 f(&r) 41 } 42 return r.Do(r.ctx, t) 43 } 44 } 45 46 // ----- API Definition ------------------------------------------------------- 47 48 // IndicesFieldUsageStats returns the field usage stats for each field of an index 49 // 50 // This API is experimental. 51 // 52 // 53 type IndicesFieldUsageStats func(index string, o ...func(*IndicesFieldUsageStatsRequest)) (*Response, error) 54 55 // IndicesFieldUsageStatsRequest configures the Indices Field Usage Stats API request. 56 // 57 type IndicesFieldUsageStatsRequest struct { 58 Index string 59 60 AllowNoIndices *bool 61 ExpandWildcards string 62 Fields []string 63 IgnoreUnavailable *bool 64 65 Pretty bool 66 Human bool 67 ErrorTrace bool 68 FilterPath []string 69 70 Header http.Header 71 72 ctx context.Context 73 } 74 75 // Do executes the request and returns response or error. 76 // 77 func (r IndicesFieldUsageStatsRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 78 var ( 79 method string 80 path strings.Builder 81 params map[string]string 82 ) 83 84 method = "GET" 85 86 path.Grow(1 + len(r.Index) + 1 + len("_field_usage_stats")) 87 path.WriteString("/") 88 path.WriteString(r.Index) 89 path.WriteString("/") 90 path.WriteString("_field_usage_stats") 91 92 params = make(map[string]string) 93 94 if r.AllowNoIndices != nil { 95 params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices) 96 } 97 98 if r.ExpandWildcards != "" { 99 params["expand_wildcards"] = r.ExpandWildcards 100 } 101 102 if len(r.Fields) > 0 { 103 params["fields"] = strings.Join(r.Fields, ",") 104 } 105 106 if r.IgnoreUnavailable != nil { 107 params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable) 108 } 109 110 if r.Pretty { 111 params["pretty"] = "true" 112 } 113 114 if r.Human { 115 params["human"] = "true" 116 } 117 118 if r.ErrorTrace { 119 params["error_trace"] = "true" 120 } 121 122 if len(r.FilterPath) > 0 { 123 params["filter_path"] = strings.Join(r.FilterPath, ",") 124 } 125 126 req, err := newRequest(method, path.String(), nil) 127 if err != nil { 128 return nil, err 129 } 130 131 if len(params) > 0 { 132 q := req.URL.Query() 133 for k, v := range params { 134 q.Set(k, v) 135 } 136 req.URL.RawQuery = q.Encode() 137 } 138 139 if len(r.Header) > 0 { 140 if len(req.Header) == 0 { 141 req.Header = r.Header 142 } else { 143 for k, vv := range r.Header { 144 for _, v := range vv { 145 req.Header.Add(k, v) 146 } 147 } 148 } 149 } 150 151 if ctx != nil { 152 req = req.WithContext(ctx) 153 } 154 155 res, err := transport.Perform(req) 156 if err != nil { 157 return nil, err 158 } 159 160 response := Response{ 161 StatusCode: res.StatusCode, 162 Body: res.Body, 163 Header: res.Header, 164 } 165 166 return &response, nil 167 } 168 169 // WithContext sets the request context. 170 // 171 func (f IndicesFieldUsageStats) WithContext(v context.Context) func(*IndicesFieldUsageStatsRequest) { 172 return func(r *IndicesFieldUsageStatsRequest) { 173 r.ctx = v 174 } 175 } 176 177 // WithAllowNoIndices - whether to ignore if a wildcard indices expression resolves into no concrete indices. (this includes `_all` string or when no indices have been specified). 178 // 179 func (f IndicesFieldUsageStats) WithAllowNoIndices(v bool) func(*IndicesFieldUsageStatsRequest) { 180 return func(r *IndicesFieldUsageStatsRequest) { 181 r.AllowNoIndices = &v 182 } 183 } 184 185 // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both.. 186 // 187 func (f IndicesFieldUsageStats) WithExpandWildcards(v string) func(*IndicesFieldUsageStatsRequest) { 188 return func(r *IndicesFieldUsageStatsRequest) { 189 r.ExpandWildcards = v 190 } 191 } 192 193 // WithFields - a list of fields to include in the stats if only a subset of fields should be returned (supports wildcards). 194 // 195 func (f IndicesFieldUsageStats) WithFields(v ...string) func(*IndicesFieldUsageStatsRequest) { 196 return func(r *IndicesFieldUsageStatsRequest) { 197 r.Fields = v 198 } 199 } 200 201 // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed). 202 // 203 func (f IndicesFieldUsageStats) WithIgnoreUnavailable(v bool) func(*IndicesFieldUsageStatsRequest) { 204 return func(r *IndicesFieldUsageStatsRequest) { 205 r.IgnoreUnavailable = &v 206 } 207 } 208 209 // WithPretty makes the response body pretty-printed. 210 // 211 func (f IndicesFieldUsageStats) WithPretty() func(*IndicesFieldUsageStatsRequest) { 212 return func(r *IndicesFieldUsageStatsRequest) { 213 r.Pretty = true 214 } 215 } 216 217 // WithHuman makes statistical values human-readable. 218 // 219 func (f IndicesFieldUsageStats) WithHuman() func(*IndicesFieldUsageStatsRequest) { 220 return func(r *IndicesFieldUsageStatsRequest) { 221 r.Human = true 222 } 223 } 224 225 // WithErrorTrace includes the stack trace for errors in the response body. 226 // 227 func (f IndicesFieldUsageStats) WithErrorTrace() func(*IndicesFieldUsageStatsRequest) { 228 return func(r *IndicesFieldUsageStatsRequest) { 229 r.ErrorTrace = true 230 } 231 } 232 233 // WithFilterPath filters the properties of the response body. 234 // 235 func (f IndicesFieldUsageStats) WithFilterPath(v ...string) func(*IndicesFieldUsageStatsRequest) { 236 return func(r *IndicesFieldUsageStatsRequest) { 237 r.FilterPath = v 238 } 239 } 240 241 // WithHeader adds the headers to the HTTP request. 242 // 243 func (f IndicesFieldUsageStats) WithHeader(h map[string]string) func(*IndicesFieldUsageStatsRequest) { 244 return func(r *IndicesFieldUsageStatsRequest) { 245 if r.Header == nil { 246 r.Header = make(http.Header) 247 } 248 for k, v := range h { 249 r.Header.Add(k, v) 250 } 251 } 252 } 253 254 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 255 // 256 func (f IndicesFieldUsageStats) WithOpaqueID(s string) func(*IndicesFieldUsageStatsRequest) { 257 return func(r *IndicesFieldUsageStatsRequest) { 258 if r.Header == nil { 259 r.Header = make(http.Header) 260 } 261 r.Header.Set("X-Opaque-Id", s) 262 } 263 }