indices_refresh.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. // RefreshService explicitly refreshes one or more indices.
  13. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-refresh.html.
  14. type RefreshService struct {
  15. client *Client
  16. index []string
  17. force *bool
  18. pretty bool
  19. }
  20. // NewRefreshService creates a new instance of RefreshService.
  21. func NewRefreshService(client *Client) *RefreshService {
  22. builder := &RefreshService{
  23. client: client,
  24. }
  25. return builder
  26. }
  27. // Index specifies the indices to refresh.
  28. func (s *RefreshService) Index(index ...string) *RefreshService {
  29. s.index = append(s.index, index...)
  30. return s
  31. }
  32. // Force forces a refresh.
  33. func (s *RefreshService) Force(force bool) *RefreshService {
  34. s.force = &force
  35. return s
  36. }
  37. // Pretty asks Elasticsearch to return indented JSON.
  38. func (s *RefreshService) Pretty(pretty bool) *RefreshService {
  39. s.pretty = pretty
  40. return s
  41. }
  42. // buildURL builds the URL for the operation.
  43. func (s *RefreshService) buildURL() (string, url.Values, error) {
  44. var err error
  45. var path string
  46. if len(s.index) > 0 {
  47. path, err = uritemplates.Expand("/{index}/_refresh", map[string]string{
  48. "index": strings.Join(s.index, ","),
  49. })
  50. } else {
  51. path = "/_refresh"
  52. }
  53. if err != nil {
  54. return "", url.Values{}, err
  55. }
  56. // Add query string parameters
  57. params := url.Values{}
  58. if s.force != nil {
  59. params.Set("force", fmt.Sprintf("%v", *s.force))
  60. }
  61. if s.pretty {
  62. params.Set("pretty", fmt.Sprintf("%v", s.pretty))
  63. }
  64. return path, params, nil
  65. }
  66. // Do executes the request.
  67. func (s *RefreshService) Do(ctx context.Context) (*RefreshResult, error) {
  68. path, params, err := s.buildURL()
  69. if err != nil {
  70. return nil, err
  71. }
  72. // Get response
  73. res, err := s.client.PerformRequest(ctx, "POST", path, params, nil)
  74. if err != nil {
  75. return nil, err
  76. }
  77. // Return result
  78. ret := new(RefreshResult)
  79. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  80. return nil, err
  81. }
  82. return ret, nil
  83. }
  84. // -- Result of a refresh request.
  85. // RefreshResult is the outcome of RefreshService.Do.
  86. type RefreshResult struct {
  87. Shards shardsInfo `json:"_shards,omitempty"`
  88. }