github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.clear_cache.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 newIndicesClearCacheFunc(t Transport) IndicesClearCache { 37 return func(o ...func(*IndicesClearCacheRequest)) (*Response, error) { 38 var r = IndicesClearCacheRequest{} 39 for _, f := range o { 40 f(&r) 41 } 42 return r.Do(r.ctx, t) 43 } 44 } 45 46 // ----- API Definition ------------------------------------------------------- 47 48 // IndicesClearCache clears all or specific caches for one or more indices. 49 // 50 // 51 type IndicesClearCache func(o ...func(*IndicesClearCacheRequest)) (*Response, error) 52 53 // IndicesClearCacheRequest configures the Indices Clear Cache API request. 54 // 55 type IndicesClearCacheRequest struct { 56 Index []string 57 58 AllowNoIndices *bool 59 ExpandWildcards string 60 Fielddata *bool 61 Fields []string 62 IgnoreUnavailable *bool 63 Query *bool 64 Request *bool 65 66 Pretty bool 67 Human bool 68 ErrorTrace bool 69 FilterPath []string 70 71 Header http.Header 72 73 ctx context.Context 74 } 75 76 // Do executes the request and returns response or error. 77 // 78 func (r IndicesClearCacheRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 79 var ( 80 method string 81 path strings.Builder 82 params map[string]string 83 ) 84 85 method = "POST" 86 87 path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len("_cache") + 1 + len("clear")) 88 if len(r.Index) > 0 { 89 path.WriteString("/") 90 path.WriteString(strings.Join(r.Index, ",")) 91 } 92 path.WriteString("/") 93 path.WriteString("_cache") 94 path.WriteString("/") 95 path.WriteString("clear") 96 97 params = make(map[string]string) 98 99 if r.AllowNoIndices != nil { 100 params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices) 101 } 102 103 if r.ExpandWildcards != "" { 104 params["expand_wildcards"] = r.ExpandWildcards 105 } 106 107 if r.Fielddata != nil { 108 params["fielddata"] = strconv.FormatBool(*r.Fielddata) 109 } 110 111 if len(r.Fields) > 0 { 112 params["fields"] = strings.Join(r.Fields, ",") 113 } 114 115 if r.IgnoreUnavailable != nil { 116 params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable) 117 } 118 119 if len(r.Index) > 0 { 120 params["index"] = strings.Join(r.Index, ",") 121 } 122 123 if r.Query != nil { 124 params["query"] = strconv.FormatBool(*r.Query) 125 } 126 127 if r.Request != nil { 128 params["request"] = strconv.FormatBool(*r.Request) 129 } 130 131 if r.Pretty { 132 params["pretty"] = "true" 133 } 134 135 if r.Human { 136 params["human"] = "true" 137 } 138 139 if r.ErrorTrace { 140 params["error_trace"] = "true" 141 } 142 143 if len(r.FilterPath) > 0 { 144 params["filter_path"] = strings.Join(r.FilterPath, ",") 145 } 146 147 req, err := newRequest(method, path.String(), nil) 148 if err != nil { 149 return nil, err 150 } 151 152 if len(params) > 0 { 153 q := req.URL.Query() 154 for k, v := range params { 155 q.Set(k, v) 156 } 157 req.URL.RawQuery = q.Encode() 158 } 159 160 if len(r.Header) > 0 { 161 if len(req.Header) == 0 { 162 req.Header = r.Header 163 } else { 164 for k, vv := range r.Header { 165 for _, v := range vv { 166 req.Header.Add(k, v) 167 } 168 } 169 } 170 } 171 172 if ctx != nil { 173 req = req.WithContext(ctx) 174 } 175 176 res, err := transport.Perform(req) 177 if err != nil { 178 return nil, err 179 } 180 181 response := Response{ 182 StatusCode: res.StatusCode, 183 Body: res.Body, 184 Header: res.Header, 185 } 186 187 return &response, nil 188 } 189 190 // WithContext sets the request context. 191 // 192 func (f IndicesClearCache) WithContext(v context.Context) func(*IndicesClearCacheRequest) { 193 return func(r *IndicesClearCacheRequest) { 194 r.ctx = v 195 } 196 } 197 198 // WithIndex - a list of index name to limit the operation. 199 // 200 func (f IndicesClearCache) WithIndex(v ...string) func(*IndicesClearCacheRequest) { 201 return func(r *IndicesClearCacheRequest) { 202 r.Index = v 203 } 204 } 205 206 // 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). 207 // 208 func (f IndicesClearCache) WithAllowNoIndices(v bool) func(*IndicesClearCacheRequest) { 209 return func(r *IndicesClearCacheRequest) { 210 r.AllowNoIndices = &v 211 } 212 } 213 214 // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both.. 215 // 216 func (f IndicesClearCache) WithExpandWildcards(v string) func(*IndicesClearCacheRequest) { 217 return func(r *IndicesClearCacheRequest) { 218 r.ExpandWildcards = v 219 } 220 } 221 222 // WithFielddata - clear field data. 223 // 224 func (f IndicesClearCache) WithFielddata(v bool) func(*IndicesClearCacheRequest) { 225 return func(r *IndicesClearCacheRequest) { 226 r.Fielddata = &v 227 } 228 } 229 230 // WithFields - a list of fields to clear when using the `fielddata` parameter (default: all). 231 // 232 func (f IndicesClearCache) WithFields(v ...string) func(*IndicesClearCacheRequest) { 233 return func(r *IndicesClearCacheRequest) { 234 r.Fields = v 235 } 236 } 237 238 // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed). 239 // 240 func (f IndicesClearCache) WithIgnoreUnavailable(v bool) func(*IndicesClearCacheRequest) { 241 return func(r *IndicesClearCacheRequest) { 242 r.IgnoreUnavailable = &v 243 } 244 } 245 246 // WithQuery - clear query caches. 247 // 248 func (f IndicesClearCache) WithQuery(v bool) func(*IndicesClearCacheRequest) { 249 return func(r *IndicesClearCacheRequest) { 250 r.Query = &v 251 } 252 } 253 254 // WithRequest - clear request cache. 255 // 256 func (f IndicesClearCache) WithRequest(v bool) func(*IndicesClearCacheRequest) { 257 return func(r *IndicesClearCacheRequest) { 258 r.Request = &v 259 } 260 } 261 262 // WithPretty makes the response body pretty-printed. 263 // 264 func (f IndicesClearCache) WithPretty() func(*IndicesClearCacheRequest) { 265 return func(r *IndicesClearCacheRequest) { 266 r.Pretty = true 267 } 268 } 269 270 // WithHuman makes statistical values human-readable. 271 // 272 func (f IndicesClearCache) WithHuman() func(*IndicesClearCacheRequest) { 273 return func(r *IndicesClearCacheRequest) { 274 r.Human = true 275 } 276 } 277 278 // WithErrorTrace includes the stack trace for errors in the response body. 279 // 280 func (f IndicesClearCache) WithErrorTrace() func(*IndicesClearCacheRequest) { 281 return func(r *IndicesClearCacheRequest) { 282 r.ErrorTrace = true 283 } 284 } 285 286 // WithFilterPath filters the properties of the response body. 287 // 288 func (f IndicesClearCache) WithFilterPath(v ...string) func(*IndicesClearCacheRequest) { 289 return func(r *IndicesClearCacheRequest) { 290 r.FilterPath = v 291 } 292 } 293 294 // WithHeader adds the headers to the HTTP request. 295 // 296 func (f IndicesClearCache) WithHeader(h map[string]string) func(*IndicesClearCacheRequest) { 297 return func(r *IndicesClearCacheRequest) { 298 if r.Header == nil { 299 r.Header = make(http.Header) 300 } 301 for k, v := range h { 302 r.Header.Add(k, v) 303 } 304 } 305 } 306 307 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 308 // 309 func (f IndicesClearCache) WithOpaqueID(s string) func(*IndicesClearCacheRequest) { 310 return func(r *IndicesClearCacheRequest) { 311 if r.Header == nil { 312 r.Header = make(http.Header) 313 } 314 r.Header.Set("X-Opaque-Id", s) 315 } 316 }