indices_put_settings.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. // IndicesPutSettingsService changes specific index level settings in
  13. // real time.
  14. //
  15. // See the documentation at
  16. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-update-settings.html.
  17. type IndicesPutSettingsService struct {
  18. client *Client
  19. pretty bool
  20. index []string
  21. allowNoIndices *bool
  22. expandWildcards string
  23. flatSettings *bool
  24. ignoreUnavailable *bool
  25. masterTimeout string
  26. bodyJson interface{}
  27. bodyString string
  28. }
  29. // NewIndicesPutSettingsService creates a new IndicesPutSettingsService.
  30. func NewIndicesPutSettingsService(client *Client) *IndicesPutSettingsService {
  31. return &IndicesPutSettingsService{
  32. client: client,
  33. index: make([]string, 0),
  34. }
  35. }
  36. // Index is a list of index names the mapping should be added to
  37. // (supports wildcards); use `_all` or omit to add the mapping on all indices.
  38. func (s *IndicesPutSettingsService) Index(indices ...string) *IndicesPutSettingsService {
  39. s.index = append(s.index, indices...)
  40. return s
  41. }
  42. // AllowNoIndices indicates whether to ignore if a wildcard indices
  43. // expression resolves into no concrete indices. (This includes `_all`
  44. // string or when no indices have been specified).
  45. func (s *IndicesPutSettingsService) AllowNoIndices(allowNoIndices bool) *IndicesPutSettingsService {
  46. s.allowNoIndices = &allowNoIndices
  47. return s
  48. }
  49. // ExpandWildcards specifies whether to expand wildcard expression to
  50. // concrete indices that are open, closed or both.
  51. func (s *IndicesPutSettingsService) ExpandWildcards(expandWildcards string) *IndicesPutSettingsService {
  52. s.expandWildcards = expandWildcards
  53. return s
  54. }
  55. // FlatSettings indicates whether to return settings in flat format (default: false).
  56. func (s *IndicesPutSettingsService) FlatSettings(flatSettings bool) *IndicesPutSettingsService {
  57. s.flatSettings = &flatSettings
  58. return s
  59. }
  60. // IgnoreUnavailable specifies whether specified concrete indices should be
  61. // ignored when unavailable (missing or closed).
  62. func (s *IndicesPutSettingsService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesPutSettingsService {
  63. s.ignoreUnavailable = &ignoreUnavailable
  64. return s
  65. }
  66. // MasterTimeout is the timeout for connection to master.
  67. func (s *IndicesPutSettingsService) MasterTimeout(masterTimeout string) *IndicesPutSettingsService {
  68. s.masterTimeout = masterTimeout
  69. return s
  70. }
  71. // Pretty indicates that the JSON response be indented and human readable.
  72. func (s *IndicesPutSettingsService) Pretty(pretty bool) *IndicesPutSettingsService {
  73. s.pretty = pretty
  74. return s
  75. }
  76. // BodyJson is documented as: The index settings to be updated.
  77. func (s *IndicesPutSettingsService) BodyJson(body interface{}) *IndicesPutSettingsService {
  78. s.bodyJson = body
  79. return s
  80. }
  81. // BodyString is documented as: The index settings to be updated.
  82. func (s *IndicesPutSettingsService) BodyString(body string) *IndicesPutSettingsService {
  83. s.bodyString = body
  84. return s
  85. }
  86. // buildURL builds the URL for the operation.
  87. func (s *IndicesPutSettingsService) buildURL() (string, url.Values, error) {
  88. // Build URL
  89. var err error
  90. var path string
  91. if len(s.index) > 0 {
  92. path, err = uritemplates.Expand("/{index}/_settings", map[string]string{
  93. "index": strings.Join(s.index, ","),
  94. })
  95. } else {
  96. path = "/_settings"
  97. }
  98. if err != nil {
  99. return "", url.Values{}, err
  100. }
  101. // Add query string parameters
  102. params := url.Values{}
  103. if s.pretty {
  104. params.Set("pretty", "1")
  105. }
  106. if s.allowNoIndices != nil {
  107. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  108. }
  109. if s.expandWildcards != "" {
  110. params.Set("expand_wildcards", s.expandWildcards)
  111. }
  112. if s.flatSettings != nil {
  113. params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings))
  114. }
  115. if s.ignoreUnavailable != nil {
  116. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  117. }
  118. if s.masterTimeout != "" {
  119. params.Set("master_timeout", s.masterTimeout)
  120. }
  121. return path, params, nil
  122. }
  123. // Validate checks if the operation is valid.
  124. func (s *IndicesPutSettingsService) Validate() error {
  125. return nil
  126. }
  127. // Do executes the operation.
  128. func (s *IndicesPutSettingsService) Do(ctx context.Context) (*IndicesPutSettingsResponse, error) {
  129. // Check pre-conditions
  130. if err := s.Validate(); err != nil {
  131. return nil, err
  132. }
  133. // Get URL for request
  134. path, params, err := s.buildURL()
  135. if err != nil {
  136. return nil, err
  137. }
  138. // Setup HTTP request body
  139. var body interface{}
  140. if s.bodyJson != nil {
  141. body = s.bodyJson
  142. } else {
  143. body = s.bodyString
  144. }
  145. // Get HTTP response
  146. res, err := s.client.PerformRequest(ctx, "PUT", path, params, body)
  147. if err != nil {
  148. return nil, err
  149. }
  150. // Return operation response
  151. ret := new(IndicesPutSettingsResponse)
  152. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  153. return nil, err
  154. }
  155. return ret, nil
  156. }
  157. // IndicesPutSettingsResponse is the response of IndicesPutSettingsService.Do.
  158. type IndicesPutSettingsResponse struct {
  159. Acknowledged bool `json:"acknowledged"`
  160. }