You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

indices_forcemerge.go 6.9KB

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