indices_get.go 5.4 KB

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