indices_exists.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/http"
  9. "net/url"
  10. "strings"
  11. "gopkg.in/olivere/elastic.v5/uritemplates"
  12. )
  13. // IndicesExistsService checks if an index or indices exist or not.
  14. //
  15. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-exists.html
  16. // for details.
  17. type IndicesExistsService struct {
  18. client *Client
  19. pretty bool
  20. index []string
  21. ignoreUnavailable *bool
  22. allowNoIndices *bool
  23. expandWildcards string
  24. local *bool
  25. }
  26. // NewIndicesExistsService creates and initializes a new IndicesExistsService.
  27. func NewIndicesExistsService(client *Client) *IndicesExistsService {
  28. return &IndicesExistsService{
  29. client: client,
  30. index: make([]string, 0),
  31. }
  32. }
  33. // Index is a list of one or more indices to check.
  34. func (s *IndicesExistsService) Index(index []string) *IndicesExistsService {
  35. s.index = index
  36. return s
  37. }
  38. // AllowNoIndices indicates whether to ignore if a wildcard indices expression
  39. // resolves into no concrete indices. (This includes `_all` string or
  40. // when no indices have been specified).
  41. func (s *IndicesExistsService) AllowNoIndices(allowNoIndices bool) *IndicesExistsService {
  42. s.allowNoIndices = &allowNoIndices
  43. return s
  44. }
  45. // ExpandWildcards indicates whether to expand wildcard expression to
  46. // concrete indices that are open, closed or both.
  47. func (s *IndicesExistsService) ExpandWildcards(expandWildcards string) *IndicesExistsService {
  48. s.expandWildcards = expandWildcards
  49. return s
  50. }
  51. // Local, when set, returns local information and does not retrieve the state
  52. // from master node (default: false).
  53. func (s *IndicesExistsService) Local(local bool) *IndicesExistsService {
  54. s.local = &local
  55. return s
  56. }
  57. // IgnoreUnavailable indicates whether specified concrete indices should be
  58. // ignored when unavailable (missing or closed).
  59. func (s *IndicesExistsService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesExistsService {
  60. s.ignoreUnavailable = &ignoreUnavailable
  61. return s
  62. }
  63. // Pretty indicates that the JSON response be indented and human readable.
  64. func (s *IndicesExistsService) Pretty(pretty bool) *IndicesExistsService {
  65. s.pretty = pretty
  66. return s
  67. }
  68. // buildURL builds the URL for the operation.
  69. func (s *IndicesExistsService) buildURL() (string, url.Values, error) {
  70. // Build URL
  71. path, err := uritemplates.Expand("/{index}", map[string]string{
  72. "index": strings.Join(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.local != nil {
  83. params.Set("local", fmt.Sprintf("%v", *s.local))
  84. }
  85. if s.ignoreUnavailable != nil {
  86. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  87. }
  88. if s.allowNoIndices != nil {
  89. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  90. }
  91. if s.expandWildcards != "" {
  92. params.Set("expand_wildcards", s.expandWildcards)
  93. }
  94. return path, params, nil
  95. }
  96. // Validate checks if the operation is valid.
  97. func (s *IndicesExistsService) Validate() error {
  98. var invalid []string
  99. if len(s.index) == 0 {
  100. invalid = append(invalid, "Index")
  101. }
  102. if len(invalid) > 0 {
  103. return fmt.Errorf("missing required fields: %v", invalid)
  104. }
  105. return nil
  106. }
  107. // Do executes the operation.
  108. func (s *IndicesExistsService) Do(ctx context.Context) (bool, error) {
  109. // Check pre-conditions
  110. if err := s.Validate(); err != nil {
  111. return false, err
  112. }
  113. // Get URL for request
  114. path, params, err := s.buildURL()
  115. if err != nil {
  116. return false, err
  117. }
  118. // Get HTTP response
  119. res, err := s.client.PerformRequest(ctx, "HEAD", path, params, nil, 404)
  120. if err != nil {
  121. return false, err
  122. }
  123. // Return operation response
  124. switch res.StatusCode {
  125. case http.StatusOK:
  126. return true, nil
  127. case http.StatusNotFound:
  128. return false, nil
  129. default:
  130. return false, fmt.Errorf("elastic: got HTTP code %d when it should have been either 200 or 404", res.StatusCode)
  131. }
  132. }