indices_get_settings.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. // IndicesGetSettingsService allows to retrieve settings of one
  13. // or more indices.
  14. //
  15. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-get-settings.html
  16. // for more details.
  17. type IndicesGetSettingsService struct {
  18. client *Client
  19. pretty bool
  20. index []string
  21. name []string
  22. ignoreUnavailable *bool
  23. allowNoIndices *bool
  24. expandWildcards string
  25. flatSettings *bool
  26. local *bool
  27. }
  28. // NewIndicesGetSettingsService creates a new IndicesGetSettingsService.
  29. func NewIndicesGetSettingsService(client *Client) *IndicesGetSettingsService {
  30. return &IndicesGetSettingsService{
  31. client: client,
  32. index: make([]string, 0),
  33. name: make([]string, 0),
  34. }
  35. }
  36. // Index is a list of index names; use `_all` or empty string to perform
  37. // the operation on all indices.
  38. func (s *IndicesGetSettingsService) Index(indices ...string) *IndicesGetSettingsService {
  39. s.index = append(s.index, indices...)
  40. return s
  41. }
  42. // Name are the names of the settings that should be included.
  43. func (s *IndicesGetSettingsService) Name(name ...string) *IndicesGetSettingsService {
  44. s.name = append(s.name, name...)
  45. return s
  46. }
  47. // IgnoreUnavailable indicates whether specified concrete indices should
  48. // be ignored when unavailable (missing or closed).
  49. func (s *IndicesGetSettingsService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesGetSettingsService {
  50. s.ignoreUnavailable = &ignoreUnavailable
  51. return s
  52. }
  53. // AllowNoIndices indicates whether to ignore if a wildcard indices
  54. // expression resolves into no concrete indices.
  55. // (This includes `_all` string or when no indices have been specified).
  56. func (s *IndicesGetSettingsService) AllowNoIndices(allowNoIndices bool) *IndicesGetSettingsService {
  57. s.allowNoIndices = &allowNoIndices
  58. return s
  59. }
  60. // ExpandWildcards indicates whether to expand wildcard expression
  61. // to concrete indices that are open, closed or both.
  62. // Options: open, closed, none, all. Default: open,closed.
  63. func (s *IndicesGetSettingsService) ExpandWildcards(expandWildcards string) *IndicesGetSettingsService {
  64. s.expandWildcards = expandWildcards
  65. return s
  66. }
  67. // FlatSettings indicates whether to return settings in flat format (default: false).
  68. func (s *IndicesGetSettingsService) FlatSettings(flatSettings bool) *IndicesGetSettingsService {
  69. s.flatSettings = &flatSettings
  70. return s
  71. }
  72. // Local indicates whether to return local information, do not retrieve
  73. // the state from master node (default: false).
  74. func (s *IndicesGetSettingsService) Local(local bool) *IndicesGetSettingsService {
  75. s.local = &local
  76. return s
  77. }
  78. // Pretty indicates that the JSON response be indented and human readable.
  79. func (s *IndicesGetSettingsService) Pretty(pretty bool) *IndicesGetSettingsService {
  80. s.pretty = pretty
  81. return s
  82. }
  83. // buildURL builds the URL for the operation.
  84. func (s *IndicesGetSettingsService) buildURL() (string, url.Values, error) {
  85. var err error
  86. var path string
  87. var index []string
  88. if len(s.index) > 0 {
  89. index = s.index
  90. } else {
  91. index = []string{"_all"}
  92. }
  93. if len(s.name) > 0 {
  94. // Build URL
  95. path, err = uritemplates.Expand("/{index}/_settings/{name}", map[string]string{
  96. "index": strings.Join(index, ","),
  97. "name": strings.Join(s.name, ","),
  98. })
  99. } else {
  100. // Build URL
  101. path, err = uritemplates.Expand("/{index}/_settings", map[string]string{
  102. "index": strings.Join(index, ","),
  103. })
  104. }
  105. if err != nil {
  106. return "", url.Values{}, err
  107. }
  108. // Add query string parameters
  109. params := url.Values{}
  110. if s.pretty {
  111. params.Set("pretty", "1")
  112. }
  113. if s.ignoreUnavailable != nil {
  114. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  115. }
  116. if s.allowNoIndices != nil {
  117. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  118. }
  119. if s.expandWildcards != "" {
  120. params.Set("expand_wildcards", s.expandWildcards)
  121. }
  122. if s.flatSettings != nil {
  123. params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings))
  124. }
  125. if s.local != nil {
  126. params.Set("local", fmt.Sprintf("%v", *s.local))
  127. }
  128. return path, params, nil
  129. }
  130. // Validate checks if the operation is valid.
  131. func (s *IndicesGetSettingsService) Validate() error {
  132. return nil
  133. }
  134. // Do executes the operation.
  135. func (s *IndicesGetSettingsService) Do(ctx context.Context) (map[string]*IndicesGetSettingsResponse, 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(ctx, "GET", path, params, nil)
  147. if err != nil {
  148. return nil, err
  149. }
  150. // Return operation response
  151. var ret map[string]*IndicesGetSettingsResponse
  152. if err := s.client.decoder.Decode(res.Body, &ret); err != nil {
  153. return nil, err
  154. }
  155. return ret, nil
  156. }
  157. // IndicesGetSettingsResponse is the response of IndicesGetSettingsService.Do.
  158. type IndicesGetSettingsResponse struct {
  159. Settings map[string]interface{} `json:"settings"`
  160. }