api.indices.split.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 newIndicesSplitFunc(t Transport) IndicesSplit {
  29. return func(index string, target string, o ...func(*IndicesSplitRequest)) (*Response, error) {
  30. var r = IndicesSplitRequest{Index: index, Target: target}
  31. for _, f := range o {
  32. f(&r)
  33. }
  34. return r.Do(r.ctx, t)
  35. }
  36. }
  37. // ----- API Definition -------------------------------------------------------
  38. // IndicesSplit allows you to split an existing index into a new index with more primary shards.
  39. //
  40. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-split-index.html.
  41. type IndicesSplit func(index string, target string, o ...func(*IndicesSplitRequest)) (*Response, error)
  42. // IndicesSplitRequest configures the Indices Split API request.
  43. type IndicesSplitRequest struct {
  44. Index string
  45. Body io.Reader
  46. Target string
  47. CopySettings *bool
  48. MasterTimeout time.Duration
  49. Timeout time.Duration
  50. WaitForActiveShards string
  51. Pretty bool
  52. Human bool
  53. ErrorTrace bool
  54. FilterPath []string
  55. Header http.Header
  56. ctx context.Context
  57. }
  58. // Do executes the request and returns response or error.
  59. func (r IndicesSplitRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  60. var (
  61. method string
  62. path strings.Builder
  63. params map[string]string
  64. )
  65. method = "PUT"
  66. path.Grow(1 + len(r.Index) + 1 + len("_split") + 1 + len(r.Target))
  67. path.WriteString("/")
  68. path.WriteString(r.Index)
  69. path.WriteString("/")
  70. path.WriteString("_split")
  71. path.WriteString("/")
  72. path.WriteString(r.Target)
  73. params = make(map[string]string)
  74. if r.CopySettings != nil {
  75. params["copy_settings"] = strconv.FormatBool(*r.CopySettings)
  76. }
  77. if r.MasterTimeout != 0 {
  78. params["master_timeout"] = formatDuration(r.MasterTimeout)
  79. }
  80. if r.Timeout != 0 {
  81. params["timeout"] = formatDuration(r.Timeout)
  82. }
  83. if r.WaitForActiveShards != "" {
  84. params["wait_for_active_shards"] = r.WaitForActiveShards
  85. }
  86. if r.Pretty {
  87. params["pretty"] = "true"
  88. }
  89. if r.Human {
  90. params["human"] = "true"
  91. }
  92. if r.ErrorTrace {
  93. params["error_trace"] = "true"
  94. }
  95. if len(r.FilterPath) > 0 {
  96. params["filter_path"] = strings.Join(r.FilterPath, ",")
  97. }
  98. req, err := newRequest(method, path.String(), r.Body)
  99. if err != nil {
  100. return nil, err
  101. }
  102. if len(params) > 0 {
  103. q := req.URL.Query()
  104. for k, v := range params {
  105. q.Set(k, v)
  106. }
  107. req.URL.RawQuery = q.Encode()
  108. }
  109. if len(r.Header) > 0 {
  110. if len(req.Header) == 0 {
  111. req.Header = r.Header
  112. } else {
  113. for k, vv := range r.Header {
  114. for _, v := range vv {
  115. req.Header.Add(k, v)
  116. }
  117. }
  118. }
  119. }
  120. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  121. req.Header[headerContentType] = headerContentTypeJSON
  122. }
  123. if ctx != nil {
  124. req = req.WithContext(ctx)
  125. }
  126. res, err := transport.Perform(req)
  127. if err != nil {
  128. return nil, err
  129. }
  130. response := Response{
  131. StatusCode: res.StatusCode,
  132. Body: res.Body,
  133. Header: res.Header,
  134. }
  135. return &response, nil
  136. }
  137. // WithContext sets the request context.
  138. func (f IndicesSplit) WithContext(v context.Context) func(*IndicesSplitRequest) {
  139. return func(r *IndicesSplitRequest) {
  140. r.ctx = v
  141. }
  142. }
  143. // WithBody - The configuration for the target index (`settings` and `aliases`).
  144. func (f IndicesSplit) WithBody(v io.Reader) func(*IndicesSplitRequest) {
  145. return func(r *IndicesSplitRequest) {
  146. r.Body = v
  147. }
  148. }
  149. // WithCopySettings - whether or not to copy settings from the source index (defaults to false).
  150. func (f IndicesSplit) WithCopySettings(v bool) func(*IndicesSplitRequest) {
  151. return func(r *IndicesSplitRequest) {
  152. r.CopySettings = &v
  153. }
  154. }
  155. // WithMasterTimeout - specify timeout for connection to master.
  156. func (f IndicesSplit) WithMasterTimeout(v time.Duration) func(*IndicesSplitRequest) {
  157. return func(r *IndicesSplitRequest) {
  158. r.MasterTimeout = v
  159. }
  160. }
  161. // WithTimeout - explicit operation timeout.
  162. func (f IndicesSplit) WithTimeout(v time.Duration) func(*IndicesSplitRequest) {
  163. return func(r *IndicesSplitRequest) {
  164. r.Timeout = v
  165. }
  166. }
  167. // WithWaitForActiveShards - set the number of active shards to wait for on the shrunken index before the operation returns..
  168. func (f IndicesSplit) WithWaitForActiveShards(v string) func(*IndicesSplitRequest) {
  169. return func(r *IndicesSplitRequest) {
  170. r.WaitForActiveShards = v
  171. }
  172. }
  173. // WithPretty makes the response body pretty-printed.
  174. func (f IndicesSplit) WithPretty() func(*IndicesSplitRequest) {
  175. return func(r *IndicesSplitRequest) {
  176. r.Pretty = true
  177. }
  178. }
  179. // WithHuman makes statistical values human-readable.
  180. func (f IndicesSplit) WithHuman() func(*IndicesSplitRequest) {
  181. return func(r *IndicesSplitRequest) {
  182. r.Human = true
  183. }
  184. }
  185. // WithErrorTrace includes the stack trace for errors in the response body.
  186. func (f IndicesSplit) WithErrorTrace() func(*IndicesSplitRequest) {
  187. return func(r *IndicesSplitRequest) {
  188. r.ErrorTrace = true
  189. }
  190. }
  191. // WithFilterPath filters the properties of the response body.
  192. func (f IndicesSplit) WithFilterPath(v ...string) func(*IndicesSplitRequest) {
  193. return func(r *IndicesSplitRequest) {
  194. r.FilterPath = v
  195. }
  196. }
  197. // WithHeader adds the headers to the HTTP request.
  198. func (f IndicesSplit) WithHeader(h map[string]string) func(*IndicesSplitRequest) {
  199. return func(r *IndicesSplitRequest) {
  200. if r.Header == nil {
  201. r.Header = make(http.Header)
  202. }
  203. for k, v := range h {
  204. r.Header.Add(k, v)
  205. }
  206. }
  207. }
  208. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  209. func (f IndicesSplit) WithOpaqueID(s string) func(*IndicesSplitRequest) {
  210. return func(r *IndicesSplitRequest) {
  211. if r.Header == nil {
  212. r.Header = make(http.Header)
  213. }
  214. r.Header.Set("X-Opaque-Id", s)
  215. }
  216. }