api.explain.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 newExplainFunc(t Transport) Explain {
  28. return func(index string, id string, o ...func(*ExplainRequest)) (*Response, error) {
  29. var r = ExplainRequest{Index: index, DocumentID: id}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // Explain returns information about why a specific matches (or doesn't match) a query.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/search-explain.html.
  40. type Explain func(index string, id string, o ...func(*ExplainRequest)) (*Response, error)
  41. // ExplainRequest configures the Explain API request.
  42. type ExplainRequest struct {
  43. Index string
  44. DocumentType string
  45. DocumentID string
  46. Body io.Reader
  47. Analyzer string
  48. AnalyzeWildcard *bool
  49. DefaultOperator string
  50. Df string
  51. Lenient *bool
  52. Preference string
  53. Query string
  54. Routing string
  55. Source []string
  56. SourceExcludes []string
  57. SourceIncludes []string
  58. StoredFields []string
  59. Pretty bool
  60. Human bool
  61. ErrorTrace bool
  62. FilterPath []string
  63. Header http.Header
  64. ctx context.Context
  65. }
  66. // Do executes the request and returns response or error.
  67. func (r ExplainRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  68. var (
  69. method string
  70. path strings.Builder
  71. params map[string]string
  72. )
  73. method = "POST"
  74. if r.DocumentType == "" {
  75. r.DocumentType = "_doc"
  76. }
  77. path.Grow(1 + len(r.Index) + 1 + len(r.DocumentType) + 1 + len(r.DocumentID) + 1 + len("_explain"))
  78. path.WriteString("/")
  79. path.WriteString(r.Index)
  80. if r.DocumentType != "" {
  81. path.WriteString("/")
  82. path.WriteString(r.DocumentType)
  83. }
  84. path.WriteString("/")
  85. path.WriteString(r.DocumentID)
  86. path.WriteString("/")
  87. path.WriteString("_explain")
  88. params = make(map[string]string)
  89. if r.Analyzer != "" {
  90. params["analyzer"] = r.Analyzer
  91. }
  92. if r.AnalyzeWildcard != nil {
  93. params["analyze_wildcard"] = strconv.FormatBool(*r.AnalyzeWildcard)
  94. }
  95. if r.DefaultOperator != "" {
  96. params["default_operator"] = r.DefaultOperator
  97. }
  98. if r.Df != "" {
  99. params["df"] = r.Df
  100. }
  101. if r.Lenient != nil {
  102. params["lenient"] = strconv.FormatBool(*r.Lenient)
  103. }
  104. if r.Preference != "" {
  105. params["preference"] = r.Preference
  106. }
  107. if r.Query != "" {
  108. params["q"] = r.Query
  109. }
  110. if r.Routing != "" {
  111. params["routing"] = r.Routing
  112. }
  113. if len(r.Source) > 0 {
  114. params["_source"] = strings.Join(r.Source, ",")
  115. }
  116. if len(r.SourceExcludes) > 0 {
  117. params["_source_excludes"] = strings.Join(r.SourceExcludes, ",")
  118. }
  119. if len(r.SourceIncludes) > 0 {
  120. params["_source_includes"] = strings.Join(r.SourceIncludes, ",")
  121. }
  122. if len(r.StoredFields) > 0 {
  123. params["stored_fields"] = strings.Join(r.StoredFields, ",")
  124. }
  125. if r.Pretty {
  126. params["pretty"] = "true"
  127. }
  128. if r.Human {
  129. params["human"] = "true"
  130. }
  131. if r.ErrorTrace {
  132. params["error_trace"] = "true"
  133. }
  134. if len(r.FilterPath) > 0 {
  135. params["filter_path"] = strings.Join(r.FilterPath, ",")
  136. }
  137. req, err := newRequest(method, path.String(), r.Body)
  138. if err != nil {
  139. return nil, err
  140. }
  141. if len(params) > 0 {
  142. q := req.URL.Query()
  143. for k, v := range params {
  144. q.Set(k, v)
  145. }
  146. req.URL.RawQuery = q.Encode()
  147. }
  148. if len(r.Header) > 0 {
  149. if len(req.Header) == 0 {
  150. req.Header = r.Header
  151. } else {
  152. for k, vv := range r.Header {
  153. for _, v := range vv {
  154. req.Header.Add(k, v)
  155. }
  156. }
  157. }
  158. }
  159. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  160. req.Header[headerContentType] = headerContentTypeJSON
  161. }
  162. if ctx != nil {
  163. req = req.WithContext(ctx)
  164. }
  165. res, err := transport.Perform(req)
  166. if err != nil {
  167. return nil, err
  168. }
  169. response := Response{
  170. StatusCode: res.StatusCode,
  171. Body: res.Body,
  172. Header: res.Header,
  173. }
  174. return &response, nil
  175. }
  176. // WithContext sets the request context.
  177. func (f Explain) WithContext(v context.Context) func(*ExplainRequest) {
  178. return func(r *ExplainRequest) {
  179. r.ctx = v
  180. }
  181. }
  182. // WithBody - The query definition using the Query DSL.
  183. func (f Explain) WithBody(v io.Reader) func(*ExplainRequest) {
  184. return func(r *ExplainRequest) {
  185. r.Body = v
  186. }
  187. }
  188. // WithDocumentType - the type of the document.
  189. func (f Explain) WithDocumentType(v string) func(*ExplainRequest) {
  190. return func(r *ExplainRequest) {
  191. r.DocumentType = v
  192. }
  193. }
  194. // WithAnalyzer - the analyzer for the query string query.
  195. func (f Explain) WithAnalyzer(v string) func(*ExplainRequest) {
  196. return func(r *ExplainRequest) {
  197. r.Analyzer = v
  198. }
  199. }
  200. // WithAnalyzeWildcard - specify whether wildcards and prefix queries in the query string query should be analyzed (default: false).
  201. func (f Explain) WithAnalyzeWildcard(v bool) func(*ExplainRequest) {
  202. return func(r *ExplainRequest) {
  203. r.AnalyzeWildcard = &v
  204. }
  205. }
  206. // WithDefaultOperator - the default operator for query string query (and or or).
  207. func (f Explain) WithDefaultOperator(v string) func(*ExplainRequest) {
  208. return func(r *ExplainRequest) {
  209. r.DefaultOperator = v
  210. }
  211. }
  212. // WithDf - the default field for query string query (default: _all).
  213. func (f Explain) WithDf(v string) func(*ExplainRequest) {
  214. return func(r *ExplainRequest) {
  215. r.Df = v
  216. }
  217. }
  218. // WithLenient - specify whether format-based query failures (such as providing text to a numeric field) should be ignored.
  219. func (f Explain) WithLenient(v bool) func(*ExplainRequest) {
  220. return func(r *ExplainRequest) {
  221. r.Lenient = &v
  222. }
  223. }
  224. // WithPreference - specify the node or shard the operation should be performed on (default: random).
  225. func (f Explain) WithPreference(v string) func(*ExplainRequest) {
  226. return func(r *ExplainRequest) {
  227. r.Preference = v
  228. }
  229. }
  230. // WithQuery - query in the lucene query string syntax.
  231. func (f Explain) WithQuery(v string) func(*ExplainRequest) {
  232. return func(r *ExplainRequest) {
  233. r.Query = v
  234. }
  235. }
  236. // WithRouting - specific routing value.
  237. func (f Explain) WithRouting(v string) func(*ExplainRequest) {
  238. return func(r *ExplainRequest) {
  239. r.Routing = v
  240. }
  241. }
  242. // WithSource - true or false to return the _source field or not, or a list of fields to return.
  243. func (f Explain) WithSource(v ...string) func(*ExplainRequest) {
  244. return func(r *ExplainRequest) {
  245. r.Source = v
  246. }
  247. }
  248. // WithSourceExcludes - a list of fields to exclude from the returned _source field.
  249. func (f Explain) WithSourceExcludes(v ...string) func(*ExplainRequest) {
  250. return func(r *ExplainRequest) {
  251. r.SourceExcludes = v
  252. }
  253. }
  254. // WithSourceIncludes - a list of fields to extract and return from the _source field.
  255. func (f Explain) WithSourceIncludes(v ...string) func(*ExplainRequest) {
  256. return func(r *ExplainRequest) {
  257. r.SourceIncludes = v
  258. }
  259. }
  260. // WithStoredFields - a list of stored fields to return in the response.
  261. func (f Explain) WithStoredFields(v ...string) func(*ExplainRequest) {
  262. return func(r *ExplainRequest) {
  263. r.StoredFields = v
  264. }
  265. }
  266. // WithPretty makes the response body pretty-printed.
  267. func (f Explain) WithPretty() func(*ExplainRequest) {
  268. return func(r *ExplainRequest) {
  269. r.Pretty = true
  270. }
  271. }
  272. // WithHuman makes statistical values human-readable.
  273. func (f Explain) WithHuman() func(*ExplainRequest) {
  274. return func(r *ExplainRequest) {
  275. r.Human = true
  276. }
  277. }
  278. // WithErrorTrace includes the stack trace for errors in the response body.
  279. func (f Explain) WithErrorTrace() func(*ExplainRequest) {
  280. return func(r *ExplainRequest) {
  281. r.ErrorTrace = true
  282. }
  283. }
  284. // WithFilterPath filters the properties of the response body.
  285. func (f Explain) WithFilterPath(v ...string) func(*ExplainRequest) {
  286. return func(r *ExplainRequest) {
  287. r.FilterPath = v
  288. }
  289. }
  290. // WithHeader adds the headers to the HTTP request.
  291. func (f Explain) WithHeader(h map[string]string) func(*ExplainRequest) {
  292. return func(r *ExplainRequest) {
  293. if r.Header == nil {
  294. r.Header = make(http.Header)
  295. }
  296. for k, v := range h {
  297. r.Header.Add(k, v)
  298. }
  299. }
  300. }
  301. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  302. func (f Explain) WithOpaqueID(s string) func(*ExplainRequest) {
  303. return func(r *ExplainRequest) {
  304. if r.Header == nil {
  305. r.Header = make(http.Header)
  306. }
  307. r.Header.Set("X-Opaque-Id", s)
  308. }
  309. }