optimize.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. type OptimizeService struct {
  13. client *Client
  14. indices []string
  15. maxNumSegments *int
  16. onlyExpungeDeletes *bool
  17. flush *bool
  18. waitForMerge *bool
  19. force *bool
  20. pretty bool
  21. }
  22. func NewOptimizeService(client *Client) *OptimizeService {
  23. builder := &OptimizeService{
  24. client: client,
  25. indices: make([]string, 0),
  26. }
  27. return builder
  28. }
  29. func (s *OptimizeService) Index(index string) *OptimizeService {
  30. s.indices = append(s.indices, index)
  31. return s
  32. }
  33. func (s *OptimizeService) Indices(indices ...string) *OptimizeService {
  34. s.indices = append(s.indices, indices...)
  35. return s
  36. }
  37. func (s *OptimizeService) MaxNumSegments(maxNumSegments int) *OptimizeService {
  38. s.maxNumSegments = &maxNumSegments
  39. return s
  40. }
  41. func (s *OptimizeService) OnlyExpungeDeletes(onlyExpungeDeletes bool) *OptimizeService {
  42. s.onlyExpungeDeletes = &onlyExpungeDeletes
  43. return s
  44. }
  45. func (s *OptimizeService) Flush(flush bool) *OptimizeService {
  46. s.flush = &flush
  47. return s
  48. }
  49. func (s *OptimizeService) WaitForMerge(waitForMerge bool) *OptimizeService {
  50. s.waitForMerge = &waitForMerge
  51. return s
  52. }
  53. func (s *OptimizeService) Force(force bool) *OptimizeService {
  54. s.force = &force
  55. return s
  56. }
  57. func (s *OptimizeService) Pretty(pretty bool) *OptimizeService {
  58. s.pretty = pretty
  59. return s
  60. }
  61. func (s *OptimizeService) Do() (*OptimizeResult, error) {
  62. // Build url
  63. path := "/"
  64. // Indices part
  65. indexPart := make([]string, 0)
  66. for _, index := range s.indices {
  67. index, err := uritemplates.Expand("{index}", map[string]string{
  68. "index": index,
  69. })
  70. if err != nil {
  71. return nil, err
  72. }
  73. indexPart = append(indexPart, index)
  74. }
  75. if len(indexPart) > 0 {
  76. path += strings.Join(indexPart, ",")
  77. }
  78. path += "/_optimize"
  79. // Parameters
  80. params := make(url.Values)
  81. if s.maxNumSegments != nil {
  82. params.Set("max_num_segments", fmt.Sprintf("%d", *s.maxNumSegments))
  83. }
  84. if s.onlyExpungeDeletes != nil {
  85. params.Set("only_expunge_deletes", fmt.Sprintf("%v", *s.onlyExpungeDeletes))
  86. }
  87. if s.flush != nil {
  88. params.Set("flush", fmt.Sprintf("%v", *s.flush))
  89. }
  90. if s.waitForMerge != nil {
  91. params.Set("wait_for_merge", fmt.Sprintf("%v", *s.waitForMerge))
  92. }
  93. if s.force != nil {
  94. params.Set("force", fmt.Sprintf("%v", *s.force))
  95. }
  96. if s.pretty {
  97. params.Set("pretty", fmt.Sprintf("%v", s.pretty))
  98. }
  99. // Get response
  100. res, err := s.client.PerformRequest("POST", path, params, nil)
  101. if err != nil {
  102. return nil, err
  103. }
  104. // Return result
  105. ret := new(OptimizeResult)
  106. if err := json.Unmarshal(res.Body, ret); err != nil {
  107. return nil, err
  108. }
  109. return ret, nil
  110. }
  111. // -- Result of an optimize request.
  112. type OptimizeResult struct {
  113. Shards shardsInfo `json:"_shards,omitempty"`
  114. }