github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.mtermvectors.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 ) 36 37 func newMtermvectorsFunc(t Transport) Mtermvectors { 38 return func(o ...func(*MtermvectorsRequest)) (*Response, error) { 39 var r = MtermvectorsRequest{} 40 for _, f := range o { 41 f(&r) 42 } 43 return r.Do(r.ctx, t) 44 } 45 } 46 47 // ----- API Definition ------------------------------------------------------- 48 49 // Mtermvectors returns multiple termvectors in one request. 50 // 51 // 52 type Mtermvectors func(o ...func(*MtermvectorsRequest)) (*Response, error) 53 54 // MtermvectorsRequest configures the Mtermvectors API request. 55 // 56 type MtermvectorsRequest struct { 57 Index string 58 59 Body io.Reader 60 61 Fields []string 62 FieldStatistics *bool 63 Ids []string 64 Offsets *bool 65 Payloads *bool 66 Positions *bool 67 Preference string 68 Realtime *bool 69 Routing string 70 TermStatistics *bool 71 Version *int 72 VersionType string 73 74 Pretty bool 75 Human bool 76 ErrorTrace bool 77 FilterPath []string 78 79 Header http.Header 80 81 ctx context.Context 82 } 83 84 // Do executes the request and returns response or error. 85 // 86 func (r MtermvectorsRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 87 var ( 88 method string 89 path strings.Builder 90 params map[string]string 91 ) 92 93 method = "POST" 94 95 path.Grow(1 + len(r.Index) + 1 + len("_mtermvectors")) 96 if r.Index != "" { 97 path.WriteString("/") 98 path.WriteString(r.Index) 99 } 100 path.WriteString("/") 101 path.WriteString("_mtermvectors") 102 103 params = make(map[string]string) 104 105 if len(r.Fields) > 0 { 106 params["fields"] = strings.Join(r.Fields, ",") 107 } 108 109 if r.FieldStatistics != nil { 110 params["field_statistics"] = strconv.FormatBool(*r.FieldStatistics) 111 } 112 113 if len(r.Ids) > 0 { 114 params["ids"] = strings.Join(r.Ids, ",") 115 } 116 117 if r.Offsets != nil { 118 params["offsets"] = strconv.FormatBool(*r.Offsets) 119 } 120 121 if r.Payloads != nil { 122 params["payloads"] = strconv.FormatBool(*r.Payloads) 123 } 124 125 if r.Positions != nil { 126 params["positions"] = strconv.FormatBool(*r.Positions) 127 } 128 129 if r.Preference != "" { 130 params["preference"] = r.Preference 131 } 132 133 if r.Realtime != nil { 134 params["realtime"] = strconv.FormatBool(*r.Realtime) 135 } 136 137 if r.Routing != "" { 138 params["routing"] = r.Routing 139 } 140 141 if r.TermStatistics != nil { 142 params["term_statistics"] = strconv.FormatBool(*r.TermStatistics) 143 } 144 145 if r.Version != nil { 146 params["version"] = strconv.FormatInt(int64(*r.Version), 10) 147 } 148 149 if r.VersionType != "" { 150 params["version_type"] = r.VersionType 151 } 152 153 if r.Pretty { 154 params["pretty"] = "true" 155 } 156 157 if r.Human { 158 params["human"] = "true" 159 } 160 161 if r.ErrorTrace { 162 params["error_trace"] = "true" 163 } 164 165 if len(r.FilterPath) > 0 { 166 params["filter_path"] = strings.Join(r.FilterPath, ",") 167 } 168 169 req, err := newRequest(method, path.String(), r.Body) 170 if err != nil { 171 return nil, err 172 } 173 174 if len(params) > 0 { 175 q := req.URL.Query() 176 for k, v := range params { 177 q.Set(k, v) 178 } 179 req.URL.RawQuery = q.Encode() 180 } 181 182 if r.Body != nil { 183 req.Header[headerContentType] = headerContentTypeJSON 184 } 185 186 if len(r.Header) > 0 { 187 if len(req.Header) == 0 { 188 req.Header = r.Header 189 } else { 190 for k, vv := range r.Header { 191 for _, v := range vv { 192 req.Header.Add(k, v) 193 } 194 } 195 } 196 } 197 198 if ctx != nil { 199 req = req.WithContext(ctx) 200 } 201 202 res, err := transport.Perform(req) 203 if err != nil { 204 return nil, err 205 } 206 207 response := Response{ 208 StatusCode: res.StatusCode, 209 Body: res.Body, 210 Header: res.Header, 211 } 212 213 return &response, nil 214 } 215 216 // WithContext sets the request context. 217 // 218 func (f Mtermvectors) WithContext(v context.Context) func(*MtermvectorsRequest) { 219 return func(r *MtermvectorsRequest) { 220 r.ctx = v 221 } 222 } 223 224 // WithBody - Define ids, documents, parameters or a list of parameters per document here. You must at least provide a list of document ids. See documentation.. 225 // 226 func (f Mtermvectors) WithBody(v io.Reader) func(*MtermvectorsRequest) { 227 return func(r *MtermvectorsRequest) { 228 r.Body = v 229 } 230 } 231 232 // WithIndex - the index in which the document resides.. 233 // 234 func (f Mtermvectors) WithIndex(v string) func(*MtermvectorsRequest) { 235 return func(r *MtermvectorsRequest) { 236 r.Index = v 237 } 238 } 239 240 // WithFields - a list of fields to return. applies to all returned documents unless otherwise specified in body "params" or "docs".. 241 // 242 func (f Mtermvectors) WithFields(v ...string) func(*MtermvectorsRequest) { 243 return func(r *MtermvectorsRequest) { 244 r.Fields = v 245 } 246 } 247 248 // WithFieldStatistics - specifies if document count, sum of document frequencies and sum of total term frequencies should be returned. applies to all returned documents unless otherwise specified in body "params" or "docs".. 249 // 250 func (f Mtermvectors) WithFieldStatistics(v bool) func(*MtermvectorsRequest) { 251 return func(r *MtermvectorsRequest) { 252 r.FieldStatistics = &v 253 } 254 } 255 256 // WithIds - a list of documents ids. you must define ids as parameter or set "ids" or "docs" in the request body. 257 // 258 func (f Mtermvectors) WithIds(v ...string) func(*MtermvectorsRequest) { 259 return func(r *MtermvectorsRequest) { 260 r.Ids = v 261 } 262 } 263 264 // WithOffsets - specifies if term offsets should be returned. applies to all returned documents unless otherwise specified in body "params" or "docs".. 265 // 266 func (f Mtermvectors) WithOffsets(v bool) func(*MtermvectorsRequest) { 267 return func(r *MtermvectorsRequest) { 268 r.Offsets = &v 269 } 270 } 271 272 // WithPayloads - specifies if term payloads should be returned. applies to all returned documents unless otherwise specified in body "params" or "docs".. 273 // 274 func (f Mtermvectors) WithPayloads(v bool) func(*MtermvectorsRequest) { 275 return func(r *MtermvectorsRequest) { 276 r.Payloads = &v 277 } 278 } 279 280 // WithPositions - specifies if term positions should be returned. applies to all returned documents unless otherwise specified in body "params" or "docs".. 281 // 282 func (f Mtermvectors) WithPositions(v bool) func(*MtermvectorsRequest) { 283 return func(r *MtermvectorsRequest) { 284 r.Positions = &v 285 } 286 } 287 288 // WithPreference - specify the node or shard the operation should be performed on (default: random) .applies to all returned documents unless otherwise specified in body "params" or "docs".. 289 // 290 func (f Mtermvectors) WithPreference(v string) func(*MtermvectorsRequest) { 291 return func(r *MtermvectorsRequest) { 292 r.Preference = v 293 } 294 } 295 296 // WithRealtime - specifies if requests are real-time as opposed to near-real-time (default: true).. 297 // 298 func (f Mtermvectors) WithRealtime(v bool) func(*MtermvectorsRequest) { 299 return func(r *MtermvectorsRequest) { 300 r.Realtime = &v 301 } 302 } 303 304 // WithRouting - specific routing value. applies to all returned documents unless otherwise specified in body "params" or "docs".. 305 // 306 func (f Mtermvectors) WithRouting(v string) func(*MtermvectorsRequest) { 307 return func(r *MtermvectorsRequest) { 308 r.Routing = v 309 } 310 } 311 312 // WithTermStatistics - specifies if total term frequency and document frequency should be returned. applies to all returned documents unless otherwise specified in body "params" or "docs".. 313 // 314 func (f Mtermvectors) WithTermStatistics(v bool) func(*MtermvectorsRequest) { 315 return func(r *MtermvectorsRequest) { 316 r.TermStatistics = &v 317 } 318 } 319 320 // WithVersion - explicit version number for concurrency control. 321 // 322 func (f Mtermvectors) WithVersion(v int) func(*MtermvectorsRequest) { 323 return func(r *MtermvectorsRequest) { 324 r.Version = &v 325 } 326 } 327 328 // WithVersionType - specific version type. 329 // 330 func (f Mtermvectors) WithVersionType(v string) func(*MtermvectorsRequest) { 331 return func(r *MtermvectorsRequest) { 332 r.VersionType = v 333 } 334 } 335 336 // WithPretty makes the response body pretty-printed. 337 // 338 func (f Mtermvectors) WithPretty() func(*MtermvectorsRequest) { 339 return func(r *MtermvectorsRequest) { 340 r.Pretty = true 341 } 342 } 343 344 // WithHuman makes statistical values human-readable. 345 // 346 func (f Mtermvectors) WithHuman() func(*MtermvectorsRequest) { 347 return func(r *MtermvectorsRequest) { 348 r.Human = true 349 } 350 } 351 352 // WithErrorTrace includes the stack trace for errors in the response body. 353 // 354 func (f Mtermvectors) WithErrorTrace() func(*MtermvectorsRequest) { 355 return func(r *MtermvectorsRequest) { 356 r.ErrorTrace = true 357 } 358 } 359 360 // WithFilterPath filters the properties of the response body. 361 // 362 func (f Mtermvectors) WithFilterPath(v ...string) func(*MtermvectorsRequest) { 363 return func(r *MtermvectorsRequest) { 364 r.FilterPath = v 365 } 366 } 367 368 // WithHeader adds the headers to the HTTP request. 369 // 370 func (f Mtermvectors) WithHeader(h map[string]string) func(*MtermvectorsRequest) { 371 return func(r *MtermvectorsRequest) { 372 if r.Header == nil { 373 r.Header = make(http.Header) 374 } 375 for k, v := range h { 376 r.Header.Add(k, v) 377 } 378 } 379 } 380 381 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 382 // 383 func (f Mtermvectors) WithOpaqueID(s string) func(*MtermvectorsRequest) { 384 return func(r *MtermvectorsRequest) { 385 if r.Header == nil { 386 r.Header = make(http.Header) 387 } 388 r.Header.Set("X-Opaque-Id", s) 389 } 390 }