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