indices_open.go 4.2 KB

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