optimize.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2014 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/http"
  9. "net/url"
  10. "strings"
  11. "github.com/olivere/elastic/uritemplates"
  12. )
  13. type OptimizeService struct {
  14. client *Client
  15. indices []string
  16. maxNumSegments *int
  17. onlyExpungeDeletes *bool
  18. flush *bool
  19. waitForMerge *bool
  20. force *bool
  21. pretty bool
  22. debug bool
  23. }
  24. func NewOptimizeService(client *Client) *OptimizeService {
  25. builder := &OptimizeService{
  26. client: client,
  27. indices: make([]string, 0),
  28. }
  29. return builder
  30. }
  31. func (s *OptimizeService) Index(index string) *OptimizeService {
  32. s.indices = append(s.indices, index)
  33. return s
  34. }
  35. func (s *OptimizeService) Indices(indices ...string) *OptimizeService {
  36. s.indices = append(s.indices, indices...)
  37. return s
  38. }
  39. func (s *OptimizeService) MaxNumSegments(maxNumSegments int) *OptimizeService {
  40. s.maxNumSegments = &maxNumSegments
  41. return s
  42. }
  43. func (s *OptimizeService) OnlyExpungeDeletes(onlyExpungeDeletes bool) *OptimizeService {
  44. s.onlyExpungeDeletes = &onlyExpungeDeletes
  45. return s
  46. }
  47. func (s *OptimizeService) Flush(flush bool) *OptimizeService {
  48. s.flush = &flush
  49. return s
  50. }
  51. func (s *OptimizeService) WaitForMerge(waitForMerge bool) *OptimizeService {
  52. s.waitForMerge = &waitForMerge
  53. return s
  54. }
  55. func (s *OptimizeService) Force(force bool) *OptimizeService {
  56. s.force = &force
  57. return s
  58. }
  59. func (s *OptimizeService) Pretty(pretty bool) *OptimizeService {
  60. s.pretty = pretty
  61. return s
  62. }
  63. func (s *OptimizeService) Debug(debug bool) *OptimizeService {
  64. s.debug = debug
  65. return s
  66. }
  67. func (s *OptimizeService) Do() (*OptimizeResult, error) {
  68. // Build url
  69. urls := "/"
  70. // Indices part
  71. indexPart := make([]string, 0)
  72. for _, index := range s.indices {
  73. index, err := uritemplates.Expand("{index}", map[string]string{
  74. "index": index,
  75. })
  76. if err != nil {
  77. return nil, err
  78. }
  79. indexPart = append(indexPart, index)
  80. }
  81. if len(indexPart) > 0 {
  82. urls += strings.Join(indexPart, ",")
  83. }
  84. urls += "/_optimize"
  85. // Parameters
  86. params := make(url.Values)
  87. if s.maxNumSegments != nil {
  88. params.Set("max_num_segments", fmt.Sprintf("%d", *s.maxNumSegments))
  89. }
  90. if s.onlyExpungeDeletes != nil {
  91. params.Set("only_expunge_deletes", fmt.Sprintf("%v", *s.onlyExpungeDeletes))
  92. }
  93. if s.flush != nil {
  94. params.Set("flush", fmt.Sprintf("%v", *s.flush))
  95. }
  96. if s.waitForMerge != nil {
  97. params.Set("wait_for_merge", fmt.Sprintf("%v", *s.waitForMerge))
  98. }
  99. if s.force != nil {
  100. params.Set("force", fmt.Sprintf("%v", *s.force))
  101. }
  102. if s.pretty {
  103. params.Set("pretty", fmt.Sprintf("%v", s.pretty))
  104. }
  105. if len(params) > 0 {
  106. urls += "?" + params.Encode()
  107. }
  108. // Set up a new request
  109. req, err := s.client.NewRequest("POST", urls)
  110. if err != nil {
  111. return nil, err
  112. }
  113. if s.debug {
  114. s.client.dumpRequest((*http.Request)(req))
  115. }
  116. // Get response
  117. res, err := s.client.c.Do((*http.Request)(req))
  118. if err != nil {
  119. return nil, err
  120. }
  121. if err := checkResponse(res); err != nil {
  122. return nil, err
  123. }
  124. defer res.Body.Close()
  125. if s.debug {
  126. s.client.dumpResponse(res)
  127. }
  128. ret := new(OptimizeResult)
  129. if err := json.NewDecoder(res.Body).Decode(ret); err != nil {
  130. return nil, err
  131. }
  132. return ret, nil
  133. }
  134. // -- Result of an optimize request.
  135. type OptimizeResult struct {
  136. Shards shardsInfo `json:"_shards,omitempty"`
  137. }