api.msearch_template.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 newMsearchTemplateFunc(t Transport) MsearchTemplate {
  28. return func(body io.Reader, o ...func(*MsearchTemplateRequest)) (*Response, error) {
  29. var r = MsearchTemplateRequest{Body: body}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // MsearchTemplate allows to execute several search template operations in one request.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html.
  40. type MsearchTemplate func(body io.Reader, o ...func(*MsearchTemplateRequest)) (*Response, error)
  41. // MsearchTemplateRequest configures the Msearch Template API request.
  42. type MsearchTemplateRequest struct {
  43. Index []string
  44. DocumentType []string
  45. Body io.Reader
  46. CcsMinimizeRoundtrips *bool
  47. MaxConcurrentSearches *int
  48. RestTotalHitsAsInt *bool
  49. SearchType string
  50. TypedKeys *bool
  51. Pretty bool
  52. Human bool
  53. ErrorTrace bool
  54. FilterPath []string
  55. Header http.Header
  56. ctx context.Context
  57. }
  58. // Do executes the request and returns response or error.
  59. func (r MsearchTemplateRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  60. var (
  61. method string
  62. path strings.Builder
  63. params map[string]string
  64. )
  65. method = "POST"
  66. path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len(strings.Join(r.DocumentType, ",")) + 1 + len("_msearch") + 1 + len("template"))
  67. if len(r.Index) > 0 {
  68. path.WriteString("/")
  69. path.WriteString(strings.Join(r.Index, ","))
  70. }
  71. if len(r.DocumentType) > 0 {
  72. path.WriteString("/")
  73. path.WriteString(strings.Join(r.DocumentType, ","))
  74. }
  75. path.WriteString("/")
  76. path.WriteString("_msearch")
  77. path.WriteString("/")
  78. path.WriteString("template")
  79. params = make(map[string]string)
  80. if r.CcsMinimizeRoundtrips != nil {
  81. params["ccs_minimize_roundtrips"] = strconv.FormatBool(*r.CcsMinimizeRoundtrips)
  82. }
  83. if r.MaxConcurrentSearches != nil {
  84. params["max_concurrent_searches"] = strconv.FormatInt(int64(*r.MaxConcurrentSearches), 10)
  85. }
  86. if r.RestTotalHitsAsInt != nil {
  87. params["rest_total_hits_as_int"] = strconv.FormatBool(*r.RestTotalHitsAsInt)
  88. }
  89. if r.SearchType != "" {
  90. params["search_type"] = r.SearchType
  91. }
  92. if r.TypedKeys != nil {
  93. params["typed_keys"] = strconv.FormatBool(*r.TypedKeys)
  94. }
  95. if r.Pretty {
  96. params["pretty"] = "true"
  97. }
  98. if r.Human {
  99. params["human"] = "true"
  100. }
  101. if r.ErrorTrace {
  102. params["error_trace"] = "true"
  103. }
  104. if len(r.FilterPath) > 0 {
  105. params["filter_path"] = strings.Join(r.FilterPath, ",")
  106. }
  107. req, err := newRequest(method, path.String(), r.Body)
  108. if err != nil {
  109. return nil, err
  110. }
  111. if len(params) > 0 {
  112. q := req.URL.Query()
  113. for k, v := range params {
  114. q.Set(k, v)
  115. }
  116. req.URL.RawQuery = q.Encode()
  117. }
  118. if len(r.Header) > 0 {
  119. if len(req.Header) == 0 {
  120. req.Header = r.Header
  121. } else {
  122. for k, vv := range r.Header {
  123. for _, v := range vv {
  124. req.Header.Add(k, v)
  125. }
  126. }
  127. }
  128. }
  129. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  130. req.Header[headerContentType] = headerContentTypeJSON
  131. }
  132. if ctx != nil {
  133. req = req.WithContext(ctx)
  134. }
  135. res, err := transport.Perform(req)
  136. if err != nil {
  137. return nil, err
  138. }
  139. response := Response{
  140. StatusCode: res.StatusCode,
  141. Body: res.Body,
  142. Header: res.Header,
  143. }
  144. return &response, nil
  145. }
  146. // WithContext sets the request context.
  147. func (f MsearchTemplate) WithContext(v context.Context) func(*MsearchTemplateRequest) {
  148. return func(r *MsearchTemplateRequest) {
  149. r.ctx = v
  150. }
  151. }
  152. // WithIndex - a list of index names to use as default.
  153. func (f MsearchTemplate) WithIndex(v ...string) func(*MsearchTemplateRequest) {
  154. return func(r *MsearchTemplateRequest) {
  155. r.Index = v
  156. }
  157. }
  158. // WithDocumentType - a list of document types to use as default.
  159. func (f MsearchTemplate) WithDocumentType(v ...string) func(*MsearchTemplateRequest) {
  160. return func(r *MsearchTemplateRequest) {
  161. r.DocumentType = v
  162. }
  163. }
  164. // WithCcsMinimizeRoundtrips - indicates whether network round-trips should be minimized as part of cross-cluster search requests execution.
  165. func (f MsearchTemplate) WithCcsMinimizeRoundtrips(v bool) func(*MsearchTemplateRequest) {
  166. return func(r *MsearchTemplateRequest) {
  167. r.CcsMinimizeRoundtrips = &v
  168. }
  169. }
  170. // WithMaxConcurrentSearches - controls the maximum number of concurrent searches the multi search api will execute.
  171. func (f MsearchTemplate) WithMaxConcurrentSearches(v int) func(*MsearchTemplateRequest) {
  172. return func(r *MsearchTemplateRequest) {
  173. r.MaxConcurrentSearches = &v
  174. }
  175. }
  176. // WithRestTotalHitsAsInt - indicates whether hits.total should be rendered as an integer or an object in the rest search response.
  177. func (f MsearchTemplate) WithRestTotalHitsAsInt(v bool) func(*MsearchTemplateRequest) {
  178. return func(r *MsearchTemplateRequest) {
  179. r.RestTotalHitsAsInt = &v
  180. }
  181. }
  182. // WithSearchType - search operation type.
  183. func (f MsearchTemplate) WithSearchType(v string) func(*MsearchTemplateRequest) {
  184. return func(r *MsearchTemplateRequest) {
  185. r.SearchType = v
  186. }
  187. }
  188. // WithTypedKeys - specify whether aggregation and suggester names should be prefixed by their respective types in the response.
  189. func (f MsearchTemplate) WithTypedKeys(v bool) func(*MsearchTemplateRequest) {
  190. return func(r *MsearchTemplateRequest) {
  191. r.TypedKeys = &v
  192. }
  193. }
  194. // WithPretty makes the response body pretty-printed.
  195. func (f MsearchTemplate) WithPretty() func(*MsearchTemplateRequest) {
  196. return func(r *MsearchTemplateRequest) {
  197. r.Pretty = true
  198. }
  199. }
  200. // WithHuman makes statistical values human-readable.
  201. func (f MsearchTemplate) WithHuman() func(*MsearchTemplateRequest) {
  202. return func(r *MsearchTemplateRequest) {
  203. r.Human = true
  204. }
  205. }
  206. // WithErrorTrace includes the stack trace for errors in the response body.
  207. func (f MsearchTemplate) WithErrorTrace() func(*MsearchTemplateRequest) {
  208. return func(r *MsearchTemplateRequest) {
  209. r.ErrorTrace = true
  210. }
  211. }
  212. // WithFilterPath filters the properties of the response body.
  213. func (f MsearchTemplate) WithFilterPath(v ...string) func(*MsearchTemplateRequest) {
  214. return func(r *MsearchTemplateRequest) {
  215. r.FilterPath = v
  216. }
  217. }
  218. // WithHeader adds the headers to the HTTP request.
  219. func (f MsearchTemplate) WithHeader(h map[string]string) func(*MsearchTemplateRequest) {
  220. return func(r *MsearchTemplateRequest) {
  221. if r.Header == nil {
  222. r.Header = make(http.Header)
  223. }
  224. for k, v := range h {
  225. r.Header.Add(k, v)
  226. }
  227. }
  228. }
  229. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  230. func (f MsearchTemplate) WithOpaqueID(s string) func(*MsearchTemplateRequest) {
  231. return func(r *MsearchTemplateRequest) {
  232. if r.Header == nil {
  233. r.Header = make(http.Header)
  234. }
  235. r.Header.Set("X-Opaque-Id", s)
  236. }
  237. }