api.reindex.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. "fmt"
  23. "io"
  24. "net/http"
  25. "strconv"
  26. "strings"
  27. "time"
  28. )
  29. func newReindexFunc(t Transport) Reindex {
  30. return func(body io.Reader, o ...func(*ReindexRequest)) (*Response, error) {
  31. var r = ReindexRequest{Body: body}
  32. for _, f := range o {
  33. f(&r)
  34. }
  35. return r.Do(r.ctx, t)
  36. }
  37. }
  38. // ----- API Definition -------------------------------------------------------
  39. // Reindex allows to copy documents from one index to another, optionally filtering the source
  40. // documents by a query, changing the destination index settings, or fetching the
  41. // documents from a remote cluster.
  42. //
  43. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-reindex.html.
  44. type Reindex func(body io.Reader, o ...func(*ReindexRequest)) (*Response, error)
  45. // ReindexRequest configures the Reindex API request.
  46. type ReindexRequest struct {
  47. Body io.Reader
  48. MaxDocs *int
  49. Refresh *bool
  50. RequestsPerSecond *int
  51. Scroll time.Duration
  52. Slices interface{}
  53. Timeout time.Duration
  54. WaitForActiveShards string
  55. WaitForCompletion *bool
  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 ReindexRequest) 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(len("/_reindex"))
  72. path.WriteString("/_reindex")
  73. params = make(map[string]string)
  74. if r.MaxDocs != nil {
  75. params["max_docs"] = strconv.FormatInt(int64(*r.MaxDocs), 10)
  76. }
  77. if r.Refresh != nil {
  78. params["refresh"] = strconv.FormatBool(*r.Refresh)
  79. }
  80. if r.RequestsPerSecond != nil {
  81. params["requests_per_second"] = strconv.FormatInt(int64(*r.RequestsPerSecond), 10)
  82. }
  83. if r.Scroll != 0 {
  84. params["scroll"] = formatDuration(r.Scroll)
  85. }
  86. if r.Slices != nil {
  87. params["slices"] = fmt.Sprintf("%v", r.Slices)
  88. }
  89. if r.Timeout != 0 {
  90. params["timeout"] = formatDuration(r.Timeout)
  91. }
  92. if r.WaitForActiveShards != "" {
  93. params["wait_for_active_shards"] = r.WaitForActiveShards
  94. }
  95. if r.WaitForCompletion != nil {
  96. params["wait_for_completion"] = strconv.FormatBool(*r.WaitForCompletion)
  97. }
  98. if r.Pretty {
  99. params["pretty"] = "true"
  100. }
  101. if r.Human {
  102. params["human"] = "true"
  103. }
  104. if r.ErrorTrace {
  105. params["error_trace"] = "true"
  106. }
  107. if len(r.FilterPath) > 0 {
  108. params["filter_path"] = strings.Join(r.FilterPath, ",")
  109. }
  110. req, err := newRequest(method, path.String(), r.Body)
  111. if err != nil {
  112. return nil, err
  113. }
  114. if len(params) > 0 {
  115. q := req.URL.Query()
  116. for k, v := range params {
  117. q.Set(k, v)
  118. }
  119. req.URL.RawQuery = q.Encode()
  120. }
  121. if len(r.Header) > 0 {
  122. if len(req.Header) == 0 {
  123. req.Header = r.Header
  124. } else {
  125. for k, vv := range r.Header {
  126. for _, v := range vv {
  127. req.Header.Add(k, v)
  128. }
  129. }
  130. }
  131. }
  132. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  133. req.Header[headerContentType] = headerContentTypeJSON
  134. }
  135. if ctx != nil {
  136. req = req.WithContext(ctx)
  137. }
  138. res, err := transport.Perform(req)
  139. if err != nil {
  140. return nil, err
  141. }
  142. response := Response{
  143. StatusCode: res.StatusCode,
  144. Body: res.Body,
  145. Header: res.Header,
  146. }
  147. return &response, nil
  148. }
  149. // WithContext sets the request context.
  150. func (f Reindex) WithContext(v context.Context) func(*ReindexRequest) {
  151. return func(r *ReindexRequest) {
  152. r.ctx = v
  153. }
  154. }
  155. // WithMaxDocs - maximum number of documents to process (default: all documents).
  156. func (f Reindex) WithMaxDocs(v int) func(*ReindexRequest) {
  157. return func(r *ReindexRequest) {
  158. r.MaxDocs = &v
  159. }
  160. }
  161. // WithRefresh - should the affected indexes be refreshed?.
  162. func (f Reindex) WithRefresh(v bool) func(*ReindexRequest) {
  163. return func(r *ReindexRequest) {
  164. r.Refresh = &v
  165. }
  166. }
  167. // WithRequestsPerSecond - the throttle to set on this request in sub-requests per second. -1 means no throttle..
  168. func (f Reindex) WithRequestsPerSecond(v int) func(*ReindexRequest) {
  169. return func(r *ReindexRequest) {
  170. r.RequestsPerSecond = &v
  171. }
  172. }
  173. // WithScroll - control how long to keep the search context alive.
  174. func (f Reindex) WithScroll(v time.Duration) func(*ReindexRequest) {
  175. return func(r *ReindexRequest) {
  176. r.Scroll = v
  177. }
  178. }
  179. // WithSlices - the number of slices this task should be divided into. defaults to 1, meaning the task isn't sliced into subtasks. can be set to `auto`..
  180. func (f Reindex) WithSlices(v interface{}) func(*ReindexRequest) {
  181. return func(r *ReindexRequest) {
  182. r.Slices = v
  183. }
  184. }
  185. // WithTimeout - time each individual bulk request should wait for shards that are unavailable..
  186. func (f Reindex) WithTimeout(v time.Duration) func(*ReindexRequest) {
  187. return func(r *ReindexRequest) {
  188. r.Timeout = v
  189. }
  190. }
  191. // WithWaitForActiveShards - sets the number of shard copies that must be active before proceeding with the reindex 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).
  192. func (f Reindex) WithWaitForActiveShards(v string) func(*ReindexRequest) {
  193. return func(r *ReindexRequest) {
  194. r.WaitForActiveShards = v
  195. }
  196. }
  197. // WithWaitForCompletion - should the request should block until the reindex is complete..
  198. func (f Reindex) WithWaitForCompletion(v bool) func(*ReindexRequest) {
  199. return func(r *ReindexRequest) {
  200. r.WaitForCompletion = &v
  201. }
  202. }
  203. // WithPretty makes the response body pretty-printed.
  204. func (f Reindex) WithPretty() func(*ReindexRequest) {
  205. return func(r *ReindexRequest) {
  206. r.Pretty = true
  207. }
  208. }
  209. // WithHuman makes statistical values human-readable.
  210. func (f Reindex) WithHuman() func(*ReindexRequest) {
  211. return func(r *ReindexRequest) {
  212. r.Human = true
  213. }
  214. }
  215. // WithErrorTrace includes the stack trace for errors in the response body.
  216. func (f Reindex) WithErrorTrace() func(*ReindexRequest) {
  217. return func(r *ReindexRequest) {
  218. r.ErrorTrace = true
  219. }
  220. }
  221. // WithFilterPath filters the properties of the response body.
  222. func (f Reindex) WithFilterPath(v ...string) func(*ReindexRequest) {
  223. return func(r *ReindexRequest) {
  224. r.FilterPath = v
  225. }
  226. }
  227. // WithHeader adds the headers to the HTTP request.
  228. func (f Reindex) WithHeader(h map[string]string) func(*ReindexRequest) {
  229. return func(r *ReindexRequest) {
  230. if r.Header == nil {
  231. r.Header = make(http.Header)
  232. }
  233. for k, v := range h {
  234. r.Header.Add(k, v)
  235. }
  236. }
  237. }
  238. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  239. func (f Reindex) WithOpaqueID(s string) func(*ReindexRequest) {
  240. return func(r *ReindexRequest) {
  241. if r.Header == nil {
  242. r.Header = make(http.Header)
  243. }
  244. r.Header.Set("X-Opaque-Id", s)
  245. }
  246. }