Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

script_put.go 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/http"
  9. "net/url"
  10. "strings"
  11. "github.com/olivere/elastic/v7/uritemplates"
  12. )
  13. // PutScriptService adds or updates a stored script in Elasticsearch.
  14. //
  15. // See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/modules-scripting.html
  16. // for details.
  17. type PutScriptService struct {
  18. client *Client
  19. pretty *bool // pretty format the returned JSON response
  20. human *bool // return human readable values for statistics
  21. errorTrace *bool // include the stack trace of returned errors
  22. filterPath []string // list of filters used to reduce the response
  23. headers http.Header // custom request-level HTTP headers
  24. id string
  25. context string
  26. timeout string
  27. masterTimeout string
  28. bodyJson interface{}
  29. bodyString string
  30. }
  31. // NewPutScriptService creates a new PutScriptService.
  32. func NewPutScriptService(client *Client) *PutScriptService {
  33. return &PutScriptService{
  34. client: client,
  35. }
  36. }
  37. // Pretty tells Elasticsearch whether to return a formatted JSON response.
  38. func (s *PutScriptService) Pretty(pretty bool) *PutScriptService {
  39. s.pretty = &pretty
  40. return s
  41. }
  42. // Human specifies whether human readable values should be returned in
  43. // the JSON response, e.g. "7.5mb".
  44. func (s *PutScriptService) Human(human bool) *PutScriptService {
  45. s.human = &human
  46. return s
  47. }
  48. // ErrorTrace specifies whether to include the stack trace of returned errors.
  49. func (s *PutScriptService) ErrorTrace(errorTrace bool) *PutScriptService {
  50. s.errorTrace = &errorTrace
  51. return s
  52. }
  53. // FilterPath specifies a list of filters used to reduce the response.
  54. func (s *PutScriptService) FilterPath(filterPath ...string) *PutScriptService {
  55. s.filterPath = filterPath
  56. return s
  57. }
  58. // Header adds a header to the request.
  59. func (s *PutScriptService) Header(name string, value string) *PutScriptService {
  60. if s.headers == nil {
  61. s.headers = http.Header{}
  62. }
  63. s.headers.Add(name, value)
  64. return s
  65. }
  66. // Headers specifies the headers of the request.
  67. func (s *PutScriptService) Headers(headers http.Header) *PutScriptService {
  68. s.headers = headers
  69. return s
  70. }
  71. // Id is the script ID.
  72. func (s *PutScriptService) Id(id string) *PutScriptService {
  73. s.id = id
  74. return s
  75. }
  76. // Context specifies the script context (optional).
  77. func (s *PutScriptService) Context(context string) *PutScriptService {
  78. s.context = context
  79. return s
  80. }
  81. // Timeout is an explicit operation timeout.
  82. func (s *PutScriptService) Timeout(timeout string) *PutScriptService {
  83. s.timeout = timeout
  84. return s
  85. }
  86. // MasterTimeout is the timeout for connecting to master.
  87. func (s *PutScriptService) MasterTimeout(masterTimeout string) *PutScriptService {
  88. s.masterTimeout = masterTimeout
  89. return s
  90. }
  91. // BodyJson is the document as a serializable JSON interface.
  92. func (s *PutScriptService) BodyJson(body interface{}) *PutScriptService {
  93. s.bodyJson = body
  94. return s
  95. }
  96. // BodyString is the document encoded as a string.
  97. func (s *PutScriptService) BodyString(body string) *PutScriptService {
  98. s.bodyString = body
  99. return s
  100. }
  101. // buildURL builds the URL for the operation.
  102. func (s *PutScriptService) buildURL() (string, string, url.Values, error) {
  103. var (
  104. err error
  105. method = "PUT"
  106. path string
  107. )
  108. if s.context != "" {
  109. path, err = uritemplates.Expand("/_scripts/{id}/{context}", map[string]string{
  110. "id": s.id,
  111. "context": s.context,
  112. })
  113. } else {
  114. path, err = uritemplates.Expand("/_scripts/{id}", map[string]string{
  115. "id": s.id,
  116. })
  117. }
  118. if err != nil {
  119. return "", "", url.Values{}, err
  120. }
  121. // Add query string parameters
  122. params := url.Values{}
  123. if v := s.pretty; v != nil {
  124. params.Set("pretty", fmt.Sprint(*v))
  125. }
  126. if v := s.human; v != nil {
  127. params.Set("human", fmt.Sprint(*v))
  128. }
  129. if v := s.errorTrace; v != nil {
  130. params.Set("error_trace", fmt.Sprint(*v))
  131. }
  132. if len(s.filterPath) > 0 {
  133. params.Set("filter_path", strings.Join(s.filterPath, ","))
  134. }
  135. if s.timeout != "" {
  136. params.Set("timeout", s.timeout)
  137. }
  138. if s.masterTimeout != "" {
  139. params.Set("master_timestamp", s.masterTimeout)
  140. }
  141. return method, path, params, nil
  142. }
  143. // Validate checks if the operation is valid.
  144. func (s *PutScriptService) Validate() error {
  145. var invalid []string
  146. if s.id == "" {
  147. invalid = append(invalid, "Id")
  148. }
  149. if s.bodyString == "" && s.bodyJson == nil {
  150. invalid = append(invalid, "BodyJson")
  151. }
  152. if len(invalid) > 0 {
  153. return fmt.Errorf("missing required fields: %v", invalid)
  154. }
  155. return nil
  156. }
  157. // Do executes the operation.
  158. func (s *PutScriptService) Do(ctx context.Context) (*PutScriptResponse, error) {
  159. // Check pre-conditions
  160. if err := s.Validate(); err != nil {
  161. return nil, err
  162. }
  163. // Get URL for request
  164. method, path, params, err := s.buildURL()
  165. if err != nil {
  166. return nil, err
  167. }
  168. // Setup HTTP request body
  169. var body interface{}
  170. if s.bodyJson != nil {
  171. body = s.bodyJson
  172. } else {
  173. body = s.bodyString
  174. }
  175. // Get HTTP response
  176. res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
  177. Method: method,
  178. Path: path,
  179. Params: params,
  180. Body: body,
  181. Headers: s.headers,
  182. })
  183. if err != nil {
  184. return nil, err
  185. }
  186. // Return operation response
  187. ret := new(PutScriptResponse)
  188. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  189. return nil, err
  190. }
  191. return ret, nil
  192. }
  193. // PutScriptResponse is the result of saving a stored script
  194. // in Elasticsearch.
  195. type PutScriptResponse struct {
  196. AcknowledgedResponse
  197. }