api.search_template.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. "time"
  27. )
  28. func newSearchTemplateFunc(t Transport) SearchTemplate {
  29. return func(body io.Reader, o ...func(*SearchTemplateRequest)) (*Response, error) {
  30. var r = SearchTemplateRequest{Body: body}
  31. for _, f := range o {
  32. f(&r)
  33. }
  34. return r.Do(r.ctx, t)
  35. }
  36. }
  37. // ----- API Definition -------------------------------------------------------
  38. // SearchTemplate allows to use the Mustache language to pre-render a search definition.
  39. //
  40. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html.
  41. type SearchTemplate func(body io.Reader, o ...func(*SearchTemplateRequest)) (*Response, error)
  42. // SearchTemplateRequest configures the Search Template API request.
  43. type SearchTemplateRequest struct {
  44. Index []string
  45. DocumentType []string
  46. Body io.Reader
  47. AllowNoIndices *bool
  48. CcsMinimizeRoundtrips *bool
  49. ExpandWildcards string
  50. Explain *bool
  51. IgnoreThrottled *bool
  52. IgnoreUnavailable *bool
  53. Preference string
  54. Profile *bool
  55. RestTotalHitsAsInt *bool
  56. Routing []string
  57. Scroll time.Duration
  58. SearchType string
  59. TypedKeys *bool
  60. Pretty bool
  61. Human bool
  62. ErrorTrace bool
  63. FilterPath []string
  64. Header http.Header
  65. ctx context.Context
  66. }
  67. // Do executes the request and returns response or error.
  68. func (r SearchTemplateRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  69. var (
  70. method string
  71. path strings.Builder
  72. params map[string]string
  73. )
  74. method = "POST"
  75. path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len(strings.Join(r.DocumentType, ",")) + 1 + len("_search") + 1 + len("template"))
  76. if len(r.Index) > 0 {
  77. path.WriteString("/")
  78. path.WriteString(strings.Join(r.Index, ","))
  79. }
  80. if len(r.DocumentType) > 0 {
  81. path.WriteString("/")
  82. path.WriteString(strings.Join(r.DocumentType, ","))
  83. }
  84. path.WriteString("/")
  85. path.WriteString("_search")
  86. path.WriteString("/")
  87. path.WriteString("template")
  88. params = make(map[string]string)
  89. if r.AllowNoIndices != nil {
  90. params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices)
  91. }
  92. if r.CcsMinimizeRoundtrips != nil {
  93. params["ccs_minimize_roundtrips"] = strconv.FormatBool(*r.CcsMinimizeRoundtrips)
  94. }
  95. if r.ExpandWildcards != "" {
  96. params["expand_wildcards"] = r.ExpandWildcards
  97. }
  98. if r.Explain != nil {
  99. params["explain"] = strconv.FormatBool(*r.Explain)
  100. }
  101. if r.IgnoreThrottled != nil {
  102. params["ignore_throttled"] = strconv.FormatBool(*r.IgnoreThrottled)
  103. }
  104. if r.IgnoreUnavailable != nil {
  105. params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
  106. }
  107. if r.Preference != "" {
  108. params["preference"] = r.Preference
  109. }
  110. if r.Profile != nil {
  111. params["profile"] = strconv.FormatBool(*r.Profile)
  112. }
  113. if r.RestTotalHitsAsInt != nil {
  114. params["rest_total_hits_as_int"] = strconv.FormatBool(*r.RestTotalHitsAsInt)
  115. }
  116. if len(r.Routing) > 0 {
  117. params["routing"] = strings.Join(r.Routing, ",")
  118. }
  119. if r.Scroll != 0 {
  120. params["scroll"] = formatDuration(r.Scroll)
  121. }
  122. if r.SearchType != "" {
  123. params["search_type"] = r.SearchType
  124. }
  125. if r.TypedKeys != nil {
  126. params["typed_keys"] = strconv.FormatBool(*r.TypedKeys)
  127. }
  128. if r.Pretty {
  129. params["pretty"] = "true"
  130. }
  131. if r.Human {
  132. params["human"] = "true"
  133. }
  134. if r.ErrorTrace {
  135. params["error_trace"] = "true"
  136. }
  137. if len(r.FilterPath) > 0 {
  138. params["filter_path"] = strings.Join(r.FilterPath, ",")
  139. }
  140. req, err := newRequest(method, path.String(), r.Body)
  141. if err != nil {
  142. return nil, err
  143. }
  144. if len(params) > 0 {
  145. q := req.URL.Query()
  146. for k, v := range params {
  147. q.Set(k, v)
  148. }
  149. req.URL.RawQuery = q.Encode()
  150. }
  151. if len(r.Header) > 0 {
  152. if len(req.Header) == 0 {
  153. req.Header = r.Header
  154. } else {
  155. for k, vv := range r.Header {
  156. for _, v := range vv {
  157. req.Header.Add(k, v)
  158. }
  159. }
  160. }
  161. }
  162. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  163. req.Header[headerContentType] = headerContentTypeJSON
  164. }
  165. if ctx != nil {
  166. req = req.WithContext(ctx)
  167. }
  168. res, err := transport.Perform(req)
  169. if err != nil {
  170. return nil, err
  171. }
  172. response := Response{
  173. StatusCode: res.StatusCode,
  174. Body: res.Body,
  175. Header: res.Header,
  176. }
  177. return &response, nil
  178. }
  179. // WithContext sets the request context.
  180. func (f SearchTemplate) WithContext(v context.Context) func(*SearchTemplateRequest) {
  181. return func(r *SearchTemplateRequest) {
  182. r.ctx = v
  183. }
  184. }
  185. // WithIndex - a list of index names to search; use _all to perform the operation on all indices.
  186. func (f SearchTemplate) WithIndex(v ...string) func(*SearchTemplateRequest) {
  187. return func(r *SearchTemplateRequest) {
  188. r.Index = v
  189. }
  190. }
  191. // WithDocumentType - a list of document types to search; leave empty to perform the operation on all types.
  192. func (f SearchTemplate) WithDocumentType(v ...string) func(*SearchTemplateRequest) {
  193. return func(r *SearchTemplateRequest) {
  194. r.DocumentType = v
  195. }
  196. }
  197. // 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).
  198. func (f SearchTemplate) WithAllowNoIndices(v bool) func(*SearchTemplateRequest) {
  199. return func(r *SearchTemplateRequest) {
  200. r.AllowNoIndices = &v
  201. }
  202. }
  203. // WithCcsMinimizeRoundtrips - indicates whether network round-trips should be minimized as part of cross-cluster search requests execution.
  204. func (f SearchTemplate) WithCcsMinimizeRoundtrips(v bool) func(*SearchTemplateRequest) {
  205. return func(r *SearchTemplateRequest) {
  206. r.CcsMinimizeRoundtrips = &v
  207. }
  208. }
  209. // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
  210. func (f SearchTemplate) WithExpandWildcards(v string) func(*SearchTemplateRequest) {
  211. return func(r *SearchTemplateRequest) {
  212. r.ExpandWildcards = v
  213. }
  214. }
  215. // WithExplain - specify whether to return detailed information about score computation as part of a hit.
  216. func (f SearchTemplate) WithExplain(v bool) func(*SearchTemplateRequest) {
  217. return func(r *SearchTemplateRequest) {
  218. r.Explain = &v
  219. }
  220. }
  221. // WithIgnoreThrottled - whether specified concrete, expanded or aliased indices should be ignored when throttled.
  222. func (f SearchTemplate) WithIgnoreThrottled(v bool) func(*SearchTemplateRequest) {
  223. return func(r *SearchTemplateRequest) {
  224. r.IgnoreThrottled = &v
  225. }
  226. }
  227. // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed).
  228. func (f SearchTemplate) WithIgnoreUnavailable(v bool) func(*SearchTemplateRequest) {
  229. return func(r *SearchTemplateRequest) {
  230. r.IgnoreUnavailable = &v
  231. }
  232. }
  233. // WithPreference - specify the node or shard the operation should be performed on (default: random).
  234. func (f SearchTemplate) WithPreference(v string) func(*SearchTemplateRequest) {
  235. return func(r *SearchTemplateRequest) {
  236. r.Preference = v
  237. }
  238. }
  239. // WithProfile - specify whether to profile the query execution.
  240. func (f SearchTemplate) WithProfile(v bool) func(*SearchTemplateRequest) {
  241. return func(r *SearchTemplateRequest) {
  242. r.Profile = &v
  243. }
  244. }
  245. // WithRestTotalHitsAsInt - indicates whether hits.total should be rendered as an integer or an object in the rest search response.
  246. func (f SearchTemplate) WithRestTotalHitsAsInt(v bool) func(*SearchTemplateRequest) {
  247. return func(r *SearchTemplateRequest) {
  248. r.RestTotalHitsAsInt = &v
  249. }
  250. }
  251. // WithRouting - a list of specific routing values.
  252. func (f SearchTemplate) WithRouting(v ...string) func(*SearchTemplateRequest) {
  253. return func(r *SearchTemplateRequest) {
  254. r.Routing = v
  255. }
  256. }
  257. // WithScroll - specify how long a consistent view of the index should be maintained for scrolled search.
  258. func (f SearchTemplate) WithScroll(v time.Duration) func(*SearchTemplateRequest) {
  259. return func(r *SearchTemplateRequest) {
  260. r.Scroll = v
  261. }
  262. }
  263. // WithSearchType - search operation type.
  264. func (f SearchTemplate) WithSearchType(v string) func(*SearchTemplateRequest) {
  265. return func(r *SearchTemplateRequest) {
  266. r.SearchType = v
  267. }
  268. }
  269. // WithTypedKeys - specify whether aggregation and suggester names should be prefixed by their respective types in the response.
  270. func (f SearchTemplate) WithTypedKeys(v bool) func(*SearchTemplateRequest) {
  271. return func(r *SearchTemplateRequest) {
  272. r.TypedKeys = &v
  273. }
  274. }
  275. // WithPretty makes the response body pretty-printed.
  276. func (f SearchTemplate) WithPretty() func(*SearchTemplateRequest) {
  277. return func(r *SearchTemplateRequest) {
  278. r.Pretty = true
  279. }
  280. }
  281. // WithHuman makes statistical values human-readable.
  282. func (f SearchTemplate) WithHuman() func(*SearchTemplateRequest) {
  283. return func(r *SearchTemplateRequest) {
  284. r.Human = true
  285. }
  286. }
  287. // WithErrorTrace includes the stack trace for errors in the response body.
  288. func (f SearchTemplate) WithErrorTrace() func(*SearchTemplateRequest) {
  289. return func(r *SearchTemplateRequest) {
  290. r.ErrorTrace = true
  291. }
  292. }
  293. // WithFilterPath filters the properties of the response body.
  294. func (f SearchTemplate) WithFilterPath(v ...string) func(*SearchTemplateRequest) {
  295. return func(r *SearchTemplateRequest) {
  296. r.FilterPath = v
  297. }
  298. }
  299. // WithHeader adds the headers to the HTTP request.
  300. func (f SearchTemplate) WithHeader(h map[string]string) func(*SearchTemplateRequest) {
  301. return func(r *SearchTemplateRequest) {
  302. if r.Header == nil {
  303. r.Header = make(http.Header)
  304. }
  305. for k, v := range h {
  306. r.Header.Add(k, v)
  307. }
  308. }
  309. }
  310. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  311. func (f SearchTemplate) WithOpaqueID(s string) func(*SearchTemplateRequest) {
  312. return func(r *SearchTemplateRequest) {
  313. if r.Header == nil {
  314. r.Header = make(http.Header)
  315. }
  316. r.Header.Set("X-Opaque-Id", s)
  317. }
  318. }