clear_scroll.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. )
  10. // ClearScrollService clears one or more scroll contexts by their ids.
  11. //
  12. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-request-scroll.html#_clear_scroll_api
  13. // for details.
  14. type ClearScrollService struct {
  15. client *Client
  16. pretty bool
  17. scrollId []string
  18. }
  19. // NewClearScrollService creates a new ClearScrollService.
  20. func NewClearScrollService(client *Client) *ClearScrollService {
  21. return &ClearScrollService{
  22. client: client,
  23. scrollId: make([]string, 0),
  24. }
  25. }
  26. // ScrollId is a list of scroll IDs to clear.
  27. // Use _all to clear all search contexts.
  28. func (s *ClearScrollService) ScrollId(scrollIds ...string) *ClearScrollService {
  29. s.scrollId = append(s.scrollId, scrollIds...)
  30. return s
  31. }
  32. // Pretty indicates that the JSON response be indented and human readable.
  33. func (s *ClearScrollService) Pretty(pretty bool) *ClearScrollService {
  34. s.pretty = pretty
  35. return s
  36. }
  37. // buildURL builds the URL for the operation.
  38. func (s *ClearScrollService) buildURL() (string, url.Values, error) {
  39. // Build URL
  40. path := "/_search/scroll/"
  41. // Add query string parameters
  42. params := url.Values{}
  43. if s.pretty {
  44. params.Set("pretty", "1")
  45. }
  46. return path, params, nil
  47. }
  48. // Validate checks if the operation is valid.
  49. func (s *ClearScrollService) Validate() error {
  50. var invalid []string
  51. if len(s.scrollId) == 0 {
  52. invalid = append(invalid, "ScrollId")
  53. }
  54. if len(invalid) > 0 {
  55. return fmt.Errorf("missing required fields: %v", invalid)
  56. }
  57. return nil
  58. }
  59. // Do executes the operation.
  60. func (s *ClearScrollService) Do(ctx context.Context) (*ClearScrollResponse, error) {
  61. // Check pre-conditions
  62. if err := s.Validate(); err != nil {
  63. return nil, err
  64. }
  65. // Get URL for request
  66. path, params, err := s.buildURL()
  67. if err != nil {
  68. return nil, err
  69. }
  70. // Setup HTTP request body
  71. body := map[string][]string{
  72. "scroll_id": s.scrollId,
  73. }
  74. // Get HTTP response
  75. res, err := s.client.PerformRequest(ctx, "DELETE", path, params, body)
  76. if err != nil {
  77. return nil, err
  78. }
  79. // Return operation response
  80. ret := new(ClearScrollResponse)
  81. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  82. return nil, err
  83. }
  84. return ret, nil
  85. }
  86. // ClearScrollResponse is the response of ClearScrollService.Do.
  87. type ClearScrollResponse struct {
  88. }