indices_close.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. "fmt"
  8. "net/url"
  9. "gopkg.in/olivere/elastic.v5/uritemplates"
  10. )
  11. // IndicesCloseService closes an index.
  12. //
  13. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-open-close.html
  14. // for details.
  15. type IndicesCloseService struct {
  16. client *Client
  17. pretty bool
  18. index string
  19. timeout string
  20. masterTimeout string
  21. ignoreUnavailable *bool
  22. allowNoIndices *bool
  23. expandWildcards string
  24. }
  25. // NewIndicesCloseService creates and initializes a new IndicesCloseService.
  26. func NewIndicesCloseService(client *Client) *IndicesCloseService {
  27. return &IndicesCloseService{client: client}
  28. }
  29. // Index is the name of the index to close.
  30. func (s *IndicesCloseService) Index(index string) *IndicesCloseService {
  31. s.index = index
  32. return s
  33. }
  34. // Timeout is an explicit operation timeout.
  35. func (s *IndicesCloseService) Timeout(timeout string) *IndicesCloseService {
  36. s.timeout = timeout
  37. return s
  38. }
  39. // MasterTimeout specifies the timeout for connection to master.
  40. func (s *IndicesCloseService) MasterTimeout(masterTimeout string) *IndicesCloseService {
  41. s.masterTimeout = masterTimeout
  42. return s
  43. }
  44. // IgnoreUnavailable indicates whether specified concrete indices should be
  45. // ignored when unavailable (missing or closed).
  46. func (s *IndicesCloseService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesCloseService {
  47. s.ignoreUnavailable = &ignoreUnavailable
  48. return s
  49. }
  50. // AllowNoIndices indicates whether to ignore if a wildcard indices
  51. // expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified).
  52. func (s *IndicesCloseService) AllowNoIndices(allowNoIndices bool) *IndicesCloseService {
  53. s.allowNoIndices = &allowNoIndices
  54. return s
  55. }
  56. // ExpandWildcards indicates whether to expand wildcard expression to
  57. // concrete indices that are open, closed or both.
  58. func (s *IndicesCloseService) ExpandWildcards(expandWildcards string) *IndicesCloseService {
  59. s.expandWildcards = expandWildcards
  60. return s
  61. }
  62. // Pretty indicates that the JSON response be indented and human readable.
  63. func (s *IndicesCloseService) Pretty(pretty bool) *IndicesCloseService {
  64. s.pretty = pretty
  65. return s
  66. }
  67. // buildURL builds the URL for the operation.
  68. func (s *IndicesCloseService) buildURL() (string, url.Values, error) {
  69. // Build URL
  70. path, err := uritemplates.Expand("/{index}/_close", map[string]string{
  71. "index": s.index,
  72. })
  73. if err != nil {
  74. return "", url.Values{}, err
  75. }
  76. // Add query string parameters
  77. params := url.Values{}
  78. if s.allowNoIndices != nil {
  79. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  80. }
  81. if s.expandWildcards != "" {
  82. params.Set("expand_wildcards", s.expandWildcards)
  83. }
  84. if s.timeout != "" {
  85. params.Set("timeout", s.timeout)
  86. }
  87. if s.masterTimeout != "" {
  88. params.Set("master_timeout", s.masterTimeout)
  89. }
  90. if s.ignoreUnavailable != nil {
  91. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  92. }
  93. return path, params, nil
  94. }
  95. // Validate checks if the operation is valid.
  96. func (s *IndicesCloseService) Validate() error {
  97. var invalid []string
  98. if s.index == "" {
  99. invalid = append(invalid, "Index")
  100. }
  101. if len(invalid) > 0 {
  102. return fmt.Errorf("missing required fields: %v", invalid)
  103. }
  104. return nil
  105. }
  106. // Do executes the operation.
  107. func (s *IndicesCloseService) Do(ctx context.Context) (*IndicesCloseResponse, error) {
  108. // Check pre-conditions
  109. if err := s.Validate(); err != nil {
  110. return nil, err
  111. }
  112. // Get URL for request
  113. path, params, err := s.buildURL()
  114. if err != nil {
  115. return nil, err
  116. }
  117. // Get HTTP response
  118. res, err := s.client.PerformRequest(ctx, "POST", path, params, nil)
  119. if err != nil {
  120. return nil, err
  121. }
  122. // Return operation response
  123. ret := new(IndicesCloseResponse)
  124. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  125. return nil, err
  126. }
  127. return ret, nil
  128. }
  129. // IndicesCloseResponse is the response of IndicesCloseService.Do.
  130. type IndicesCloseResponse struct {
  131. Acknowledged bool `json:"acknowledged"`
  132. }