api.exists.go 7.8 KB

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