clear_scroll.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "log"
  9. "net/url"
  10. "strings"
  11. "github.com/olivere/elastic/uritemplates"
  12. )
  13. var (
  14. _ = fmt.Print
  15. _ = log.Print
  16. _ = strings.Index
  17. _ = uritemplates.Expand
  18. _ = url.Parse
  19. )
  20. // ClearScrollService is documented at http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.4/search-request-scroll.html.
  21. type ClearScrollService struct {
  22. client *Client
  23. pretty bool
  24. scrollId []string
  25. bodyJson interface{}
  26. bodyString string
  27. }
  28. // NewClearScrollService creates a new ClearScrollService.
  29. func NewClearScrollService(client *Client) *ClearScrollService {
  30. return &ClearScrollService{
  31. client: client,
  32. scrollId: make([]string, 0),
  33. }
  34. }
  35. // ScrollId is a list of scroll IDs to clear.
  36. // Use _all to clear all search contexts.
  37. func (s *ClearScrollService) ScrollId(scrollId ...string) *ClearScrollService {
  38. s.scrollId = make([]string, 0)
  39. s.scrollId = append(s.scrollId, scrollId...)
  40. return s
  41. }
  42. // buildURL builds the URL for the operation.
  43. func (s *ClearScrollService) buildURL() (string, url.Values, error) {
  44. path, err := uritemplates.Expand("/_search/scroll", map[string]string{})
  45. if err != nil {
  46. return "", url.Values{}, err
  47. }
  48. return path, url.Values{}, nil
  49. }
  50. // Validate checks if the operation is valid.
  51. func (s *ClearScrollService) Validate() error {
  52. return nil
  53. }
  54. // Do executes the operation.
  55. func (s *ClearScrollService) Do() (*ClearScrollResponse, error) {
  56. // Check pre-conditions
  57. if err := s.Validate(); err != nil {
  58. return nil, err
  59. }
  60. // Get URL for request
  61. path, params, err := s.buildURL()
  62. if err != nil {
  63. return nil, err
  64. }
  65. // Setup HTTP request body
  66. body := strings.Join(s.scrollId, ",")
  67. // Get HTTP response
  68. res, err := s.client.PerformRequest("DELETE", path, params, body)
  69. if err != nil {
  70. return nil, err
  71. }
  72. // Return operation response
  73. ret := new(ClearScrollResponse)
  74. if err := json.Unmarshal(res.Body, ret); err != nil {
  75. return nil, err
  76. }
  77. return ret, nil
  78. }
  79. // ClearScrollResponse is the response of ClearScrollService.Do.
  80. type ClearScrollResponse struct {
  81. }