github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.terms_enum.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 "strings" 34 ) 35 36 func newTermsEnumFunc(t Transport) TermsEnum { 37 return func(index []string, o ...func(*TermsEnumRequest)) (*Response, error) { 38 var r = TermsEnumRequest{Index: index} 39 for _, f := range o { 40 f(&r) 41 } 42 return r.Do(r.ctx, t) 43 } 44 } 45 46 // ----- API Definition ------------------------------------------------------- 47 48 // TermsEnum the terms enum API can be used to discover terms in the index that begin with the provided string. It is designed for low-latency look-ups used in auto-complete scenarios. 49 // 50 // This API is beta. 51 // 52 // 53 type TermsEnum func(index []string, o ...func(*TermsEnumRequest)) (*Response, error) 54 55 // TermsEnumRequest configures the Terms Enum API request. 56 // 57 type TermsEnumRequest struct { 58 Index []string 59 60 Body io.Reader 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 TermsEnumRequest) 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("_terms_enum")) 84 path.WriteString("/") 85 path.WriteString(strings.Join(r.Index, ",")) 86 path.WriteString("/") 87 path.WriteString("_terms_enum") 88 89 params = make(map[string]string) 90 91 if r.Pretty { 92 params["pretty"] = "true" 93 } 94 95 if r.Human { 96 params["human"] = "true" 97 } 98 99 if r.ErrorTrace { 100 params["error_trace"] = "true" 101 } 102 103 if len(r.FilterPath) > 0 { 104 params["filter_path"] = strings.Join(r.FilterPath, ",") 105 } 106 107 req, err := newRequest(method, path.String(), r.Body) 108 if err != nil { 109 return nil, err 110 } 111 112 if len(params) > 0 { 113 q := req.URL.Query() 114 for k, v := range params { 115 q.Set(k, v) 116 } 117 req.URL.RawQuery = q.Encode() 118 } 119 120 if r.Body != nil { 121 req.Header[headerContentType] = headerContentTypeJSON 122 } 123 124 if len(r.Header) > 0 { 125 if len(req.Header) == 0 { 126 req.Header = r.Header 127 } else { 128 for k, vv := range r.Header { 129 for _, v := range vv { 130 req.Header.Add(k, v) 131 } 132 } 133 } 134 } 135 136 if ctx != nil { 137 req = req.WithContext(ctx) 138 } 139 140 res, err := transport.Perform(req) 141 if err != nil { 142 return nil, err 143 } 144 145 response := Response{ 146 StatusCode: res.StatusCode, 147 Body: res.Body, 148 Header: res.Header, 149 } 150 151 return &response, nil 152 } 153 154 // WithContext sets the request context. 155 // 156 func (f TermsEnum) WithContext(v context.Context) func(*TermsEnumRequest) { 157 return func(r *TermsEnumRequest) { 158 r.ctx = v 159 } 160 } 161 162 // WithBody - field name, string which is the prefix expected in matching terms, timeout and size for max number of results. 163 // 164 func (f TermsEnum) WithBody(v io.Reader) func(*TermsEnumRequest) { 165 return func(r *TermsEnumRequest) { 166 r.Body = v 167 } 168 } 169 170 // WithPretty makes the response body pretty-printed. 171 // 172 func (f TermsEnum) WithPretty() func(*TermsEnumRequest) { 173 return func(r *TermsEnumRequest) { 174 r.Pretty = true 175 } 176 } 177 178 // WithHuman makes statistical values human-readable. 179 // 180 func (f TermsEnum) WithHuman() func(*TermsEnumRequest) { 181 return func(r *TermsEnumRequest) { 182 r.Human = true 183 } 184 } 185 186 // WithErrorTrace includes the stack trace for errors in the response body. 187 // 188 func (f TermsEnum) WithErrorTrace() func(*TermsEnumRequest) { 189 return func(r *TermsEnumRequest) { 190 r.ErrorTrace = true 191 } 192 } 193 194 // WithFilterPath filters the properties of the response body. 195 // 196 func (f TermsEnum) WithFilterPath(v ...string) func(*TermsEnumRequest) { 197 return func(r *TermsEnumRequest) { 198 r.FilterPath = v 199 } 200 } 201 202 // WithHeader adds the headers to the HTTP request. 203 // 204 func (f TermsEnum) WithHeader(h map[string]string) func(*TermsEnumRequest) { 205 return func(r *TermsEnumRequest) { 206 if r.Header == nil { 207 r.Header = make(http.Header) 208 } 209 for k, v := range h { 210 r.Header.Add(k, v) 211 } 212 } 213 } 214 215 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 216 // 217 func (f TermsEnum) WithOpaqueID(s string) func(*TermsEnumRequest) { 218 return func(r *TermsEnumRequest) { 219 if r.Header == nil { 220 r.Header = make(http.Header) 221 } 222 r.Header.Set("X-Opaque-Id", s) 223 } 224 }