get_template.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // GetTemplateService reads a search template.
  12. // It is documented at https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-template.html.
  13. type GetTemplateService struct {
  14. client *Client
  15. pretty bool
  16. id string
  17. version interface{}
  18. versionType string
  19. }
  20. // NewGetTemplateService creates a new GetTemplateService.
  21. func NewGetTemplateService(client *Client) *GetTemplateService {
  22. return &GetTemplateService{
  23. client: client,
  24. }
  25. }
  26. // Id is the template ID.
  27. func (s *GetTemplateService) Id(id string) *GetTemplateService {
  28. s.id = id
  29. return s
  30. }
  31. // Version is an explicit version number for concurrency control.
  32. func (s *GetTemplateService) Version(version interface{}) *GetTemplateService {
  33. s.version = version
  34. return s
  35. }
  36. // VersionType is a specific version type.
  37. func (s *GetTemplateService) VersionType(versionType string) *GetTemplateService {
  38. s.versionType = versionType
  39. return s
  40. }
  41. // buildURL builds the URL for the operation.
  42. func (s *GetTemplateService) buildURL() (string, url.Values, error) {
  43. // Build URL
  44. path, err := uritemplates.Expand("/_search/template/{id}", map[string]string{
  45. "id": s.id,
  46. })
  47. if err != nil {
  48. return "", url.Values{}, err
  49. }
  50. // Add query string parameters
  51. params := url.Values{}
  52. if s.version != nil {
  53. params.Set("version", fmt.Sprintf("%v", s.version))
  54. }
  55. if s.versionType != "" {
  56. params.Set("version_type", s.versionType)
  57. }
  58. return path, params, nil
  59. }
  60. // Validate checks if the operation is valid.
  61. func (s *GetTemplateService) Validate() error {
  62. var invalid []string
  63. if s.id == "" {
  64. invalid = append(invalid, "Id")
  65. }
  66. if len(invalid) > 0 {
  67. return fmt.Errorf("missing required fields: %v", invalid)
  68. }
  69. return nil
  70. }
  71. // Do executes the operation and returns the template.
  72. func (s *GetTemplateService) Do(ctx context.Context) (*GetTemplateResponse, error) {
  73. // Check pre-conditions
  74. if err := s.Validate(); err != nil {
  75. return nil, err
  76. }
  77. // Get URL for request
  78. path, params, err := s.buildURL()
  79. if err != nil {
  80. return nil, err
  81. }
  82. // Get HTTP response
  83. res, err := s.client.PerformRequest(ctx, "GET", path, params, nil)
  84. if err != nil {
  85. return nil, err
  86. }
  87. // Return result
  88. ret := new(GetTemplateResponse)
  89. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  90. return nil, err
  91. }
  92. return ret, nil
  93. }
  94. type GetTemplateResponse struct {
  95. Template string `json:"template"`
  96. }