api.update.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 newUpdateFunc(t Transport) Update {
  29. return func(index string, id string, body io.Reader, o ...func(*UpdateRequest)) (*Response, error) {
  30. var r = UpdateRequest{Index: index, DocumentID: id, Body: body}
  31. for _, f := range o {
  32. f(&r)
  33. }
  34. return r.Do(r.ctx, t)
  35. }
  36. }
  37. // ----- API Definition -------------------------------------------------------
  38. // Update updates a document with a script or partial document.
  39. //
  40. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-update.html.
  41. type Update func(index string, id string, body io.Reader, o ...func(*UpdateRequest)) (*Response, error)
  42. // UpdateRequest configures the Update API request.
  43. type UpdateRequest struct {
  44. Index string
  45. DocumentType string
  46. DocumentID string
  47. Body io.Reader
  48. IfPrimaryTerm *int
  49. IfSeqNo *int
  50. Lang string
  51. Refresh string
  52. RequireAlias *bool
  53. RetryOnConflict *int
  54. Routing string
  55. Source []string
  56. SourceExcludes []string
  57. SourceIncludes []string
  58. Timeout time.Duration
  59. WaitForActiveShards string
  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 UpdateRequest) 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. if r.DocumentType == "" {
  76. r.DocumentType = "_doc"
  77. }
  78. path.Grow(1 + len(r.Index) + 1 + len(r.DocumentType) + 1 + len(r.DocumentID) + 1 + len("_update"))
  79. path.WriteString("/")
  80. path.WriteString(r.Index)
  81. if r.DocumentType != "" {
  82. path.WriteString("/")
  83. path.WriteString(r.DocumentType)
  84. }
  85. path.WriteString("/")
  86. path.WriteString(r.DocumentID)
  87. path.WriteString("/")
  88. path.WriteString("_update")
  89. params = make(map[string]string)
  90. if r.IfPrimaryTerm != nil {
  91. params["if_primary_term"] = strconv.FormatInt(int64(*r.IfPrimaryTerm), 10)
  92. }
  93. if r.IfSeqNo != nil {
  94. params["if_seq_no"] = strconv.FormatInt(int64(*r.IfSeqNo), 10)
  95. }
  96. if r.Lang != "" {
  97. params["lang"] = r.Lang
  98. }
  99. if r.Refresh != "" {
  100. params["refresh"] = r.Refresh
  101. }
  102. if r.RequireAlias != nil {
  103. params["require_alias"] = strconv.FormatBool(*r.RequireAlias)
  104. }
  105. if r.RetryOnConflict != nil {
  106. params["retry_on_conflict"] = strconv.FormatInt(int64(*r.RetryOnConflict), 10)
  107. }
  108. if r.Routing != "" {
  109. params["routing"] = r.Routing
  110. }
  111. if len(r.Source) > 0 {
  112. params["_source"] = strings.Join(r.Source, ",")
  113. }
  114. if len(r.SourceExcludes) > 0 {
  115. params["_source_excludes"] = strings.Join(r.SourceExcludes, ",")
  116. }
  117. if len(r.SourceIncludes) > 0 {
  118. params["_source_includes"] = strings.Join(r.SourceIncludes, ",")
  119. }
  120. if r.Timeout != 0 {
  121. params["timeout"] = formatDuration(r.Timeout)
  122. }
  123. if r.WaitForActiveShards != "" {
  124. params["wait_for_active_shards"] = r.WaitForActiveShards
  125. }
  126. if r.Pretty {
  127. params["pretty"] = "true"
  128. }
  129. if r.Human {
  130. params["human"] = "true"
  131. }
  132. if r.ErrorTrace {
  133. params["error_trace"] = "true"
  134. }
  135. if len(r.FilterPath) > 0 {
  136. params["filter_path"] = strings.Join(r.FilterPath, ",")
  137. }
  138. req, err := newRequest(method, path.String(), r.Body)
  139. if err != nil {
  140. return nil, err
  141. }
  142. if len(params) > 0 {
  143. q := req.URL.Query()
  144. for k, v := range params {
  145. q.Set(k, v)
  146. }
  147. req.URL.RawQuery = q.Encode()
  148. }
  149. if len(r.Header) > 0 {
  150. if len(req.Header) == 0 {
  151. req.Header = r.Header
  152. } else {
  153. for k, vv := range r.Header {
  154. for _, v := range vv {
  155. req.Header.Add(k, v)
  156. }
  157. }
  158. }
  159. }
  160. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  161. req.Header[headerContentType] = headerContentTypeJSON
  162. }
  163. if ctx != nil {
  164. req = req.WithContext(ctx)
  165. }
  166. res, err := transport.Perform(req)
  167. if err != nil {
  168. return nil, err
  169. }
  170. response := Response{
  171. StatusCode: res.StatusCode,
  172. Body: res.Body,
  173. Header: res.Header,
  174. }
  175. return &response, nil
  176. }
  177. // WithContext sets the request context.
  178. func (f Update) WithContext(v context.Context) func(*UpdateRequest) {
  179. return func(r *UpdateRequest) {
  180. r.ctx = v
  181. }
  182. }
  183. // WithDocumentType - the type of the document.
  184. func (f Update) WithDocumentType(v string) func(*UpdateRequest) {
  185. return func(r *UpdateRequest) {
  186. r.DocumentType = v
  187. }
  188. }
  189. // WithIfPrimaryTerm - only perform the update operation if the last operation that has changed the document has the specified primary term.
  190. func (f Update) WithIfPrimaryTerm(v int) func(*UpdateRequest) {
  191. return func(r *UpdateRequest) {
  192. r.IfPrimaryTerm = &v
  193. }
  194. }
  195. // WithIfSeqNo - only perform the update operation if the last operation that has changed the document has the specified sequence number.
  196. func (f Update) WithIfSeqNo(v int) func(*UpdateRequest) {
  197. return func(r *UpdateRequest) {
  198. r.IfSeqNo = &v
  199. }
  200. }
  201. // WithLang - the script language (default: painless).
  202. func (f Update) WithLang(v string) func(*UpdateRequest) {
  203. return func(r *UpdateRequest) {
  204. r.Lang = v
  205. }
  206. }
  207. // WithRefresh - if `true` then refresh the affected shards to make this operation visible to search, if `wait_for` then wait for a refresh to make this operation visible to search, if `false` (the default) then do nothing with refreshes..
  208. func (f Update) WithRefresh(v string) func(*UpdateRequest) {
  209. return func(r *UpdateRequest) {
  210. r.Refresh = v
  211. }
  212. }
  213. // WithRequireAlias - when true, requires destination is an alias. default is false.
  214. func (f Update) WithRequireAlias(v bool) func(*UpdateRequest) {
  215. return func(r *UpdateRequest) {
  216. r.RequireAlias = &v
  217. }
  218. }
  219. // WithRetryOnConflict - specify how many times should the operation be retried when a conflict occurs (default: 0).
  220. func (f Update) WithRetryOnConflict(v int) func(*UpdateRequest) {
  221. return func(r *UpdateRequest) {
  222. r.RetryOnConflict = &v
  223. }
  224. }
  225. // WithRouting - specific routing value.
  226. func (f Update) WithRouting(v string) func(*UpdateRequest) {
  227. return func(r *UpdateRequest) {
  228. r.Routing = v
  229. }
  230. }
  231. // WithSource - true or false to return the _source field or not, or a list of fields to return.
  232. func (f Update) WithSource(v ...string) func(*UpdateRequest) {
  233. return func(r *UpdateRequest) {
  234. r.Source = v
  235. }
  236. }
  237. // WithSourceExcludes - a list of fields to exclude from the returned _source field.
  238. func (f Update) WithSourceExcludes(v ...string) func(*UpdateRequest) {
  239. return func(r *UpdateRequest) {
  240. r.SourceExcludes = v
  241. }
  242. }
  243. // WithSourceIncludes - a list of fields to extract and return from the _source field.
  244. func (f Update) WithSourceIncludes(v ...string) func(*UpdateRequest) {
  245. return func(r *UpdateRequest) {
  246. r.SourceIncludes = v
  247. }
  248. }
  249. // WithTimeout - explicit operation timeout.
  250. func (f Update) WithTimeout(v time.Duration) func(*UpdateRequest) {
  251. return func(r *UpdateRequest) {
  252. r.Timeout = v
  253. }
  254. }
  255. // WithWaitForActiveShards - sets the number of shard copies that must be active before proceeding with the update operation. defaults to 1, meaning the primary shard only. set to `all` for all shard copies, otherwise set to any non-negative value less than or equal to the total number of copies for the shard (number of replicas + 1).
  256. func (f Update) WithWaitForActiveShards(v string) func(*UpdateRequest) {
  257. return func(r *UpdateRequest) {
  258. r.WaitForActiveShards = v
  259. }
  260. }
  261. // WithPretty makes the response body pretty-printed.
  262. func (f Update) WithPretty() func(*UpdateRequest) {
  263. return func(r *UpdateRequest) {
  264. r.Pretty = true
  265. }
  266. }
  267. // WithHuman makes statistical values human-readable.
  268. func (f Update) WithHuman() func(*UpdateRequest) {
  269. return func(r *UpdateRequest) {
  270. r.Human = true
  271. }
  272. }
  273. // WithErrorTrace includes the stack trace for errors in the response body.
  274. func (f Update) WithErrorTrace() func(*UpdateRequest) {
  275. return func(r *UpdateRequest) {
  276. r.ErrorTrace = true
  277. }
  278. }
  279. // WithFilterPath filters the properties of the response body.
  280. func (f Update) WithFilterPath(v ...string) func(*UpdateRequest) {
  281. return func(r *UpdateRequest) {
  282. r.FilterPath = v
  283. }
  284. }
  285. // WithHeader adds the headers to the HTTP request.
  286. func (f Update) WithHeader(h map[string]string) func(*UpdateRequest) {
  287. return func(r *UpdateRequest) {
  288. if r.Header == nil {
  289. r.Header = make(http.Header)
  290. }
  291. for k, v := range h {
  292. r.Header.Add(k, v)
  293. }
  294. }
  295. }
  296. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  297. func (f Update) WithOpaqueID(s string) func(*UpdateRequest) {
  298. return func(r *UpdateRequest) {
  299. if r.Header == nil {
  300. r.Header = make(http.Header)
  301. }
  302. r.Header.Set("X-Opaque-Id", s)
  303. }
  304. }