indices_shrink.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "context"
  7. "encoding/json"
  8. "fmt"
  9. "net/url"
  10. "gopkg.in/olivere/elastic.v5/uritemplates"
  11. )
  12. // IndicesShrinkService allows you to shrink an existing index into a
  13. // new index with fewer primary shards.
  14. //
  15. // For further details, see
  16. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-shrink-index.html.
  17. type IndicesShrinkService struct {
  18. client *Client
  19. pretty bool
  20. source string
  21. target string
  22. masterTimeout string
  23. timeout string
  24. waitForActiveShards string
  25. bodyJson interface{}
  26. bodyString string
  27. }
  28. // NewIndicesShrinkService creates a new IndicesShrinkService.
  29. func NewIndicesShrinkService(client *Client) *IndicesShrinkService {
  30. return &IndicesShrinkService{
  31. client: client,
  32. }
  33. }
  34. // Source is the name of the source index to shrink.
  35. func (s *IndicesShrinkService) Source(source string) *IndicesShrinkService {
  36. s.source = source
  37. return s
  38. }
  39. // Target is the name of the target index to shrink into.
  40. func (s *IndicesShrinkService) Target(target string) *IndicesShrinkService {
  41. s.target = target
  42. return s
  43. }
  44. // MasterTimeout specifies the timeout for connection to master.
  45. func (s *IndicesShrinkService) MasterTimeout(masterTimeout string) *IndicesShrinkService {
  46. s.masterTimeout = masterTimeout
  47. return s
  48. }
  49. // Timeout is an explicit operation timeout.
  50. func (s *IndicesShrinkService) Timeout(timeout string) *IndicesShrinkService {
  51. s.timeout = timeout
  52. return s
  53. }
  54. // WaitForActiveShards sets the number of active shards to wait for on
  55. // the shrunken index before the operation returns.
  56. func (s *IndicesShrinkService) WaitForActiveShards(waitForActiveShards string) *IndicesShrinkService {
  57. s.waitForActiveShards = waitForActiveShards
  58. return s
  59. }
  60. // Pretty indicates that the JSON response be indented and human readable.
  61. func (s *IndicesShrinkService) Pretty(pretty bool) *IndicesShrinkService {
  62. s.pretty = pretty
  63. return s
  64. }
  65. // BodyJson is the configuration for the target index (`settings` and `aliases`)
  66. // defined as a JSON-serializable instance to be sent as the request body.
  67. func (s *IndicesShrinkService) BodyJson(body interface{}) *IndicesShrinkService {
  68. s.bodyJson = body
  69. return s
  70. }
  71. // BodyString is the configuration for the target index (`settings` and `aliases`)
  72. // defined as a string to send as the request body.
  73. func (s *IndicesShrinkService) BodyString(body string) *IndicesShrinkService {
  74. s.bodyString = body
  75. return s
  76. }
  77. // buildURL builds the URL for the operation.
  78. func (s *IndicesShrinkService) buildURL() (string, url.Values, error) {
  79. // Build URL
  80. path, err := uritemplates.Expand("/{source}/_shrink/{target}", map[string]string{
  81. "source": s.source,
  82. "target": s.target,
  83. })
  84. if err != nil {
  85. return "", url.Values{}, err
  86. }
  87. // Add query string parameters
  88. params := url.Values{}
  89. if s.pretty {
  90. params.Set("pretty", "1")
  91. }
  92. if s.masterTimeout != "" {
  93. params.Set("master_timeout", s.masterTimeout)
  94. }
  95. if s.timeout != "" {
  96. params.Set("timeout", s.timeout)
  97. }
  98. if s.waitForActiveShards != "" {
  99. params.Set("wait_for_active_shards", s.waitForActiveShards)
  100. }
  101. return path, params, nil
  102. }
  103. // Validate checks if the operation is valid.
  104. func (s *IndicesShrinkService) Validate() error {
  105. var invalid []string
  106. if s.source == "" {
  107. invalid = append(invalid, "Source")
  108. }
  109. if s.target == "" {
  110. invalid = append(invalid, "Target")
  111. }
  112. if len(invalid) > 0 {
  113. return fmt.Errorf("missing required fields: %v", invalid)
  114. }
  115. return nil
  116. }
  117. // Do executes the operation.
  118. func (s *IndicesShrinkService) Do(ctx context.Context) (*IndicesShrinkResponse, error) {
  119. // Check pre-conditions
  120. if err := s.Validate(); err != nil {
  121. return nil, err
  122. }
  123. // Get URL for request
  124. path, params, err := s.buildURL()
  125. if err != nil {
  126. return nil, err
  127. }
  128. // Setup HTTP request body
  129. var body interface{}
  130. if s.bodyJson != nil {
  131. body = s.bodyJson
  132. } else if s.bodyString != "" {
  133. body = s.bodyString
  134. }
  135. // Get HTTP response
  136. res, err := s.client.PerformRequest(ctx, "POST", path, params, body)
  137. if err != nil {
  138. return nil, err
  139. }
  140. // Return operation response
  141. ret := new(IndicesShrinkResponse)
  142. if err := json.Unmarshal(res.Body, ret); err != nil {
  143. return nil, err
  144. }
  145. return ret, nil
  146. }
  147. // IndicesShrinkResponse is the response of IndicesShrinkService.Do.
  148. type IndicesShrinkResponse struct {
  149. Acknowledged bool `json:"acknowledged"`
  150. ShardsAcknowledged bool `json:"shards_acknowledged"`
  151. }