github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.analyze.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 newIndicesAnalyzeFunc(t Transport) IndicesAnalyze { 37 return func(o ...func(*IndicesAnalyzeRequest)) (*Response, error) { 38 var r = IndicesAnalyzeRequest{} 39 for _, f := range o { 40 f(&r) 41 } 42 return r.Do(r.ctx, t) 43 } 44 } 45 46 // ----- API Definition ------------------------------------------------------- 47 48 // IndicesAnalyze performs the analysis process on a text and return the tokens breakdown of the text. 49 // 50 // 51 type IndicesAnalyze func(o ...func(*IndicesAnalyzeRequest)) (*Response, error) 52 53 // IndicesAnalyzeRequest configures the Indices Analyze API request. 54 // 55 type IndicesAnalyzeRequest struct { 56 Index string 57 58 Body io.Reader 59 60 Pretty bool 61 Human bool 62 ErrorTrace bool 63 FilterPath []string 64 65 Header http.Header 66 67 ctx context.Context 68 } 69 70 // Do executes the request and returns response or error. 71 // 72 func (r IndicesAnalyzeRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 73 var ( 74 method string 75 path strings.Builder 76 params map[string]string 77 ) 78 79 method = "POST" 80 81 path.Grow(1 + len(r.Index) + 1 + len("_analyze")) 82 if r.Index != "" { 83 path.WriteString("/") 84 path.WriteString(r.Index) 85 } 86 path.WriteString("/") 87 path.WriteString("_analyze") 88 89 params = make(map[string]string) 90 91 if r.Index != "" { 92 params["index"] = r.Index 93 } 94 95 if r.Pretty { 96 params["pretty"] = "true" 97 } 98 99 if r.Human { 100 params["human"] = "true" 101 } 102 103 if r.ErrorTrace { 104 params["error_trace"] = "true" 105 } 106 107 if len(r.FilterPath) > 0 { 108 params["filter_path"] = strings.Join(r.FilterPath, ",") 109 } 110 111 req, err := newRequest(method, path.String(), r.Body) 112 if err != nil { 113 return nil, err 114 } 115 116 if len(params) > 0 { 117 q := req.URL.Query() 118 for k, v := range params { 119 q.Set(k, v) 120 } 121 req.URL.RawQuery = q.Encode() 122 } 123 124 if r.Body != nil { 125 req.Header[headerContentType] = headerContentTypeJSON 126 } 127 128 if len(r.Header) > 0 { 129 if len(req.Header) == 0 { 130 req.Header = r.Header 131 } else { 132 for k, vv := range r.Header { 133 for _, v := range vv { 134 req.Header.Add(k, v) 135 } 136 } 137 } 138 } 139 140 if ctx != nil { 141 req = req.WithContext(ctx) 142 } 143 144 res, err := transport.Perform(req) 145 if err != nil { 146 return nil, err 147 } 148 149 response := Response{ 150 StatusCode: res.StatusCode, 151 Body: res.Body, 152 Header: res.Header, 153 } 154 155 return &response, nil 156 } 157 158 // WithContext sets the request context. 159 // 160 func (f IndicesAnalyze) WithContext(v context.Context) func(*IndicesAnalyzeRequest) { 161 return func(r *IndicesAnalyzeRequest) { 162 r.ctx = v 163 } 164 } 165 166 // WithBody - Define analyzer/tokenizer parameters and the text on which the analysis should be performed. 167 // 168 func (f IndicesAnalyze) WithBody(v io.Reader) func(*IndicesAnalyzeRequest) { 169 return func(r *IndicesAnalyzeRequest) { 170 r.Body = v 171 } 172 } 173 174 // WithIndex - the name of the index to scope the operation. 175 // 176 func (f IndicesAnalyze) WithIndex(v string) func(*IndicesAnalyzeRequest) { 177 return func(r *IndicesAnalyzeRequest) { 178 r.Index = v 179 } 180 } 181 182 // WithPretty makes the response body pretty-printed. 183 // 184 func (f IndicesAnalyze) WithPretty() func(*IndicesAnalyzeRequest) { 185 return func(r *IndicesAnalyzeRequest) { 186 r.Pretty = true 187 } 188 } 189 190 // WithHuman makes statistical values human-readable. 191 // 192 func (f IndicesAnalyze) WithHuman() func(*IndicesAnalyzeRequest) { 193 return func(r *IndicesAnalyzeRequest) { 194 r.Human = true 195 } 196 } 197 198 // WithErrorTrace includes the stack trace for errors in the response body. 199 // 200 func (f IndicesAnalyze) WithErrorTrace() func(*IndicesAnalyzeRequest) { 201 return func(r *IndicesAnalyzeRequest) { 202 r.ErrorTrace = true 203 } 204 } 205 206 // WithFilterPath filters the properties of the response body. 207 // 208 func (f IndicesAnalyze) WithFilterPath(v ...string) func(*IndicesAnalyzeRequest) { 209 return func(r *IndicesAnalyzeRequest) { 210 r.FilterPath = v 211 } 212 } 213 214 // WithHeader adds the headers to the HTTP request. 215 // 216 func (f IndicesAnalyze) WithHeader(h map[string]string) func(*IndicesAnalyzeRequest) { 217 return func(r *IndicesAnalyzeRequest) { 218 if r.Header == nil { 219 r.Header = make(http.Header) 220 } 221 for k, v := range h { 222 r.Header.Add(k, v) 223 } 224 } 225 } 226 227 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 228 // 229 func (f IndicesAnalyze) WithOpaqueID(s string) func(*IndicesAnalyzeRequest) { 230 return func(r *IndicesAnalyzeRequest) { 231 if r.Header == nil { 232 r.Header = make(http.Header) 233 } 234 r.Header.Set("X-Opaque-Id", s) 235 } 236 }