index_get_settings.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. // IndicesGetSettingsService allows to retrieve settings of one
  21. // or more indices.
  22. // See http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.4/indices-get-settings.html.
  23. type IndicesGetSettingsService struct {
  24. client *Client
  25. pretty bool
  26. index []string
  27. name []string
  28. ignoreUnavailable *bool
  29. allowNoIndices *bool
  30. expandWildcards string
  31. flatSettings *bool
  32. local *bool
  33. }
  34. // NewIndicesGetSettingsService creates a new IndicesGetSettingsService.
  35. func NewIndicesGetSettingsService(client *Client) *IndicesGetSettingsService {
  36. return &IndicesGetSettingsService{
  37. client: client,
  38. index: make([]string, 0),
  39. name: make([]string, 0),
  40. }
  41. }
  42. // Index is a list of index names; use `_all` or empty string to perform the operation on all indices.
  43. func (s *IndicesGetSettingsService) Index(index ...string) *IndicesGetSettingsService {
  44. s.index = append(s.index, index...)
  45. return s
  46. }
  47. // Name are the names of the settings that should be included.
  48. func (s *IndicesGetSettingsService) Name(name ...string) *IndicesGetSettingsService {
  49. s.name = append(s.name, name...)
  50. return s
  51. }
  52. // IgnoreUnavailable indicates whether specified concrete indices should
  53. // be ignored when unavailable (missing or closed).
  54. func (s *IndicesGetSettingsService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesGetSettingsService {
  55. s.ignoreUnavailable = &ignoreUnavailable
  56. return s
  57. }
  58. // AllowNoIndices indicates whether to ignore if a wildcard indices
  59. // expression resolves into no concrete indices.
  60. // (This includes `_all` string or when no indices have been specified).
  61. func (s *IndicesGetSettingsService) AllowNoIndices(allowNoIndices bool) *IndicesGetSettingsService {
  62. s.allowNoIndices = &allowNoIndices
  63. return s
  64. }
  65. // ExpandWildcards indicates whether to expand wildcard expression
  66. // to concrete indices that are open, closed or both.
  67. // Options: open, closed, none, all. Default: open,closed.
  68. func (s *IndicesGetSettingsService) ExpandWildcards(expandWildcards string) *IndicesGetSettingsService {
  69. s.expandWildcards = expandWildcards
  70. return s
  71. }
  72. // FlatSettings indicates whether to return settings in flat format (default: false).
  73. func (s *IndicesGetSettingsService) FlatSettings(flatSettings bool) *IndicesGetSettingsService {
  74. s.flatSettings = &flatSettings
  75. return s
  76. }
  77. // Local indicates whether to return local information, do not retrieve
  78. // the state from master node (default: false).
  79. func (s *IndicesGetSettingsService) Local(local bool) *IndicesGetSettingsService {
  80. s.local = &local
  81. return s
  82. }
  83. // Pretty indicates that the JSON response be indented and human readable.
  84. func (s *IndicesGetSettingsService) Pretty(pretty bool) *IndicesGetSettingsService {
  85. s.pretty = pretty
  86. return s
  87. }
  88. // buildURL builds the URL for the operation.
  89. func (s *IndicesGetSettingsService) 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.name) > 0 {
  99. // Build URL
  100. path, err = uritemplates.Expand("/{index}/_settings/{name}", map[string]string{
  101. "index": strings.Join(index, ","),
  102. "name": strings.Join(s.name, ","),
  103. })
  104. } else {
  105. // Build URL
  106. path, err = uritemplates.Expand("/{index}/_settings", 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.ignoreUnavailable != nil {
  119. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  120. }
  121. if s.allowNoIndices != nil {
  122. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  123. }
  124. if s.expandWildcards != "" {
  125. params.Set("expand_wildcards", s.expandWildcards)
  126. }
  127. if s.flatSettings != nil {
  128. params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings))
  129. }
  130. if s.local != nil {
  131. params.Set("local", fmt.Sprintf("%v", *s.local))
  132. }
  133. return path, params, nil
  134. }
  135. // Validate checks if the operation is valid.
  136. func (s *IndicesGetSettingsService) Validate() error {
  137. return nil
  138. }
  139. // Do executes the operation.
  140. func (s *IndicesGetSettingsService) Do() (map[string]*IndicesGetSettingsResponse, error) {
  141. // Check pre-conditions
  142. if err := s.Validate(); err != nil {
  143. return nil, err
  144. }
  145. // Get URL for request
  146. path, params, err := s.buildURL()
  147. if err != nil {
  148. return nil, err
  149. }
  150. // Get HTTP response
  151. res, err := s.client.PerformRequest("GET", path, params, nil)
  152. if err != nil {
  153. return nil, err
  154. }
  155. // Return operation response
  156. var ret map[string]*IndicesGetSettingsResponse
  157. if err := json.Unmarshal(res.Body, &ret); err != nil {
  158. return nil, err
  159. }
  160. return ret, nil
  161. }
  162. // IndicesGetSettingsResponse is the response of IndicesGetSettingsService.Do.
  163. type IndicesGetSettingsResponse struct {
  164. Settings map[string]interface{} `json:"settings"`
  165. }