index_get.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. "log"
  9. "net/url"
  10. "strings"
  11. "github.com/olivere/elastic/uritemplates"
  12. )
  13. var (
  14. _ = fmt.Print
  15. _ = log.Print
  16. _ = strings.Index
  17. _ = uritemplates.Expand
  18. _ = url.Parse
  19. )
  20. // IndicesGetService retrieves information about one or more indices.
  21. // See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-index.html.
  22. type IndicesGetService struct {
  23. client *Client
  24. pretty bool
  25. index []string
  26. feature []string
  27. expandWildcards string
  28. local *bool
  29. ignoreUnavailable *bool
  30. allowNoIndices *bool
  31. }
  32. // NewIndicesGetService creates a new IndicesGetService.
  33. func NewIndicesGetService(client *Client) *IndicesGetService {
  34. return &IndicesGetService{
  35. client: client,
  36. index: make([]string, 0),
  37. feature: make([]string, 0),
  38. }
  39. }
  40. // Index is a list of index names. Use _all to retrieve information about
  41. // all indices of a cluster.
  42. func (s *IndicesGetService) Index(index ...string) *IndicesGetService {
  43. s.index = append(s.index, index...)
  44. return s
  45. }
  46. // Feature is a list of features (e.g. _settings,_mappings,_warmers, and _aliases).
  47. func (s *IndicesGetService) Feature(feature ...string) *IndicesGetService {
  48. s.feature = append(s.feature, feature...)
  49. return s
  50. }
  51. // ExpandWildcards indicates whether wildcard expressions should
  52. // get expanded to open or closed indices (default: open).
  53. func (s *IndicesGetService) ExpandWildcards(expandWildcards string) *IndicesGetService {
  54. s.expandWildcards = expandWildcards
  55. return s
  56. }
  57. // Local indicates whether to return local information (do not retrieve
  58. // the state from master node (default: false)).
  59. func (s *IndicesGetService) Local(local bool) *IndicesGetService {
  60. s.local = &local
  61. return s
  62. }
  63. // IgnoreUnavailable indicates whether to ignore unavailable indexes (default: false).
  64. func (s *IndicesGetService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesGetService {
  65. s.ignoreUnavailable = &ignoreUnavailable
  66. return s
  67. }
  68. // AllowNoIndices indicates whether to ignore if a wildcard expression
  69. // resolves to no concrete indices (default: false).
  70. func (s *IndicesGetService) AllowNoIndices(allowNoIndices bool) *IndicesGetService {
  71. s.allowNoIndices = &allowNoIndices
  72. return s
  73. }
  74. // Pretty indicates that the JSON response be indented and human readable.
  75. func (s *IndicesGetService) Pretty(pretty bool) *IndicesGetService {
  76. s.pretty = pretty
  77. return s
  78. }
  79. // buildURL builds the URL for the operation.
  80. func (s *IndicesGetService) buildURL() (string, url.Values, error) {
  81. var err error
  82. var path string
  83. var index []string
  84. if len(s.index) > 0 {
  85. index = s.index
  86. } else {
  87. index = []string{"_all"}
  88. }
  89. if len(s.feature) > 0 {
  90. // Build URL
  91. path, err = uritemplates.Expand("/{index}/{feature}", map[string]string{
  92. "index": strings.Join(index, ","),
  93. "feature": strings.Join(s.feature, ","),
  94. })
  95. } else {
  96. // Build URL
  97. path, err = uritemplates.Expand("/{index}", map[string]string{
  98. "index": strings.Join(index, ","),
  99. })
  100. }
  101. if err != nil {
  102. return "", url.Values{}, err
  103. }
  104. // Add query string parameters
  105. params := url.Values{}
  106. if s.pretty {
  107. params.Set("pretty", "1")
  108. }
  109. if s.expandWildcards != "" {
  110. params.Set("expand_wildcards", s.expandWildcards)
  111. }
  112. if s.local != nil {
  113. params.Set("local", fmt.Sprintf("%v", *s.local))
  114. }
  115. if s.ignoreUnavailable != nil {
  116. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  117. }
  118. if s.allowNoIndices != nil {
  119. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  120. }
  121. return path, params, nil
  122. }
  123. // Validate checks if the operation is valid.
  124. func (s *IndicesGetService) Validate() error {
  125. var invalid []string
  126. if len(s.index) == 0 {
  127. invalid = append(invalid, "Index")
  128. }
  129. if len(invalid) > 0 {
  130. return fmt.Errorf("missing required fields: %v", invalid)
  131. }
  132. return nil
  133. }
  134. // Do executes the operation.
  135. func (s *IndicesGetService) Do() (map[string]*IndicesGetResponse, error) {
  136. // Check pre-conditions
  137. if err := s.Validate(); err != nil {
  138. return nil, err
  139. }
  140. // Get URL for request
  141. path, params, err := s.buildURL()
  142. if err != nil {
  143. return nil, err
  144. }
  145. // Get HTTP response
  146. res, err := s.client.PerformRequest("GET", path, params, nil)
  147. if err != nil {
  148. return nil, err
  149. }
  150. // Return operation response
  151. var ret map[string]*IndicesGetResponse
  152. if err := json.Unmarshal(res.Body, &ret); err != nil {
  153. return nil, err
  154. }
  155. return ret, nil
  156. }
  157. // IndicesGetResponse is part of the response of IndicesGetService.Do.
  158. type IndicesGetResponse struct {
  159. Aliases map[string]interface{} `json:"aliases"`
  160. Mappings map[string]interface{} `json:"mappings"`
  161. Settings map[string]interface{} `json:"settings"`
  162. Warmers map[string]interface{} `json:"warmers"`
  163. }