refresh.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 RefreshService struct {
  14. client *Client
  15. indices []string
  16. force *bool
  17. pretty bool
  18. debug bool
  19. }
  20. func NewRefreshService(client *Client) *RefreshService {
  21. builder := &RefreshService{
  22. client: client,
  23. indices: make([]string, 0),
  24. }
  25. return builder
  26. }
  27. func (s *RefreshService) Index(index string) *RefreshService {
  28. s.indices = append(s.indices, index)
  29. return s
  30. }
  31. func (s *RefreshService) Indices(indices ...string) *RefreshService {
  32. s.indices = append(s.indices, indices...)
  33. return s
  34. }
  35. func (s *RefreshService) Force(force bool) *RefreshService {
  36. s.force = &force
  37. return s
  38. }
  39. func (s *RefreshService) Pretty(pretty bool) *RefreshService {
  40. s.pretty = pretty
  41. return s
  42. }
  43. func (s *RefreshService) Debug(debug bool) *RefreshService {
  44. s.debug = debug
  45. return s
  46. }
  47. func (s *RefreshService) Do() (*RefreshResult, error) {
  48. // Build url
  49. urls := "/"
  50. // Indices part
  51. indexPart := make([]string, 0)
  52. for _, index := range s.indices {
  53. index, err := uritemplates.Expand("{index}", map[string]string{
  54. "index": index,
  55. })
  56. if err != nil {
  57. return nil, err
  58. }
  59. indexPart = append(indexPart, index)
  60. }
  61. if len(indexPart) > 0 {
  62. urls += strings.Join(indexPart, ",")
  63. }
  64. urls += "/_refresh"
  65. // Parameters
  66. params := make(url.Values)
  67. if s.force != nil {
  68. params.Set("force", fmt.Sprintf("%v", *s.force))
  69. }
  70. if s.pretty {
  71. params.Set("pretty", fmt.Sprintf("%v", s.pretty))
  72. }
  73. if len(params) > 0 {
  74. urls += "?" + params.Encode()
  75. }
  76. // Set up a new request
  77. req, err := s.client.NewRequest("POST", urls)
  78. if err != nil {
  79. return nil, err
  80. }
  81. if s.debug {
  82. s.client.dumpRequest((*http.Request)(req))
  83. }
  84. // Get response
  85. res, err := s.client.c.Do((*http.Request)(req))
  86. if err != nil {
  87. return nil, err
  88. }
  89. if err := checkResponse(res); err != nil {
  90. return nil, err
  91. }
  92. defer res.Body.Close()
  93. if s.debug {
  94. s.client.dumpResponse(res)
  95. }
  96. ret := new(RefreshResult)
  97. if err := json.NewDecoder(res.Body).Decode(ret); err != nil {
  98. return nil, err
  99. }
  100. return ret, nil
  101. }
  102. // -- Result of a refresh request.
  103. type RefreshResult struct {
  104. Shards shardsInfo `json:"_shards,omitempty"`
  105. }