index_open.go 3.8 KB

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