Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

indices_freeze.go 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/http"
  9. "net/url"
  10. "strings"
  11. "github.com/olivere/elastic/v7/uritemplates"
  12. )
  13. // IndicesFreezeService freezes an index.
  14. //
  15. // See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/freeze-index-api.html
  16. // and https://www.elastic.co/blog/creating-frozen-indices-with-the-elasticsearch-freeze-index-api
  17. // for details.
  18. type IndicesFreezeService struct {
  19. client *Client
  20. pretty *bool // pretty format the returned JSON response
  21. human *bool // return human readable values for statistics
  22. errorTrace *bool // include the stack trace of returned errors
  23. filterPath []string // list of filters used to reduce the response
  24. headers http.Header // custom request-level HTTP headers
  25. index string
  26. timeout string
  27. masterTimeout string
  28. ignoreUnavailable *bool
  29. allowNoIndices *bool
  30. expandWildcards string
  31. waitForActiveShards string
  32. }
  33. // NewIndicesFreezeService creates a new IndicesFreezeService.
  34. func NewIndicesFreezeService(client *Client) *IndicesFreezeService {
  35. return &IndicesFreezeService{
  36. client: client,
  37. }
  38. }
  39. // Pretty tells Elasticsearch whether to return a formatted JSON response.
  40. func (s *IndicesFreezeService) Pretty(pretty bool) *IndicesFreezeService {
  41. s.pretty = &pretty
  42. return s
  43. }
  44. // Human specifies whether human readable values should be returned in
  45. // the JSON response, e.g. "7.5mb".
  46. func (s *IndicesFreezeService) Human(human bool) *IndicesFreezeService {
  47. s.human = &human
  48. return s
  49. }
  50. // ErrorTrace specifies whether to include the stack trace of returned errors.
  51. func (s *IndicesFreezeService) ErrorTrace(errorTrace bool) *IndicesFreezeService {
  52. s.errorTrace = &errorTrace
  53. return s
  54. }
  55. // FilterPath specifies a list of filters used to reduce the response.
  56. func (s *IndicesFreezeService) FilterPath(filterPath ...string) *IndicesFreezeService {
  57. s.filterPath = filterPath
  58. return s
  59. }
  60. // Header adds a header to the request.
  61. func (s *IndicesFreezeService) Header(name string, value string) *IndicesFreezeService {
  62. if s.headers == nil {
  63. s.headers = http.Header{}
  64. }
  65. s.headers.Add(name, value)
  66. return s
  67. }
  68. // Headers specifies the headers of the request.
  69. func (s *IndicesFreezeService) Headers(headers http.Header) *IndicesFreezeService {
  70. s.headers = headers
  71. return s
  72. }
  73. // Index is the name of the index to freeze.
  74. func (s *IndicesFreezeService) Index(index string) *IndicesFreezeService {
  75. s.index = index
  76. return s
  77. }
  78. // Timeout allows to specify an explicit timeout.
  79. func (s *IndicesFreezeService) Timeout(timeout string) *IndicesFreezeService {
  80. s.timeout = timeout
  81. return s
  82. }
  83. // MasterTimeout allows to specify a timeout for connection to master.
  84. func (s *IndicesFreezeService) MasterTimeout(masterTimeout string) *IndicesFreezeService {
  85. s.masterTimeout = masterTimeout
  86. return s
  87. }
  88. // IgnoreUnavailable indicates whether specified concrete indices should be
  89. // ignored when unavailable (missing or closed).
  90. func (s *IndicesFreezeService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesFreezeService {
  91. s.ignoreUnavailable = &ignoreUnavailable
  92. return s
  93. }
  94. // AllowNoIndices indicates whether to ignore if a wildcard indices expression
  95. // resolves into no concrete indices. (This includes `_all` string or when
  96. // no indices have been specified).
  97. func (s *IndicesFreezeService) AllowNoIndices(allowNoIndices bool) *IndicesFreezeService {
  98. s.allowNoIndices = &allowNoIndices
  99. return s
  100. }
  101. // ExpandWildcards specifies whether to expand wildcard expression to
  102. // concrete indices that are open, closed or both..
  103. func (s *IndicesFreezeService) ExpandWildcards(expandWildcards string) *IndicesFreezeService {
  104. s.expandWildcards = expandWildcards
  105. return s
  106. }
  107. // WaitForActiveShards sets the number of active shards to wait for
  108. // before the operation returns.
  109. func (s *IndicesFreezeService) WaitForActiveShards(numShards string) *IndicesFreezeService {
  110. s.waitForActiveShards = numShards
  111. return s
  112. }
  113. // buildURL builds the URL for the operation.
  114. func (s *IndicesFreezeService) buildURL() (string, url.Values, error) {
  115. // Build URL
  116. path, err := uritemplates.Expand("/{index}/_freeze", map[string]string{
  117. "index": s.index,
  118. })
  119. if err != nil {
  120. return "", url.Values{}, err
  121. }
  122. // Add query string parameters
  123. params := url.Values{}
  124. if v := s.pretty; v != nil {
  125. params.Set("pretty", fmt.Sprint(*v))
  126. }
  127. if v := s.human; v != nil {
  128. params.Set("human", fmt.Sprint(*v))
  129. }
  130. if v := s.errorTrace; v != nil {
  131. params.Set("error_trace", fmt.Sprint(*v))
  132. }
  133. if len(s.filterPath) > 0 {
  134. params.Set("filter_path", strings.Join(s.filterPath, ","))
  135. }
  136. if s.timeout != "" {
  137. params.Set("timeout", s.timeout)
  138. }
  139. if s.masterTimeout != "" {
  140. params.Set("master_timeout", s.masterTimeout)
  141. }
  142. if s.expandWildcards != "" {
  143. params.Set("expand_wildcards", s.expandWildcards)
  144. }
  145. if s.ignoreUnavailable != nil {
  146. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  147. }
  148. if s.allowNoIndices != nil {
  149. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  150. }
  151. if s.expandWildcards != "" {
  152. params.Set("expand_wildcards", s.expandWildcards)
  153. }
  154. if s.waitForActiveShards != "" {
  155. params.Set("wait_for_active_shards", s.waitForActiveShards)
  156. }
  157. return path, params, nil
  158. }
  159. // Validate checks if the operation is valid.
  160. func (s *IndicesFreezeService) Validate() error {
  161. var invalid []string
  162. if s.index == "" {
  163. invalid = append(invalid, "Index")
  164. }
  165. if len(invalid) > 0 {
  166. return fmt.Errorf("missing required fields: %v", invalid)
  167. }
  168. return nil
  169. }
  170. // Do executes the service.
  171. func (s *IndicesFreezeService) Do(ctx context.Context) (*IndicesFreezeResponse, error) {
  172. // Check pre-conditions
  173. if err := s.Validate(); err != nil {
  174. return nil, err
  175. }
  176. // Get URL for request
  177. path, params, err := s.buildURL()
  178. if err != nil {
  179. return nil, err
  180. }
  181. // Get HTTP response
  182. res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
  183. Method: "POST",
  184. Path: path,
  185. Params: params,
  186. Headers: s.headers,
  187. })
  188. if err != nil {
  189. return nil, err
  190. }
  191. // Return operation response
  192. ret := new(IndicesFreezeResponse)
  193. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  194. return nil, err
  195. }
  196. return ret, nil
  197. }
  198. // IndicesFreezeResponse is the outcome of freezing an index.
  199. type IndicesFreezeResponse struct {
  200. Shards *ShardsInfo `json:"_shards"`
  201. }