indices_refresh.go 2.1 KB

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