put_template.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. "gopkg.in/olivere/elastic.v5/uritemplates"
  10. )
  11. // PutTemplateService creates or updates a search template.
  12. // The documentation can be found at
  13. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-template.html.
  14. type PutTemplateService struct {
  15. client *Client
  16. pretty bool
  17. id string
  18. opType string
  19. version *int
  20. versionType string
  21. bodyJson interface{}
  22. bodyString string
  23. }
  24. // NewPutTemplateService creates a new PutTemplateService.
  25. func NewPutTemplateService(client *Client) *PutTemplateService {
  26. return &PutTemplateService{
  27. client: client,
  28. }
  29. }
  30. // Id is the template ID.
  31. func (s *PutTemplateService) Id(id string) *PutTemplateService {
  32. s.id = id
  33. return s
  34. }
  35. // OpType is an explicit operation type.
  36. func (s *PutTemplateService) OpType(opType string) *PutTemplateService {
  37. s.opType = opType
  38. return s
  39. }
  40. // Version is an explicit version number for concurrency control.
  41. func (s *PutTemplateService) Version(version int) *PutTemplateService {
  42. s.version = &version
  43. return s
  44. }
  45. // VersionType is a specific version type.
  46. func (s *PutTemplateService) VersionType(versionType string) *PutTemplateService {
  47. s.versionType = versionType
  48. return s
  49. }
  50. // BodyJson is the document as a JSON serializable object.
  51. func (s *PutTemplateService) BodyJson(body interface{}) *PutTemplateService {
  52. s.bodyJson = body
  53. return s
  54. }
  55. // BodyString is the document as a string.
  56. func (s *PutTemplateService) BodyString(body string) *PutTemplateService {
  57. s.bodyString = body
  58. return s
  59. }
  60. // buildURL builds the URL for the operation.
  61. func (s *PutTemplateService) buildURL() (string, url.Values, error) {
  62. // Build URL
  63. path, err := uritemplates.Expand("/_search/template/{id}", map[string]string{
  64. "id": s.id,
  65. })
  66. if err != nil {
  67. return "", url.Values{}, err
  68. }
  69. // Add query string parameters
  70. params := url.Values{}
  71. if s.version != nil {
  72. params.Set("version", fmt.Sprintf("%d", *s.version))
  73. }
  74. if s.versionType != "" {
  75. params.Set("version_type", s.versionType)
  76. }
  77. if s.opType != "" {
  78. params.Set("op_type", s.opType)
  79. }
  80. return path, params, nil
  81. }
  82. // Validate checks if the operation is valid.
  83. func (s *PutTemplateService) Validate() error {
  84. var invalid []string
  85. if s.id == "" {
  86. invalid = append(invalid, "Id")
  87. }
  88. if s.bodyString == "" && s.bodyJson == nil {
  89. invalid = append(invalid, "BodyJson")
  90. }
  91. if len(invalid) > 0 {
  92. return fmt.Errorf("missing required fields: %v", invalid)
  93. }
  94. return nil
  95. }
  96. // Do executes the operation.
  97. func (s *PutTemplateService) Do(ctx context.Context) (*AcknowledgedResponse, error) {
  98. // Check pre-conditions
  99. if err := s.Validate(); err != nil {
  100. return nil, err
  101. }
  102. // Get URL for request
  103. path, params, err := s.buildURL()
  104. if err != nil {
  105. return nil, err
  106. }
  107. // Setup HTTP request body
  108. var body interface{}
  109. if s.bodyJson != nil {
  110. body = s.bodyJson
  111. } else {
  112. body = s.bodyString
  113. }
  114. // Get HTTP response
  115. res, err := s.client.PerformRequest(ctx, "PUT", path, params, body)
  116. if err != nil {
  117. return nil, err
  118. }
  119. // Return operation response
  120. ret := new(AcknowledgedResponse)
  121. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  122. return nil, err
  123. }
  124. return ret, nil
  125. }