api.get_source.go 7.7 KB

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