refresh.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 RefreshService struct {
  13. client *Client
  14. indices []string
  15. force *bool
  16. pretty bool
  17. }
  18. func NewRefreshService(client *Client) *RefreshService {
  19. builder := &RefreshService{
  20. client: client,
  21. indices: make([]string, 0),
  22. }
  23. return builder
  24. }
  25. func (s *RefreshService) Index(index string) *RefreshService {
  26. s.indices = append(s.indices, index)
  27. return s
  28. }
  29. func (s *RefreshService) Indices(indices ...string) *RefreshService {
  30. s.indices = append(s.indices, indices...)
  31. return s
  32. }
  33. func (s *RefreshService) Force(force bool) *RefreshService {
  34. s.force = &force
  35. return s
  36. }
  37. func (s *RefreshService) Pretty(pretty bool) *RefreshService {
  38. s.pretty = pretty
  39. return s
  40. }
  41. func (s *RefreshService) Do() (*RefreshResult, error) {
  42. // Build url
  43. path := "/"
  44. // Indices part
  45. indexPart := make([]string, 0)
  46. for _, index := range s.indices {
  47. index, err := uritemplates.Expand("{index}", map[string]string{
  48. "index": index,
  49. })
  50. if err != nil {
  51. return nil, err
  52. }
  53. indexPart = append(indexPart, index)
  54. }
  55. if len(indexPart) > 0 {
  56. path += strings.Join(indexPart, ",")
  57. }
  58. path += "/_refresh"
  59. // Parameters
  60. params := make(url.Values)
  61. if s.force != nil {
  62. params.Set("force", fmt.Sprintf("%v", *s.force))
  63. }
  64. if s.pretty {
  65. params.Set("pretty", fmt.Sprintf("%v", s.pretty))
  66. }
  67. // Get response
  68. res, err := s.client.PerformRequest("POST", path, params, nil)
  69. if err != nil {
  70. return nil, err
  71. }
  72. // Return result
  73. ret := new(RefreshResult)
  74. if err := json.Unmarshal(res.Body, ret); err != nil {
  75. return nil, err
  76. }
  77. return ret, nil
  78. }
  79. // -- Result of a refresh request.
  80. type RefreshResult struct {
  81. Shards shardsInfo `json:"_shards,omitempty"`
  82. }