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