api.bulk.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 newBulkFunc(t Transport) Bulk {
  29. return func(body io.Reader, o ...func(*BulkRequest)) (*Response, error) {
  30. var r = BulkRequest{Body: body}
  31. for _, f := range o {
  32. f(&r)
  33. }
  34. return r.Do(r.ctx, t)
  35. }
  36. }
  37. // ----- API Definition -------------------------------------------------------
  38. // Bulk allows to perform multiple index/update/delete operations in a single request.
  39. //
  40. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-bulk.html.
  41. type Bulk func(body io.Reader, o ...func(*BulkRequest)) (*Response, error)
  42. // BulkRequest configures the Bulk API request.
  43. type BulkRequest struct {
  44. Index string
  45. DocumentType string
  46. Body io.Reader
  47. Pipeline string
  48. Refresh string
  49. RequireAlias *bool
  50. Routing string
  51. Source []string
  52. SourceExcludes []string
  53. SourceIncludes []string
  54. Timeout time.Duration
  55. WaitForActiveShards string
  56. Pretty bool
  57. Human bool
  58. ErrorTrace bool
  59. FilterPath []string
  60. Header http.Header
  61. ctx context.Context
  62. }
  63. // Do executes the request and returns response or error.
  64. func (r BulkRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  65. var (
  66. method string
  67. path strings.Builder
  68. params map[string]string
  69. )
  70. method = "POST"
  71. path.Grow(1 + len(r.Index) + 1 + len(r.DocumentType) + 1 + len("_bulk"))
  72. if r.Index != "" {
  73. path.WriteString("/")
  74. path.WriteString(r.Index)
  75. }
  76. if r.DocumentType != "" {
  77. path.WriteString("/")
  78. path.WriteString(r.DocumentType)
  79. }
  80. path.WriteString("/")
  81. path.WriteString("_bulk")
  82. params = make(map[string]string)
  83. if r.Pipeline != "" {
  84. params["pipeline"] = r.Pipeline
  85. }
  86. if r.Refresh != "" {
  87. params["refresh"] = r.Refresh
  88. }
  89. if r.RequireAlias != nil {
  90. params["require_alias"] = strconv.FormatBool(*r.RequireAlias)
  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 r.Timeout != 0 {
  105. params["timeout"] = formatDuration(r.Timeout)
  106. }
  107. if r.DocumentType != "" {
  108. params["type"] = r.DocumentType
  109. }
  110. if r.WaitForActiveShards != "" {
  111. params["wait_for_active_shards"] = r.WaitForActiveShards
  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(), r.Body)
  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 r.Body != nil && req.Header.Get(headerContentType) == "" {
  148. req.Header[headerContentType] = headerContentTypeJSON
  149. }
  150. if ctx != nil {
  151. req = req.WithContext(ctx)
  152. }
  153. res, err := transport.Perform(req)
  154. if err != nil {
  155. return nil, err
  156. }
  157. response := Response{
  158. StatusCode: res.StatusCode,
  159. Body: res.Body,
  160. Header: res.Header,
  161. }
  162. return &response, nil
  163. }
  164. // WithContext sets the request context.
  165. func (f Bulk) WithContext(v context.Context) func(*BulkRequest) {
  166. return func(r *BulkRequest) {
  167. r.ctx = v
  168. }
  169. }
  170. // WithIndex - default index for items which don't provide one.
  171. func (f Bulk) WithIndex(v string) func(*BulkRequest) {
  172. return func(r *BulkRequest) {
  173. r.Index = v
  174. }
  175. }
  176. // WithDocumentType - default document type for items which don't provide one.
  177. func (f Bulk) WithDocumentType(v string) func(*BulkRequest) {
  178. return func(r *BulkRequest) {
  179. r.DocumentType = v
  180. }
  181. }
  182. // WithPipeline - the pipeline ID to preprocess incoming documents with.
  183. func (f Bulk) WithPipeline(v string) func(*BulkRequest) {
  184. return func(r *BulkRequest) {
  185. r.Pipeline = v
  186. }
  187. }
  188. // 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..
  189. func (f Bulk) WithRefresh(v string) func(*BulkRequest) {
  190. return func(r *BulkRequest) {
  191. r.Refresh = v
  192. }
  193. }
  194. // WithRequireAlias - sets require_alias for all incoming documents. defaults to unset (false).
  195. func (f Bulk) WithRequireAlias(v bool) func(*BulkRequest) {
  196. return func(r *BulkRequest) {
  197. r.RequireAlias = &v
  198. }
  199. }
  200. // WithRouting - specific routing value.
  201. func (f Bulk) WithRouting(v string) func(*BulkRequest) {
  202. return func(r *BulkRequest) {
  203. r.Routing = v
  204. }
  205. }
  206. // WithSource - true or false to return the _source field or not, or default list of fields to return, can be overridden on each sub-request.
  207. func (f Bulk) WithSource(v ...string) func(*BulkRequest) {
  208. return func(r *BulkRequest) {
  209. r.Source = v
  210. }
  211. }
  212. // WithSourceExcludes - default list of fields to exclude from the returned _source field, can be overridden on each sub-request.
  213. func (f Bulk) WithSourceExcludes(v ...string) func(*BulkRequest) {
  214. return func(r *BulkRequest) {
  215. r.SourceExcludes = v
  216. }
  217. }
  218. // WithSourceIncludes - default list of fields to extract and return from the _source field, can be overridden on each sub-request.
  219. func (f Bulk) WithSourceIncludes(v ...string) func(*BulkRequest) {
  220. return func(r *BulkRequest) {
  221. r.SourceIncludes = v
  222. }
  223. }
  224. // WithTimeout - explicit operation timeout.
  225. func (f Bulk) WithTimeout(v time.Duration) func(*BulkRequest) {
  226. return func(r *BulkRequest) {
  227. r.Timeout = v
  228. }
  229. }
  230. // WithWaitForActiveShards - sets the number of shard copies that must be active before proceeding with the bulk 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).
  231. func (f Bulk) WithWaitForActiveShards(v string) func(*BulkRequest) {
  232. return func(r *BulkRequest) {
  233. r.WaitForActiveShards = v
  234. }
  235. }
  236. // WithPretty makes the response body pretty-printed.
  237. func (f Bulk) WithPretty() func(*BulkRequest) {
  238. return func(r *BulkRequest) {
  239. r.Pretty = true
  240. }
  241. }
  242. // WithHuman makes statistical values human-readable.
  243. func (f Bulk) WithHuman() func(*BulkRequest) {
  244. return func(r *BulkRequest) {
  245. r.Human = true
  246. }
  247. }
  248. // WithErrorTrace includes the stack trace for errors in the response body.
  249. func (f Bulk) WithErrorTrace() func(*BulkRequest) {
  250. return func(r *BulkRequest) {
  251. r.ErrorTrace = true
  252. }
  253. }
  254. // WithFilterPath filters the properties of the response body.
  255. func (f Bulk) WithFilterPath(v ...string) func(*BulkRequest) {
  256. return func(r *BulkRequest) {
  257. r.FilterPath = v
  258. }
  259. }
  260. // WithHeader adds the headers to the HTTP request.
  261. func (f Bulk) WithHeader(h map[string]string) func(*BulkRequest) {
  262. return func(r *BulkRequest) {
  263. if r.Header == nil {
  264. r.Header = make(http.Header)
  265. }
  266. for k, v := range h {
  267. r.Header.Add(k, v)
  268. }
  269. }
  270. }
  271. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  272. func (f Bulk) WithOpaqueID(s string) func(*BulkRequest) {
  273. return func(r *BulkRequest) {
  274. if r.Header == nil {
  275. r.Header = make(http.Header)
  276. }
  277. r.Header.Set("X-Opaque-Id", s)
  278. }
  279. }