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