api.indices.validate_query.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // Licensed to Elasticsearch B.V. under one or more contributor
  2. // license agreements. See the NOTICE file distributed with
  3. // this work for additional information regarding copyright
  4. // ownership. Elasticsearch B.V. licenses this file to you under
  5. // the Apache License, Version 2.0 (the "License"); you may
  6. // not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. //
  18. // Code generated from specification version 7.17.10: DO NOT EDIT
  19. package esapi
  20. import (
  21. "context"
  22. "io"
  23. "net/http"
  24. "strconv"
  25. "strings"
  26. )
  27. func newIndicesValidateQueryFunc(t Transport) IndicesValidateQuery {
  28. return func(o ...func(*IndicesValidateQueryRequest)) (*Response, error) {
  29. var r = IndicesValidateQueryRequest{}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // IndicesValidateQuery allows a user to validate a potentially expensive query without executing it.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/search-validate.html.
  40. type IndicesValidateQuery func(o ...func(*IndicesValidateQueryRequest)) (*Response, error)
  41. // IndicesValidateQueryRequest configures the Indices Validate Query API request.
  42. type IndicesValidateQueryRequest struct {
  43. Index []string
  44. DocumentType []string
  45. Body io.Reader
  46. AllowNoIndices *bool
  47. AllShards *bool
  48. Analyzer string
  49. AnalyzeWildcard *bool
  50. DefaultOperator string
  51. Df string
  52. ExpandWildcards string
  53. Explain *bool
  54. IgnoreUnavailable *bool
  55. Lenient *bool
  56. Query string
  57. Rewrite *bool
  58. Pretty bool
  59. Human bool
  60. ErrorTrace bool
  61. FilterPath []string
  62. Header http.Header
  63. ctx context.Context
  64. }
  65. // Do executes the request and returns response or error.
  66. func (r IndicesValidateQueryRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  67. var (
  68. method string
  69. path strings.Builder
  70. params map[string]string
  71. )
  72. method = "POST"
  73. path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len(strings.Join(r.DocumentType, ",")) + 1 + len("_validate") + 1 + len("query"))
  74. if len(r.Index) > 0 {
  75. path.WriteString("/")
  76. path.WriteString(strings.Join(r.Index, ","))
  77. }
  78. if len(r.DocumentType) > 0 {
  79. path.WriteString("/")
  80. path.WriteString(strings.Join(r.DocumentType, ","))
  81. }
  82. path.WriteString("/")
  83. path.WriteString("_validate")
  84. path.WriteString("/")
  85. path.WriteString("query")
  86. params = make(map[string]string)
  87. if r.AllowNoIndices != nil {
  88. params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices)
  89. }
  90. if r.AllShards != nil {
  91. params["all_shards"] = strconv.FormatBool(*r.AllShards)
  92. }
  93. if r.Analyzer != "" {
  94. params["analyzer"] = r.Analyzer
  95. }
  96. if r.AnalyzeWildcard != nil {
  97. params["analyze_wildcard"] = strconv.FormatBool(*r.AnalyzeWildcard)
  98. }
  99. if r.DefaultOperator != "" {
  100. params["default_operator"] = r.DefaultOperator
  101. }
  102. if r.Df != "" {
  103. params["df"] = r.Df
  104. }
  105. if r.ExpandWildcards != "" {
  106. params["expand_wildcards"] = r.ExpandWildcards
  107. }
  108. if r.Explain != nil {
  109. params["explain"] = strconv.FormatBool(*r.Explain)
  110. }
  111. if r.IgnoreUnavailable != nil {
  112. params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
  113. }
  114. if r.Lenient != nil {
  115. params["lenient"] = strconv.FormatBool(*r.Lenient)
  116. }
  117. if r.Query != "" {
  118. params["q"] = r.Query
  119. }
  120. if r.Rewrite != nil {
  121. params["rewrite"] = strconv.FormatBool(*r.Rewrite)
  122. }
  123. if r.Pretty {
  124. params["pretty"] = "true"
  125. }
  126. if r.Human {
  127. params["human"] = "true"
  128. }
  129. if r.ErrorTrace {
  130. params["error_trace"] = "true"
  131. }
  132. if len(r.FilterPath) > 0 {
  133. params["filter_path"] = strings.Join(r.FilterPath, ",")
  134. }
  135. req, err := newRequest(method, path.String(), r.Body)
  136. if err != nil {
  137. return nil, err
  138. }
  139. if len(params) > 0 {
  140. q := req.URL.Query()
  141. for k, v := range params {
  142. q.Set(k, v)
  143. }
  144. req.URL.RawQuery = q.Encode()
  145. }
  146. if len(r.Header) > 0 {
  147. if len(req.Header) == 0 {
  148. req.Header = r.Header
  149. } else {
  150. for k, vv := range r.Header {
  151. for _, v := range vv {
  152. req.Header.Add(k, v)
  153. }
  154. }
  155. }
  156. }
  157. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  158. req.Header[headerContentType] = headerContentTypeJSON
  159. }
  160. if ctx != nil {
  161. req = req.WithContext(ctx)
  162. }
  163. res, err := transport.Perform(req)
  164. if err != nil {
  165. return nil, err
  166. }
  167. response := Response{
  168. StatusCode: res.StatusCode,
  169. Body: res.Body,
  170. Header: res.Header,
  171. }
  172. return &response, nil
  173. }
  174. // WithContext sets the request context.
  175. func (f IndicesValidateQuery) WithContext(v context.Context) func(*IndicesValidateQueryRequest) {
  176. return func(r *IndicesValidateQueryRequest) {
  177. r.ctx = v
  178. }
  179. }
  180. // WithBody - The query definition specified with the Query DSL.
  181. func (f IndicesValidateQuery) WithBody(v io.Reader) func(*IndicesValidateQueryRequest) {
  182. return func(r *IndicesValidateQueryRequest) {
  183. r.Body = v
  184. }
  185. }
  186. // WithIndex - a list of index names to restrict the operation; use _all to perform the operation on all indices.
  187. func (f IndicesValidateQuery) WithIndex(v ...string) func(*IndicesValidateQueryRequest) {
  188. return func(r *IndicesValidateQueryRequest) {
  189. r.Index = v
  190. }
  191. }
  192. // WithDocumentType - a list of document types to restrict the operation; leave empty to perform the operation on all types.
  193. func (f IndicesValidateQuery) WithDocumentType(v ...string) func(*IndicesValidateQueryRequest) {
  194. return func(r *IndicesValidateQueryRequest) {
  195. r.DocumentType = v
  196. }
  197. }
  198. // 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).
  199. func (f IndicesValidateQuery) WithAllowNoIndices(v bool) func(*IndicesValidateQueryRequest) {
  200. return func(r *IndicesValidateQueryRequest) {
  201. r.AllowNoIndices = &v
  202. }
  203. }
  204. // WithAllShards - execute validation on all shards instead of one random shard per index.
  205. func (f IndicesValidateQuery) WithAllShards(v bool) func(*IndicesValidateQueryRequest) {
  206. return func(r *IndicesValidateQueryRequest) {
  207. r.AllShards = &v
  208. }
  209. }
  210. // WithAnalyzer - the analyzer to use for the query string.
  211. func (f IndicesValidateQuery) WithAnalyzer(v string) func(*IndicesValidateQueryRequest) {
  212. return func(r *IndicesValidateQueryRequest) {
  213. r.Analyzer = v
  214. }
  215. }
  216. // WithAnalyzeWildcard - specify whether wildcard and prefix queries should be analyzed (default: false).
  217. func (f IndicesValidateQuery) WithAnalyzeWildcard(v bool) func(*IndicesValidateQueryRequest) {
  218. return func(r *IndicesValidateQueryRequest) {
  219. r.AnalyzeWildcard = &v
  220. }
  221. }
  222. // WithDefaultOperator - the default operator for query string query (and or or).
  223. func (f IndicesValidateQuery) WithDefaultOperator(v string) func(*IndicesValidateQueryRequest) {
  224. return func(r *IndicesValidateQueryRequest) {
  225. r.DefaultOperator = v
  226. }
  227. }
  228. // WithDf - the field to use as default where no field prefix is given in the query string.
  229. func (f IndicesValidateQuery) WithDf(v string) func(*IndicesValidateQueryRequest) {
  230. return func(r *IndicesValidateQueryRequest) {
  231. r.Df = v
  232. }
  233. }
  234. // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
  235. func (f IndicesValidateQuery) WithExpandWildcards(v string) func(*IndicesValidateQueryRequest) {
  236. return func(r *IndicesValidateQueryRequest) {
  237. r.ExpandWildcards = v
  238. }
  239. }
  240. // WithExplain - return detailed information about the error.
  241. func (f IndicesValidateQuery) WithExplain(v bool) func(*IndicesValidateQueryRequest) {
  242. return func(r *IndicesValidateQueryRequest) {
  243. r.Explain = &v
  244. }
  245. }
  246. // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed).
  247. func (f IndicesValidateQuery) WithIgnoreUnavailable(v bool) func(*IndicesValidateQueryRequest) {
  248. return func(r *IndicesValidateQueryRequest) {
  249. r.IgnoreUnavailable = &v
  250. }
  251. }
  252. // WithLenient - specify whether format-based query failures (such as providing text to a numeric field) should be ignored.
  253. func (f IndicesValidateQuery) WithLenient(v bool) func(*IndicesValidateQueryRequest) {
  254. return func(r *IndicesValidateQueryRequest) {
  255. r.Lenient = &v
  256. }
  257. }
  258. // WithQuery - query in the lucene query string syntax.
  259. func (f IndicesValidateQuery) WithQuery(v string) func(*IndicesValidateQueryRequest) {
  260. return func(r *IndicesValidateQueryRequest) {
  261. r.Query = v
  262. }
  263. }
  264. // WithRewrite - provide a more detailed explanation showing the actual lucene query that will be executed..
  265. func (f IndicesValidateQuery) WithRewrite(v bool) func(*IndicesValidateQueryRequest) {
  266. return func(r *IndicesValidateQueryRequest) {
  267. r.Rewrite = &v
  268. }
  269. }
  270. // WithPretty makes the response body pretty-printed.
  271. func (f IndicesValidateQuery) WithPretty() func(*IndicesValidateQueryRequest) {
  272. return func(r *IndicesValidateQueryRequest) {
  273. r.Pretty = true
  274. }
  275. }
  276. // WithHuman makes statistical values human-readable.
  277. func (f IndicesValidateQuery) WithHuman() func(*IndicesValidateQueryRequest) {
  278. return func(r *IndicesValidateQueryRequest) {
  279. r.Human = true
  280. }
  281. }
  282. // WithErrorTrace includes the stack trace for errors in the response body.
  283. func (f IndicesValidateQuery) WithErrorTrace() func(*IndicesValidateQueryRequest) {
  284. return func(r *IndicesValidateQueryRequest) {
  285. r.ErrorTrace = true
  286. }
  287. }
  288. // WithFilterPath filters the properties of the response body.
  289. func (f IndicesValidateQuery) WithFilterPath(v ...string) func(*IndicesValidateQueryRequest) {
  290. return func(r *IndicesValidateQueryRequest) {
  291. r.FilterPath = v
  292. }
  293. }
  294. // WithHeader adds the headers to the HTTP request.
  295. func (f IndicesValidateQuery) WithHeader(h map[string]string) func(*IndicesValidateQueryRequest) {
  296. return func(r *IndicesValidateQueryRequest) {
  297. if r.Header == nil {
  298. r.Header = make(http.Header)
  299. }
  300. for k, v := range h {
  301. r.Header.Add(k, v)
  302. }
  303. }
  304. }
  305. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  306. func (f IndicesValidateQuery) WithOpaqueID(s string) func(*IndicesValidateQueryRequest) {
  307. return func(r *IndicesValidateQueryRequest) {
  308. if r.Header == nil {
  309. r.Header = make(http.Header)
  310. }
  311. r.Header.Set("X-Opaque-Id", s)
  312. }
  313. }