github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.validate_query.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 newIndicesValidateQueryFunc(t Transport) IndicesValidateQuery { 38 return func(o ...func(*IndicesValidateQueryRequest)) (*Response, error) { 39 var r = IndicesValidateQueryRequest{} 40 for _, f := range o { 41 f(&r) 42 } 43 return r.Do(r.ctx, t) 44 } 45 } 46 47 // ----- API Definition ------------------------------------------------------- 48 49 // IndicesValidateQuery allows a user to validate a potentially expensive query without executing it. 50 // 51 // 52 type IndicesValidateQuery func(o ...func(*IndicesValidateQueryRequest)) (*Response, error) 53 54 // IndicesValidateQueryRequest configures the Indices Validate Query API request. 55 // 56 type IndicesValidateQueryRequest struct { 57 Index []string 58 59 Body io.Reader 60 61 AllowNoIndices *bool 62 AllShards *bool 63 Analyzer string 64 AnalyzeWildcard *bool 65 DefaultOperator string 66 Df string 67 ExpandWildcards string 68 Explain *bool 69 IgnoreUnavailable *bool 70 Lenient *bool 71 Query string 72 Rewrite *bool 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 IndicesValidateQueryRequest) 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(strings.Join(r.Index, ",")) + 1 + len("_validate") + 1 + len("query")) 96 if len(r.Index) > 0 { 97 path.WriteString("/") 98 path.WriteString(strings.Join(r.Index, ",")) 99 } 100 path.WriteString("/") 101 path.WriteString("_validate") 102 path.WriteString("/") 103 path.WriteString("query") 104 105 params = make(map[string]string) 106 107 if r.AllowNoIndices != nil { 108 params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices) 109 } 110 111 if r.AllShards != nil { 112 params["all_shards"] = strconv.FormatBool(*r.AllShards) 113 } 114 115 if r.Analyzer != "" { 116 params["analyzer"] = r.Analyzer 117 } 118 119 if r.AnalyzeWildcard != nil { 120 params["analyze_wildcard"] = strconv.FormatBool(*r.AnalyzeWildcard) 121 } 122 123 if r.DefaultOperator != "" { 124 params["default_operator"] = r.DefaultOperator 125 } 126 127 if r.Df != "" { 128 params["df"] = r.Df 129 } 130 131 if r.ExpandWildcards != "" { 132 params["expand_wildcards"] = r.ExpandWildcards 133 } 134 135 if r.Explain != nil { 136 params["explain"] = strconv.FormatBool(*r.Explain) 137 } 138 139 if r.IgnoreUnavailable != nil { 140 params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable) 141 } 142 143 if r.Lenient != nil { 144 params["lenient"] = strconv.FormatBool(*r.Lenient) 145 } 146 147 if r.Query != "" { 148 params["q"] = r.Query 149 } 150 151 if r.Rewrite != nil { 152 params["rewrite"] = strconv.FormatBool(*r.Rewrite) 153 } 154 155 if r.Pretty { 156 params["pretty"] = "true" 157 } 158 159 if r.Human { 160 params["human"] = "true" 161 } 162 163 if r.ErrorTrace { 164 params["error_trace"] = "true" 165 } 166 167 if len(r.FilterPath) > 0 { 168 params["filter_path"] = strings.Join(r.FilterPath, ",") 169 } 170 171 req, err := newRequest(method, path.String(), r.Body) 172 if err != nil { 173 return nil, err 174 } 175 176 if len(params) > 0 { 177 q := req.URL.Query() 178 for k, v := range params { 179 q.Set(k, v) 180 } 181 req.URL.RawQuery = q.Encode() 182 } 183 184 if r.Body != nil { 185 req.Header[headerContentType] = headerContentTypeJSON 186 } 187 188 if len(r.Header) > 0 { 189 if len(req.Header) == 0 { 190 req.Header = r.Header 191 } else { 192 for k, vv := range r.Header { 193 for _, v := range vv { 194 req.Header.Add(k, v) 195 } 196 } 197 } 198 } 199 200 if ctx != nil { 201 req = req.WithContext(ctx) 202 } 203 204 res, err := transport.Perform(req) 205 if err != nil { 206 return nil, err 207 } 208 209 response := Response{ 210 StatusCode: res.StatusCode, 211 Body: res.Body, 212 Header: res.Header, 213 } 214 215 return &response, nil 216 } 217 218 // WithContext sets the request context. 219 // 220 func (f IndicesValidateQuery) WithContext(v context.Context) func(*IndicesValidateQueryRequest) { 221 return func(r *IndicesValidateQueryRequest) { 222 r.ctx = v 223 } 224 } 225 226 // WithBody - The query definition specified with the Query DSL. 227 // 228 func (f IndicesValidateQuery) WithBody(v io.Reader) func(*IndicesValidateQueryRequest) { 229 return func(r *IndicesValidateQueryRequest) { 230 r.Body = v 231 } 232 } 233 234 // WithIndex - a list of index names to restrict the operation; use _all to perform the operation on all indices. 235 // 236 func (f IndicesValidateQuery) WithIndex(v ...string) func(*IndicesValidateQueryRequest) { 237 return func(r *IndicesValidateQueryRequest) { 238 r.Index = v 239 } 240 } 241 242 // WithAllowNoIndices - whether to ignore if a wildcard indices expression resolves into no concrete indices. (this includes `_all` string or when no indices have been specified). 243 // 244 func (f IndicesValidateQuery) WithAllowNoIndices(v bool) func(*IndicesValidateQueryRequest) { 245 return func(r *IndicesValidateQueryRequest) { 246 r.AllowNoIndices = &v 247 } 248 } 249 250 // WithAllShards - execute validation on all shards instead of one random shard per index. 251 // 252 func (f IndicesValidateQuery) WithAllShards(v bool) func(*IndicesValidateQueryRequest) { 253 return func(r *IndicesValidateQueryRequest) { 254 r.AllShards = &v 255 } 256 } 257 258 // WithAnalyzer - the analyzer to use for the query string. 259 // 260 func (f IndicesValidateQuery) WithAnalyzer(v string) func(*IndicesValidateQueryRequest) { 261 return func(r *IndicesValidateQueryRequest) { 262 r.Analyzer = v 263 } 264 } 265 266 // WithAnalyzeWildcard - specify whether wildcard and prefix queries should be analyzed (default: false). 267 // 268 func (f IndicesValidateQuery) WithAnalyzeWildcard(v bool) func(*IndicesValidateQueryRequest) { 269 return func(r *IndicesValidateQueryRequest) { 270 r.AnalyzeWildcard = &v 271 } 272 } 273 274 // WithDefaultOperator - the default operator for query string query (and or or). 275 // 276 func (f IndicesValidateQuery) WithDefaultOperator(v string) func(*IndicesValidateQueryRequest) { 277 return func(r *IndicesValidateQueryRequest) { 278 r.DefaultOperator = v 279 } 280 } 281 282 // WithDf - the field to use as default where no field prefix is given in the query string. 283 // 284 func (f IndicesValidateQuery) WithDf(v string) func(*IndicesValidateQueryRequest) { 285 return func(r *IndicesValidateQueryRequest) { 286 r.Df = v 287 } 288 } 289 290 // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both.. 291 // 292 func (f IndicesValidateQuery) WithExpandWildcards(v string) func(*IndicesValidateQueryRequest) { 293 return func(r *IndicesValidateQueryRequest) { 294 r.ExpandWildcards = v 295 } 296 } 297 298 // WithExplain - return detailed information about the error. 299 // 300 func (f IndicesValidateQuery) WithExplain(v bool) func(*IndicesValidateQueryRequest) { 301 return func(r *IndicesValidateQueryRequest) { 302 r.Explain = &v 303 } 304 } 305 306 // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed). 307 // 308 func (f IndicesValidateQuery) WithIgnoreUnavailable(v bool) func(*IndicesValidateQueryRequest) { 309 return func(r *IndicesValidateQueryRequest) { 310 r.IgnoreUnavailable = &v 311 } 312 } 313 314 // WithLenient - specify whether format-based query failures (such as providing text to a numeric field) should be ignored. 315 // 316 func (f IndicesValidateQuery) WithLenient(v bool) func(*IndicesValidateQueryRequest) { 317 return func(r *IndicesValidateQueryRequest) { 318 r.Lenient = &v 319 } 320 } 321 322 // WithQuery - query in the lucene query string syntax. 323 // 324 func (f IndicesValidateQuery) WithQuery(v string) func(*IndicesValidateQueryRequest) { 325 return func(r *IndicesValidateQueryRequest) { 326 r.Query = v 327 } 328 } 329 330 // WithRewrite - provide a more detailed explanation showing the actual lucene query that will be executed.. 331 // 332 func (f IndicesValidateQuery) WithRewrite(v bool) func(*IndicesValidateQueryRequest) { 333 return func(r *IndicesValidateQueryRequest) { 334 r.Rewrite = &v 335 } 336 } 337 338 // WithPretty makes the response body pretty-printed. 339 // 340 func (f IndicesValidateQuery) WithPretty() func(*IndicesValidateQueryRequest) { 341 return func(r *IndicesValidateQueryRequest) { 342 r.Pretty = true 343 } 344 } 345 346 // WithHuman makes statistical values human-readable. 347 // 348 func (f IndicesValidateQuery) WithHuman() func(*IndicesValidateQueryRequest) { 349 return func(r *IndicesValidateQueryRequest) { 350 r.Human = true 351 } 352 } 353 354 // WithErrorTrace includes the stack trace for errors in the response body. 355 // 356 func (f IndicesValidateQuery) WithErrorTrace() func(*IndicesValidateQueryRequest) { 357 return func(r *IndicesValidateQueryRequest) { 358 r.ErrorTrace = true 359 } 360 } 361 362 // WithFilterPath filters the properties of the response body. 363 // 364 func (f IndicesValidateQuery) WithFilterPath(v ...string) func(*IndicesValidateQueryRequest) { 365 return func(r *IndicesValidateQueryRequest) { 366 r.FilterPath = v 367 } 368 } 369 370 // WithHeader adds the headers to the HTTP request. 371 // 372 func (f IndicesValidateQuery) WithHeader(h map[string]string) func(*IndicesValidateQueryRequest) { 373 return func(r *IndicesValidateQueryRequest) { 374 if r.Header == nil { 375 r.Header = make(http.Header) 376 } 377 for k, v := range h { 378 r.Header.Add(k, v) 379 } 380 } 381 } 382 383 // WithOpaqueID adds the X-Opaque-Id header to the HTTP request. 384 // 385 func (f IndicesValidateQuery) WithOpaqueID(s string) func(*IndicesValidateQueryRequest) { 386 return func(r *IndicesValidateQueryRequest) { 387 if r.Header == nil { 388 r.Header = make(http.Header) 389 } 390 r.Header.Set("X-Opaque-Id", s) 391 } 392 }