flush.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. "net/url"
  9. "strings"
  10. "github.com/olivere/elastic/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 http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-flush.html
  17. // for details.
  18. type FlushService struct {
  19. client *Client
  20. indices []string
  21. force *bool
  22. full *bool
  23. waitIfOngoing *bool
  24. ignoreUnavailable *bool
  25. allowNoIndices *bool
  26. expandWildcards string
  27. }
  28. func NewFlushService(client *Client) *FlushService {
  29. builder := &FlushService{
  30. client: client,
  31. }
  32. return builder
  33. }
  34. func (s *FlushService) Index(index string) *FlushService {
  35. if s.indices == nil {
  36. s.indices = make([]string, 0)
  37. }
  38. s.indices = append(s.indices, index)
  39. return s
  40. }
  41. func (s *FlushService) Indices(indices ...string) *FlushService {
  42. if s.indices == nil {
  43. s.indices = make([]string, 0)
  44. }
  45. s.indices = append(s.indices, indices...)
  46. return s
  47. }
  48. // Force specifies whether to force a flush even if it is not necessary.
  49. func (s *FlushService) Force(force bool) *FlushService {
  50. s.force = &force
  51. return s
  52. }
  53. // Full, when set to true, creates a new index writer for the index and
  54. // refreshes all settings related to the index.
  55. func (s *FlushService) Full(full bool) *FlushService {
  56. s.full = &full
  57. return s
  58. }
  59. // WaitIfOngoing will block until the flush can be executed (if set to true)
  60. // if another flush operation is already executing. The default is false
  61. // and will cause an exception to be thrown on the shard level if another
  62. // flush operation is already running. [1.4.0.Beta1]
  63. func (s *FlushService) WaitIfOngoing(wait bool) *FlushService {
  64. s.waitIfOngoing = &wait
  65. return s
  66. }
  67. // IgnoreUnavailable specifies whether concrete indices should be ignored
  68. // when unavailable (e.g. missing or closed).
  69. func (s *FlushService) IgnoreUnavailable(ignoreUnavailable bool) *FlushService {
  70. s.ignoreUnavailable = &ignoreUnavailable
  71. return s
  72. }
  73. // AllowNoIndices specifies whether to ignore if a wildcard expression
  74. // yields no indices. This includes the _all index or when no indices
  75. // have been specified.
  76. func (s *FlushService) AllowNoIndices(allowNoIndices bool) *FlushService {
  77. s.allowNoIndices = &allowNoIndices
  78. return s
  79. }
  80. // ExpandWildcards specifies whether to expand wildcards to concrete indices
  81. // that are open, closed, or both. Use one of "open", "closed", "none", or "all".
  82. func (s *FlushService) ExpandWildcards(expandWildcards string) *FlushService {
  83. s.expandWildcards = expandWildcards
  84. return s
  85. }
  86. // Do executes the service.
  87. func (s *FlushService) Do() (*FlushResult, error) {
  88. // Build url
  89. path := "/"
  90. // Indices part
  91. if len(s.indices) > 0 {
  92. indexPart := make([]string, 0)
  93. for _, index := range s.indices {
  94. index, err := uritemplates.Expand("{index}", map[string]string{
  95. "index": index,
  96. })
  97. if err != nil {
  98. return nil, err
  99. }
  100. indexPart = append(indexPart, index)
  101. }
  102. path += strings.Join(indexPart, ",") + "/"
  103. }
  104. path += "_flush"
  105. // Parameters
  106. params := make(url.Values)
  107. if s.force != nil {
  108. params.Set("force", fmt.Sprintf("%v", *s.force))
  109. }
  110. if s.full != nil {
  111. params.Set("full", fmt.Sprintf("%v", *s.full))
  112. }
  113. if s.waitIfOngoing != nil {
  114. params.Set("wait_if_ongoing", fmt.Sprintf("%v", *s.waitIfOngoing))
  115. }
  116. if s.ignoreUnavailable != nil {
  117. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  118. }
  119. if s.allowNoIndices != nil {
  120. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  121. }
  122. if s.expandWildcards != "" {
  123. params.Set("expand_wildcards", s.expandWildcards)
  124. }
  125. // Get response
  126. res, err := s.client.PerformRequest("POST", path, params, nil)
  127. if err != nil {
  128. return nil, err
  129. }
  130. // Return result
  131. ret := new(FlushResult)
  132. if err := json.Unmarshal(res.Body, ret); err != nil {
  133. return nil, err
  134. }
  135. return ret, nil
  136. }
  137. // -- Result of a flush request.
  138. type shardsInfo struct {
  139. Total int `json:"total"`
  140. Successful int `json:"successful"`
  141. Failed int `json:"failed"`
  142. }
  143. type FlushResult struct {
  144. Shards shardsInfo `json:"_shards"`
  145. }