indices_forcemerge.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. // IndicesForcemergeService allows to force merging of one or more indices.
  13. // The merge relates to the number of segments a Lucene index holds
  14. // within each shard. The force merge operation allows to reduce the number
  15. // of segments by merging them.
  16. //
  17. // See http://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-forcemerge.html
  18. // for more information.
  19. type IndicesForcemergeService struct {
  20. client *Client
  21. pretty bool
  22. index []string
  23. allowNoIndices *bool
  24. expandWildcards string
  25. flush *bool
  26. ignoreUnavailable *bool
  27. maxNumSegments interface{}
  28. onlyExpungeDeletes *bool
  29. operationThreading interface{}
  30. }
  31. // NewIndicesForcemergeService creates a new IndicesForcemergeService.
  32. func NewIndicesForcemergeService(client *Client) *IndicesForcemergeService {
  33. return &IndicesForcemergeService{
  34. client: client,
  35. index: make([]string, 0),
  36. }
  37. }
  38. // Index is a list of index names; use `_all` or empty string to perform
  39. // the operation on all indices.
  40. func (s *IndicesForcemergeService) Index(index ...string) *IndicesForcemergeService {
  41. if s.index == nil {
  42. s.index = make([]string, 0)
  43. }
  44. s.index = append(s.index, index...)
  45. return s
  46. }
  47. // AllowNoIndices indicates whether to ignore if a wildcard indices
  48. // expression resolves into no concrete indices.
  49. // (This includes `_all` string or when no indices have been specified).
  50. func (s *IndicesForcemergeService) AllowNoIndices(allowNoIndices bool) *IndicesForcemergeService {
  51. s.allowNoIndices = &allowNoIndices
  52. return s
  53. }
  54. // ExpandWildcards indicates whether to expand wildcard expression to
  55. // concrete indices that are open, closed or both..
  56. func (s *IndicesForcemergeService) ExpandWildcards(expandWildcards string) *IndicesForcemergeService {
  57. s.expandWildcards = expandWildcards
  58. return s
  59. }
  60. // Flush specifies whether the index should be flushed after performing
  61. // the operation (default: true).
  62. func (s *IndicesForcemergeService) Flush(flush bool) *IndicesForcemergeService {
  63. s.flush = &flush
  64. return s
  65. }
  66. // IgnoreUnavailable indicates whether specified concrete indices should
  67. // be ignored when unavailable (missing or closed).
  68. func (s *IndicesForcemergeService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesForcemergeService {
  69. s.ignoreUnavailable = &ignoreUnavailable
  70. return s
  71. }
  72. // MaxNumSegments specifies the number of segments the index should be
  73. // merged into (default: dynamic).
  74. func (s *IndicesForcemergeService) MaxNumSegments(maxNumSegments interface{}) *IndicesForcemergeService {
  75. s.maxNumSegments = maxNumSegments
  76. return s
  77. }
  78. // OnlyExpungeDeletes specifies whether the operation should only expunge
  79. // deleted documents.
  80. func (s *IndicesForcemergeService) OnlyExpungeDeletes(onlyExpungeDeletes bool) *IndicesForcemergeService {
  81. s.onlyExpungeDeletes = &onlyExpungeDeletes
  82. return s
  83. }
  84. func (s *IndicesForcemergeService) OperationThreading(operationThreading interface{}) *IndicesForcemergeService {
  85. s.operationThreading = operationThreading
  86. return s
  87. }
  88. // Pretty indicates that the JSON response be indented and human readable.
  89. func (s *IndicesForcemergeService) Pretty(pretty bool) *IndicesForcemergeService {
  90. s.pretty = pretty
  91. return s
  92. }
  93. // buildURL builds the URL for the operation.
  94. func (s *IndicesForcemergeService) buildURL() (string, url.Values, error) {
  95. var err error
  96. var path string
  97. // Build URL
  98. if len(s.index) > 0 {
  99. path, err = uritemplates.Expand("/{index}/_forcemerge", map[string]string{
  100. "index": strings.Join(s.index, ","),
  101. })
  102. } else {
  103. path = "/_forcemerge"
  104. }
  105. if err != nil {
  106. return "", url.Values{}, err
  107. }
  108. // Add query string parameters
  109. params := url.Values{}
  110. if s.pretty {
  111. params.Set("pretty", "1")
  112. }
  113. if s.allowNoIndices != nil {
  114. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  115. }
  116. if s.expandWildcards != "" {
  117. params.Set("expand_wildcards", s.expandWildcards)
  118. }
  119. if s.flush != nil {
  120. params.Set("flush", fmt.Sprintf("%v", *s.flush))
  121. }
  122. if s.ignoreUnavailable != nil {
  123. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  124. }
  125. if s.maxNumSegments != nil {
  126. params.Set("max_num_segments", fmt.Sprintf("%v", s.maxNumSegments))
  127. }
  128. if s.onlyExpungeDeletes != nil {
  129. params.Set("only_expunge_deletes", fmt.Sprintf("%v", *s.onlyExpungeDeletes))
  130. }
  131. if s.operationThreading != nil {
  132. params.Set("operation_threading", fmt.Sprintf("%v", s.operationThreading))
  133. }
  134. return path, params, nil
  135. }
  136. // Validate checks if the operation is valid.
  137. func (s *IndicesForcemergeService) Validate() error {
  138. return nil
  139. }
  140. // Do executes the operation.
  141. func (s *IndicesForcemergeService) Do(ctx context.Context) (*IndicesForcemergeResponse, error) {
  142. // Check pre-conditions
  143. if err := s.Validate(); err != nil {
  144. return nil, err
  145. }
  146. // Get URL for request
  147. path, params, err := s.buildURL()
  148. if err != nil {
  149. return nil, err
  150. }
  151. // Get HTTP response
  152. res, err := s.client.PerformRequest(ctx, "POST", path, params, nil)
  153. if err != nil {
  154. return nil, err
  155. }
  156. // Return operation response
  157. ret := new(IndicesForcemergeResponse)
  158. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  159. return nil, err
  160. }
  161. return ret, nil
  162. }
  163. // IndicesForcemergeResponse is the response of IndicesForcemergeService.Do.
  164. type IndicesForcemergeResponse struct {
  165. Shards *ShardsInfo `json:"_shards"`
  166. }