github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.flush.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 newIndicesFlushFunc(t Transport) IndicesFlush { 37 return func(o ...func(*IndicesFlushRequest)) (*Response, error) { 38 var r = IndicesFlushRequest{} 39 for _, f := range o { 40 f(&r) 41 } 42 return r.Do(r.ctx, t) 43 } 44 } 45 46 // ----- API Definition ------------------------------------------------------- 47 48 // IndicesFlush performs the flush operation on one or more indices. 49 // 50 // 51 type IndicesFlush func(o ...func(*IndicesFlushRequest)) (*Response, error) 52 53 // IndicesFlushRequest configures the Indices Flush API request. 54 // 55 type IndicesFlushRequest struct { 56 Index []string 57 58 AllowNoIndices *bool 59 ExpandWildcards string 60 Force *bool 61 IgnoreUnavailable *bool 62 WaitIfOngoing *bool 63 64 Pretty bool 65 Human bool 66 ErrorTrace bool 67 FilterPath []string 68 69 Header http.Header 70 71 ctx context.Context 72 } 73 74 // Do executes the request and returns response or error. 75 // 76 func (r IndicesFlushRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 77 var ( 78 method string 79 path strings.Builder 80 params map[string]string 81 ) 82 83 method = "POST" 84 85 path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len("_flush")) 86 if len(r.Index) > 0 { 87 path.WriteString("/") 88 path.WriteString(strings.Join(r.Index, ",")) 89 } 90 path.WriteString("/") 91 path.WriteString("_flush") 92 93 params = make(map[string]string) 94 95 if r.AllowNoIndices != nil { 96 params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices) 97 } 98 99 if r.ExpandWildcards != "" { 100 params["expand_wildcards"] = r.ExpandWildcards 101 } 102 103 if r.Force != nil { 104 params["force"] = strconv.FormatBool(*r.Force) 105 } 106 107 if r.IgnoreUnavailable != nil { 108 params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable) 109 } 110 111 if r.WaitIfOngoing != nil { 112 params["wait_if_ongoing"] = strconv.FormatBool(*r.WaitIfOngoing) 113 } 114 115 if r.Pretty { 116 params["pretty"] = "true" 117 } 118 119 if r.Human { 120 params["human"] = "true" 121 } 122 123 if r.ErrorTrace { 124 params["error_trace"] = "true" 125 } 126 127 if len(r.FilterPath) > 0 { 128 params["filter_path"] = strings.Join(r.FilterPath, ",") 129 } 130 131 req, err := newRequest(method, path.String(), nil) 132 if err != nil { 133 return nil, err 134 } 135 136 if len(params) > 0 { 137 q := req.URL.Query() 138 for k, v := range params { 139 q.Set(k, v) 140 } 141 req.URL.RawQuery = q.Encode() 142 } 143 144 if len(r.Header) > 0 { 145 if len(req.Header) == 0 { 146 req.Header = r.Header 147 } else { 148 for k, vv := range r.Header { 149 for _, v := range vv { 150 req.Header.Add(k, v) 151 } 152 } 153 } 154 } 155 156 if ctx != nil { 157 req = req.WithContext(ctx) 158 } 159 160 res, err := transport.Perform(req) 161 if err != nil { 162 return nil, err 163 } 164 165 response := Response{ 166 StatusCode: res.StatusCode, 167 Body: res.Body, 168 Header: res.Header, 169 } 170 171 return &response, nil 172 } 173 174 // WithContext sets the request context. 175 // 176 func (f IndicesFlush) WithContext(v context.Context) func(*IndicesFlushRequest) { 177 return func(r *IndicesFlushRequest) { 178 r.ctx = v 179 } 180 } 181 182 // WithIndex - a list of index names; use _all for all indices. 183 // 184 func (f IndicesFlush) WithIndex(v ...string) func(*IndicesFlushRequest) { 185 return func(r *IndicesFlushRequest) { 186 r.Index = v 187 } 188 } 189 190 // 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). 191 // 192 func (f IndicesFlush) WithAllowNoIndices(v bool) func(*IndicesFlushRequest) { 193 return func(r *IndicesFlushRequest) { 194 r.AllowNoIndices = &v 195 } 196 } 197 198 // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both.. 199 // 200 func (f IndicesFlush) WithExpandWildcards(v string) func(*IndicesFlushRequest) { 201 return func(r *IndicesFlushRequest) { 202 r.ExpandWildcards = v 203 } 204 } 205 206 // WithForce - whether a flush should be forced even if it is not necessarily needed ie. if no changes will be committed to the index. this is useful if transaction log ids should be incremented even if no uncommitted changes are present. (this setting can be considered as internal). 207 // 208 func (f IndicesFlush) WithForce(v bool) func(*IndicesFlushRequest) { 209 return func(r *IndicesFlushRequest) { 210 r.Force = &v 211 } 212 } 213 214 // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed). 215 // 216 func (f IndicesFlush) WithIgnoreUnavailable(v bool) func(*IndicesFlushRequest) { 217 return func(r *IndicesFlushRequest) { 218 r.IgnoreUnavailable = &v 219 } 220 } 221 222 // WithWaitIfOngoing - if set to true the flush operation will block until the flush can be executed if another flush operation is already executing. the default is true. if set to false the flush will be skipped iff if another flush operation is already running.. 223 // 224 func (f IndicesFlush) WithWaitIfOngoing(v bool) func(*IndicesFlushRequest) { 225 return func(r *IndicesFlushRequest) { 226 r.WaitIfOngoing = &v 227 } 228 } 229 230 // WithPretty makes the response body pretty-printed. 231 // 232 func (f IndicesFlush) WithPretty() func(*IndicesFlushRequest) { 233 return func(r *IndicesFlushRequest) { 234 r.Pretty = true 235 } 236 } 237 238 // WithHuman makes statistical values human-readable. 239 // 240 func (f IndicesFlush) WithHuman() func(*IndicesFlushRequest) { 241 return func(r *IndicesFlushRequest) { 242 r.Human = true 243 } 244 } 245 246 // WithErrorTrace includes the stack trace for errors in the response body. 247 // 248 func (f IndicesFlush) WithErrorTrace() func(*IndicesFlushRequest) { 249 return func(r *IndicesFlushRequest) { 250 r.ErrorTrace = true 251 } 252 } 253 254 // WithFilterPath filters the properties of the response body. 255 // 256 func (f IndicesFlush) WithFilterPath(v ...string) func(*IndicesFlushRequest) { 257 return func(r *IndicesFlushRequest) { 258 r.FilterPath = v 259 } 260 } 261 262 // WithHeader adds the headers to the HTTP request. 263 // 264 func (f IndicesFlush) WithHeader(h map[string]string) func(*IndicesFlushRequest) { 265 return func(r *IndicesFlushRequest) { 266 if r.Header == nil { 267 r.Header = make(http.Header) 268 } 269 for k, v := range h { 270 r.Header.Add(k, v) 271 } 272 } 273 } 274 275 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 276 // 277 func (f IndicesFlush) WithOpaqueID(s string) func(*IndicesFlushRequest) { 278 return func(r *IndicesFlushRequest) { 279 if r.Header == nil { 280 r.Header = make(http.Header) 281 } 282 r.Header.Set("X-Opaque-Id", s) 283 } 284 }