api.mget.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 newMgetFunc(t Transport) Mget {
  28. return func(body io.Reader, o ...func(*MgetRequest)) (*Response, error) {
  29. var r = MgetRequest{Body: body}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // Mget allows to get multiple documents in one request.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-multi-get.html.
  40. type Mget func(body io.Reader, o ...func(*MgetRequest)) (*Response, error)
  41. // MgetRequest configures the Mget API request.
  42. type MgetRequest struct {
  43. Index string
  44. DocumentType string
  45. Body io.Reader
  46. Preference string
  47. Realtime *bool
  48. Refresh *bool
  49. Routing string
  50. Source []string
  51. SourceExcludes []string
  52. SourceIncludes []string
  53. StoredFields []string
  54. Pretty bool
  55. Human bool
  56. ErrorTrace bool
  57. FilterPath []string
  58. Header http.Header
  59. ctx context.Context
  60. }
  61. // Do executes the request and returns response or error.
  62. func (r MgetRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  63. var (
  64. method string
  65. path strings.Builder
  66. params map[string]string
  67. )
  68. method = "POST"
  69. path.Grow(1 + len(r.Index) + 1 + len(r.DocumentType) + 1 + len("_mget"))
  70. if r.Index != "" {
  71. path.WriteString("/")
  72. path.WriteString(r.Index)
  73. }
  74. if r.DocumentType != "" {
  75. path.WriteString("/")
  76. path.WriteString(r.DocumentType)
  77. }
  78. path.WriteString("/")
  79. path.WriteString("_mget")
  80. params = make(map[string]string)
  81. if r.Preference != "" {
  82. params["preference"] = r.Preference
  83. }
  84. if r.Realtime != nil {
  85. params["realtime"] = strconv.FormatBool(*r.Realtime)
  86. }
  87. if r.Refresh != nil {
  88. params["refresh"] = strconv.FormatBool(*r.Refresh)
  89. }
  90. if r.Routing != "" {
  91. params["routing"] = r.Routing
  92. }
  93. if len(r.Source) > 0 {
  94. params["_source"] = strings.Join(r.Source, ",")
  95. }
  96. if len(r.SourceExcludes) > 0 {
  97. params["_source_excludes"] = strings.Join(r.SourceExcludes, ",")
  98. }
  99. if len(r.SourceIncludes) > 0 {
  100. params["_source_includes"] = strings.Join(r.SourceIncludes, ",")
  101. }
  102. if len(r.StoredFields) > 0 {
  103. params["stored_fields"] = strings.Join(r.StoredFields, ",")
  104. }
  105. if r.Pretty {
  106. params["pretty"] = "true"
  107. }
  108. if r.Human {
  109. params["human"] = "true"
  110. }
  111. if r.ErrorTrace {
  112. params["error_trace"] = "true"
  113. }
  114. if len(r.FilterPath) > 0 {
  115. params["filter_path"] = strings.Join(r.FilterPath, ",")
  116. }
  117. req, err := newRequest(method, path.String(), r.Body)
  118. if err != nil {
  119. return nil, err
  120. }
  121. if len(params) > 0 {
  122. q := req.URL.Query()
  123. for k, v := range params {
  124. q.Set(k, v)
  125. }
  126. req.URL.RawQuery = q.Encode()
  127. }
  128. if len(r.Header) > 0 {
  129. if len(req.Header) == 0 {
  130. req.Header = r.Header
  131. } else {
  132. for k, vv := range r.Header {
  133. for _, v := range vv {
  134. req.Header.Add(k, v)
  135. }
  136. }
  137. }
  138. }
  139. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  140. req.Header[headerContentType] = headerContentTypeJSON
  141. }
  142. if ctx != nil {
  143. req = req.WithContext(ctx)
  144. }
  145. res, err := transport.Perform(req)
  146. if err != nil {
  147. return nil, err
  148. }
  149. response := Response{
  150. StatusCode: res.StatusCode,
  151. Body: res.Body,
  152. Header: res.Header,
  153. }
  154. return &response, nil
  155. }
  156. // WithContext sets the request context.
  157. func (f Mget) WithContext(v context.Context) func(*MgetRequest) {
  158. return func(r *MgetRequest) {
  159. r.ctx = v
  160. }
  161. }
  162. // WithIndex - the name of the index.
  163. func (f Mget) WithIndex(v string) func(*MgetRequest) {
  164. return func(r *MgetRequest) {
  165. r.Index = v
  166. }
  167. }
  168. // WithDocumentType - the type of the document.
  169. func (f Mget) WithDocumentType(v string) func(*MgetRequest) {
  170. return func(r *MgetRequest) {
  171. r.DocumentType = v
  172. }
  173. }
  174. // WithPreference - specify the node or shard the operation should be performed on (default: random).
  175. func (f Mget) WithPreference(v string) func(*MgetRequest) {
  176. return func(r *MgetRequest) {
  177. r.Preference = v
  178. }
  179. }
  180. // WithRealtime - specify whether to perform the operation in realtime or search mode.
  181. func (f Mget) WithRealtime(v bool) func(*MgetRequest) {
  182. return func(r *MgetRequest) {
  183. r.Realtime = &v
  184. }
  185. }
  186. // WithRefresh - refresh the shard containing the document before performing the operation.
  187. func (f Mget) WithRefresh(v bool) func(*MgetRequest) {
  188. return func(r *MgetRequest) {
  189. r.Refresh = &v
  190. }
  191. }
  192. // WithRouting - specific routing value.
  193. func (f Mget) WithRouting(v string) func(*MgetRequest) {
  194. return func(r *MgetRequest) {
  195. r.Routing = v
  196. }
  197. }
  198. // WithSource - true or false to return the _source field or not, or a list of fields to return.
  199. func (f Mget) WithSource(v ...string) func(*MgetRequest) {
  200. return func(r *MgetRequest) {
  201. r.Source = v
  202. }
  203. }
  204. // WithSourceExcludes - a list of fields to exclude from the returned _source field.
  205. func (f Mget) WithSourceExcludes(v ...string) func(*MgetRequest) {
  206. return func(r *MgetRequest) {
  207. r.SourceExcludes = v
  208. }
  209. }
  210. // WithSourceIncludes - a list of fields to extract and return from the _source field.
  211. func (f Mget) WithSourceIncludes(v ...string) func(*MgetRequest) {
  212. return func(r *MgetRequest) {
  213. r.SourceIncludes = v
  214. }
  215. }
  216. // WithStoredFields - a list of stored fields to return in the response.
  217. func (f Mget) WithStoredFields(v ...string) func(*MgetRequest) {
  218. return func(r *MgetRequest) {
  219. r.StoredFields = v
  220. }
  221. }
  222. // WithPretty makes the response body pretty-printed.
  223. func (f Mget) WithPretty() func(*MgetRequest) {
  224. return func(r *MgetRequest) {
  225. r.Pretty = true
  226. }
  227. }
  228. // WithHuman makes statistical values human-readable.
  229. func (f Mget) WithHuman() func(*MgetRequest) {
  230. return func(r *MgetRequest) {
  231. r.Human = true
  232. }
  233. }
  234. // WithErrorTrace includes the stack trace for errors in the response body.
  235. func (f Mget) WithErrorTrace() func(*MgetRequest) {
  236. return func(r *MgetRequest) {
  237. r.ErrorTrace = true
  238. }
  239. }
  240. // WithFilterPath filters the properties of the response body.
  241. func (f Mget) WithFilterPath(v ...string) func(*MgetRequest) {
  242. return func(r *MgetRequest) {
  243. r.FilterPath = v
  244. }
  245. }
  246. // WithHeader adds the headers to the HTTP request.
  247. func (f Mget) WithHeader(h map[string]string) func(*MgetRequest) {
  248. return func(r *MgetRequest) {
  249. if r.Header == nil {
  250. r.Header = make(http.Header)
  251. }
  252. for k, v := range h {
  253. r.Header.Add(k, v)
  254. }
  255. }
  256. }
  257. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  258. func (f Mget) WithOpaqueID(s string) func(*MgetRequest) {
  259. return func(r *MgetRequest) {
  260. if r.Header == nil {
  261. r.Header = make(http.Header)
  262. }
  263. r.Header.Set("X-Opaque-Id", s)
  264. }
  265. }