indices_flush.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // Flush allows to flush one or more indices. The flush process of an index
  13. // basically frees memory from the index by flushing data to the index
  14. // storage and clearing the internal transaction log.
  15. //
  16. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-flush.html
  17. // for details.
  18. type IndicesFlushService struct {
  19. client *Client
  20. pretty bool
  21. index []string
  22. force *bool
  23. waitIfOngoing *bool
  24. ignoreUnavailable *bool
  25. allowNoIndices *bool
  26. expandWildcards string
  27. }
  28. // NewIndicesFlushService creates a new IndicesFlushService.
  29. func NewIndicesFlushService(client *Client) *IndicesFlushService {
  30. return &IndicesFlushService{
  31. client: client,
  32. index: make([]string, 0),
  33. }
  34. }
  35. // Index is a list of index names; use `_all` or empty string for all indices.
  36. func (s *IndicesFlushService) Index(indices ...string) *IndicesFlushService {
  37. s.index = append(s.index, indices...)
  38. return s
  39. }
  40. // Force indicates whether a flush should be forced even if it is not
  41. // necessarily needed ie. if no changes will be committed to the index.
  42. // This is useful if transaction log IDs should be incremented even if
  43. // no uncommitted changes are present. (This setting can be considered as internal).
  44. func (s *IndicesFlushService) Force(force bool) *IndicesFlushService {
  45. s.force = &force
  46. return s
  47. }
  48. // WaitIfOngoing, if set to true, indicates that the flush operation will
  49. // block until the flush can be executed if another flush operation is
  50. // already executing. The default is false and will cause an exception
  51. // to be thrown on the shard level if another flush operation is already running..
  52. func (s *IndicesFlushService) WaitIfOngoing(waitIfOngoing bool) *IndicesFlushService {
  53. s.waitIfOngoing = &waitIfOngoing
  54. return s
  55. }
  56. // IgnoreUnavailable indicates whether specified concrete indices should be
  57. // ignored when unavailable (missing or closed).
  58. func (s *IndicesFlushService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesFlushService {
  59. s.ignoreUnavailable = &ignoreUnavailable
  60. return s
  61. }
  62. // AllowNoIndices indicates whether to ignore if a wildcard indices expression
  63. // resolves into no concrete indices. (This includes `_all` string or when
  64. // no indices have been specified).
  65. func (s *IndicesFlushService) AllowNoIndices(allowNoIndices bool) *IndicesFlushService {
  66. s.allowNoIndices = &allowNoIndices
  67. return s
  68. }
  69. // ExpandWildcards specifies whether to expand wildcard expression to
  70. // concrete indices that are open, closed or both..
  71. func (s *IndicesFlushService) ExpandWildcards(expandWildcards string) *IndicesFlushService {
  72. s.expandWildcards = expandWildcards
  73. return s
  74. }
  75. // Pretty indicates that the JSON response be indented and human readable.
  76. func (s *IndicesFlushService) Pretty(pretty bool) *IndicesFlushService {
  77. s.pretty = pretty
  78. return s
  79. }
  80. // buildURL builds the URL for the operation.
  81. func (s *IndicesFlushService) buildURL() (string, url.Values, error) {
  82. // Build URL
  83. var err error
  84. var path string
  85. if len(s.index) > 0 {
  86. path, err = uritemplates.Expand("/{index}/_flush", map[string]string{
  87. "index": strings.Join(s.index, ","),
  88. })
  89. } else {
  90. path = "/_flush"
  91. }
  92. if err != nil {
  93. return "", url.Values{}, err
  94. }
  95. // Add query string parameters
  96. params := url.Values{}
  97. if s.pretty {
  98. params.Set("pretty", "1")
  99. }
  100. if s.force != nil {
  101. params.Set("force", fmt.Sprintf("%v", *s.force))
  102. }
  103. if s.waitIfOngoing != nil {
  104. params.Set("wait_if_ongoing", fmt.Sprintf("%v", *s.waitIfOngoing))
  105. }
  106. if s.ignoreUnavailable != nil {
  107. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  108. }
  109. if s.allowNoIndices != nil {
  110. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  111. }
  112. if s.expandWildcards != "" {
  113. params.Set("expand_wildcards", s.expandWildcards)
  114. }
  115. return path, params, nil
  116. }
  117. // Validate checks if the operation is valid.
  118. func (s *IndicesFlushService) Validate() error {
  119. return nil
  120. }
  121. // Do executes the service.
  122. func (s *IndicesFlushService) Do(ctx context.Context) (*IndicesFlushResponse, error) {
  123. // Check pre-conditions
  124. if err := s.Validate(); err != nil {
  125. return nil, err
  126. }
  127. // Get URL for request
  128. path, params, err := s.buildURL()
  129. if err != nil {
  130. return nil, err
  131. }
  132. // Get HTTP response
  133. res, err := s.client.PerformRequest(ctx, "POST", path, params, nil)
  134. if err != nil {
  135. return nil, err
  136. }
  137. // Return operation response
  138. ret := new(IndicesFlushResponse)
  139. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  140. return nil, err
  141. }
  142. return ret, nil
  143. }
  144. // -- Result of a flush request.
  145. type IndicesFlushResponse struct {
  146. Shards *ShardsInfo `json:"_shards"`
  147. }