github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.create.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 newCreateFunc(t Transport) Create { 39 return func(index string, id string, body io.Reader, o ...func(*CreateRequest)) (*Response, error) { 40 var r = CreateRequest{Index: index, DocumentID: id, 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 // Create creates a new document in the index. 51 // 52 // Returns a 409 response when a document with a same ID already exists in the index. 53 // 54 // 55 type Create func(index string, id string, body io.Reader, o ...func(*CreateRequest)) (*Response, error) 56 57 // CreateRequest configures the Create API request. 58 // 59 type CreateRequest struct { 60 Index string 61 DocumentID string 62 63 Body io.Reader 64 65 Pipeline string 66 Refresh string 67 Routing string 68 Timeout time.Duration 69 Version *int 70 VersionType string 71 WaitForActiveShards string 72 73 Pretty bool 74 Human bool 75 ErrorTrace bool 76 FilterPath []string 77 78 Header http.Header 79 80 ctx context.Context 81 } 82 83 // Do executes the request and returns response or error. 84 // 85 func (r CreateRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 86 var ( 87 method string 88 path strings.Builder 89 params map[string]string 90 ) 91 92 method = "PUT" 93 94 path.Grow(1 + len(r.Index) + 1 + len(r.DocumentID) + 1 + len("_create")) 95 path.WriteString("/") 96 path.WriteString(r.Index) 97 path.WriteString("/") 98 path.WriteString("_create") 99 path.WriteString("/") 100 path.WriteString(r.DocumentID) 101 102 params = make(map[string]string) 103 104 if r.Pipeline != "" { 105 params["pipeline"] = r.Pipeline 106 } 107 108 if r.Refresh != "" { 109 params["refresh"] = r.Refresh 110 } 111 112 if r.Routing != "" { 113 params["routing"] = r.Routing 114 } 115 116 if r.Timeout != 0 { 117 params["timeout"] = formatDuration(r.Timeout) 118 } 119 120 if r.Version != nil { 121 params["version"] = strconv.FormatInt(int64(*r.Version), 10) 122 } 123 124 if r.VersionType != "" { 125 params["version_type"] = r.VersionType 126 } 127 128 if r.WaitForActiveShards != "" { 129 params["wait_for_active_shards"] = r.WaitForActiveShards 130 } 131 132 if r.Pretty { 133 params["pretty"] = "true" 134 } 135 136 if r.Human { 137 params["human"] = "true" 138 } 139 140 if r.ErrorTrace { 141 params["error_trace"] = "true" 142 } 143 144 if len(r.FilterPath) > 0 { 145 params["filter_path"] = strings.Join(r.FilterPath, ",") 146 } 147 148 req, err := newRequest(method, path.String(), r.Body) 149 if err != nil { 150 return nil, err 151 } 152 153 if len(params) > 0 { 154 q := req.URL.Query() 155 for k, v := range params { 156 q.Set(k, v) 157 } 158 req.URL.RawQuery = q.Encode() 159 } 160 161 if r.Body != nil { 162 req.Header[headerContentType] = headerContentTypeJSON 163 } 164 165 if len(r.Header) > 0 { 166 if len(req.Header) == 0 { 167 req.Header = r.Header 168 } else { 169 for k, vv := range r.Header { 170 for _, v := range vv { 171 req.Header.Add(k, v) 172 } 173 } 174 } 175 } 176 177 if ctx != nil { 178 req = req.WithContext(ctx) 179 } 180 181 res, err := transport.Perform(req) 182 if err != nil { 183 return nil, err 184 } 185 186 response := Response{ 187 StatusCode: res.StatusCode, 188 Body: res.Body, 189 Header: res.Header, 190 } 191 192 return &response, nil 193 } 194 195 // WithContext sets the request context. 196 // 197 func (f Create) WithContext(v context.Context) func(*CreateRequest) { 198 return func(r *CreateRequest) { 199 r.ctx = v 200 } 201 } 202 203 // WithPipeline - the pipeline ID to preprocess incoming documents with. 204 // 205 func (f Create) WithPipeline(v string) func(*CreateRequest) { 206 return func(r *CreateRequest) { 207 r.Pipeline = v 208 } 209 } 210 211 // WithRefresh - if `true` then refresh the affected shards to make this operation visible to search, if `wait_for` then wait for a refresh to make this operation visible to search, if `false` (the default) then do nothing with refreshes.. 212 // 213 func (f Create) WithRefresh(v string) func(*CreateRequest) { 214 return func(r *CreateRequest) { 215 r.Refresh = v 216 } 217 } 218 219 // WithRouting - specific routing value. 220 // 221 func (f Create) WithRouting(v string) func(*CreateRequest) { 222 return func(r *CreateRequest) { 223 r.Routing = v 224 } 225 } 226 227 // WithTimeout - explicit operation timeout. 228 // 229 func (f Create) WithTimeout(v time.Duration) func(*CreateRequest) { 230 return func(r *CreateRequest) { 231 r.Timeout = v 232 } 233 } 234 235 // WithVersion - explicit version number for concurrency control. 236 // 237 func (f Create) WithVersion(v int) func(*CreateRequest) { 238 return func(r *CreateRequest) { 239 r.Version = &v 240 } 241 } 242 243 // WithVersionType - specific version type. 244 // 245 func (f Create) WithVersionType(v string) func(*CreateRequest) { 246 return func(r *CreateRequest) { 247 r.VersionType = v 248 } 249 } 250 251 // WithWaitForActiveShards - sets the number of shard copies that must be active before proceeding with the index operation. defaults to 1, meaning the primary shard only. set to `all` for all shard copies, otherwise set to any non-negative value less than or equal to the total number of copies for the shard (number of replicas + 1). 252 // 253 func (f Create) WithWaitForActiveShards(v string) func(*CreateRequest) { 254 return func(r *CreateRequest) { 255 r.WaitForActiveShards = v 256 } 257 } 258 259 // WithPretty makes the response body pretty-printed. 260 // 261 func (f Create) WithPretty() func(*CreateRequest) { 262 return func(r *CreateRequest) { 263 r.Pretty = true 264 } 265 } 266 267 // WithHuman makes statistical values human-readable. 268 // 269 func (f Create) WithHuman() func(*CreateRequest) { 270 return func(r *CreateRequest) { 271 r.Human = true 272 } 273 } 274 275 // WithErrorTrace includes the stack trace for errors in the response body. 276 // 277 func (f Create) WithErrorTrace() func(*CreateRequest) { 278 return func(r *CreateRequest) { 279 r.ErrorTrace = true 280 } 281 } 282 283 // WithFilterPath filters the properties of the response body. 284 // 285 func (f Create) WithFilterPath(v ...string) func(*CreateRequest) { 286 return func(r *CreateRequest) { 287 r.FilterPath = v 288 } 289 } 290 291 // WithHeader adds the headers to the HTTP request. 292 // 293 func (f Create) WithHeader(h map[string]string) func(*CreateRequest) { 294 return func(r *CreateRequest) { 295 if r.Header == nil { 296 r.Header = make(http.Header) 297 } 298 for k, v := range h { 299 r.Header.Add(k, v) 300 } 301 } 302 } 303 304 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 305 // 306 func (f Create) WithOpaqueID(s string) func(*CreateRequest) { 307 return func(r *CreateRequest) { 308 if r.Header == nil { 309 r.Header = make(http.Header) 310 } 311 r.Header.Set("X-Opaque-Id", s) 312 } 313 }