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