index_close.go 3.9 KB

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