indices_put_template.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. "net/url"
  9. "github.com/olivere/elastic/uritemplates"
  10. )
  11. // IndicesPutTemplateService creates or updates index mappings.
  12. // See http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.4/indices-templates.html.
  13. type IndicesPutTemplateService struct {
  14. client *Client
  15. pretty bool
  16. name string
  17. order interface{}
  18. create *bool
  19. timeout string
  20. masterTimeout string
  21. flatSettings *bool
  22. bodyJson interface{}
  23. bodyString string
  24. }
  25. // NewIndicesPutTemplateService creates a new IndicesPutTemplateService.
  26. func NewIndicesPutTemplateService(client *Client) *IndicesPutTemplateService {
  27. return &IndicesPutTemplateService{
  28. client: client,
  29. }
  30. }
  31. // Name is the name of the index template.
  32. func (s *IndicesPutTemplateService) Name(name string) *IndicesPutTemplateService {
  33. s.name = name
  34. return s
  35. }
  36. // Timeout is an explicit operation timeout.
  37. func (s *IndicesPutTemplateService) Timeout(timeout string) *IndicesPutTemplateService {
  38. s.timeout = timeout
  39. return s
  40. }
  41. // MasterTimeout specifies the timeout for connection to master.
  42. func (s *IndicesPutTemplateService) MasterTimeout(masterTimeout string) *IndicesPutTemplateService {
  43. s.masterTimeout = masterTimeout
  44. return s
  45. }
  46. // FlatSettings indicates whether to return settings in flat format (default: false).
  47. func (s *IndicesPutTemplateService) FlatSettings(flatSettings bool) *IndicesPutTemplateService {
  48. s.flatSettings = &flatSettings
  49. return s
  50. }
  51. // Order is the order for this template when merging multiple matching ones
  52. // (higher numbers are merged later, overriding the lower numbers).
  53. func (s *IndicesPutTemplateService) Order(order interface{}) *IndicesPutTemplateService {
  54. s.order = order
  55. return s
  56. }
  57. // Create indicates whether the index template should only be added if
  58. // new or can also replace an existing one.
  59. func (s *IndicesPutTemplateService) Create(create bool) *IndicesPutTemplateService {
  60. s.create = &create
  61. return s
  62. }
  63. // Pretty indicates that the JSON response be indented and human readable.
  64. func (s *IndicesPutTemplateService) Pretty(pretty bool) *IndicesPutTemplateService {
  65. s.pretty = pretty
  66. return s
  67. }
  68. // BodyJson is documented as: The template definition.
  69. func (s *IndicesPutTemplateService) BodyJson(body interface{}) *IndicesPutTemplateService {
  70. s.bodyJson = body
  71. return s
  72. }
  73. // BodyString is documented as: The template definition.
  74. func (s *IndicesPutTemplateService) BodyString(body string) *IndicesPutTemplateService {
  75. s.bodyString = body
  76. return s
  77. }
  78. // buildURL builds the URL for the operation.
  79. func (s *IndicesPutTemplateService) buildURL() (string, url.Values, error) {
  80. // Build URL
  81. path, err := uritemplates.Expand("/_template/{name}", map[string]string{
  82. "name": s.name,
  83. })
  84. if err != nil {
  85. return "", url.Values{}, err
  86. }
  87. // Add query string parameters
  88. params := url.Values{}
  89. if s.pretty {
  90. params.Set("pretty", "1")
  91. }
  92. if s.order != nil {
  93. params.Set("order", fmt.Sprintf("%v", s.order))
  94. }
  95. if s.create != nil {
  96. params.Set("create", fmt.Sprintf("%v", *s.create))
  97. }
  98. if s.timeout != "" {
  99. params.Set("timeout", s.timeout)
  100. }
  101. if s.masterTimeout != "" {
  102. params.Set("master_timeout", s.masterTimeout)
  103. }
  104. if s.flatSettings != nil {
  105. params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings))
  106. }
  107. return path, params, nil
  108. }
  109. // Validate checks if the operation is valid.
  110. func (s *IndicesPutTemplateService) Validate() error {
  111. var invalid []string
  112. if s.name == "" {
  113. invalid = append(invalid, "Name")
  114. }
  115. if s.bodyString == "" && s.bodyJson == nil {
  116. invalid = append(invalid, "BodyJson")
  117. }
  118. if len(invalid) > 0 {
  119. return fmt.Errorf("missing required fields: %v", invalid)
  120. }
  121. return nil
  122. }
  123. // Do executes the operation.
  124. func (s *IndicesPutTemplateService) Do() (*IndicesPutTemplateResponse, error) {
  125. // Check pre-conditions
  126. if err := s.Validate(); err != nil {
  127. return nil, err
  128. }
  129. // Get URL for request
  130. path, params, err := s.buildURL()
  131. if err != nil {
  132. return nil, err
  133. }
  134. // Setup HTTP request body
  135. var body interface{}
  136. if s.bodyJson != nil {
  137. body = s.bodyJson
  138. } else {
  139. body = s.bodyString
  140. }
  141. // Get HTTP response
  142. res, err := s.client.PerformRequest("PUT", path, params, body)
  143. if err != nil {
  144. return nil, err
  145. }
  146. // Return operation response
  147. ret := new(IndicesPutTemplateResponse)
  148. if err := json.Unmarshal(res.Body, ret); err != nil {
  149. return nil, err
  150. }
  151. return ret, nil
  152. }
  153. // IndicesPutTemplateResponse is the response of IndicesPutTemplateService.Do.
  154. type IndicesPutTemplateResponse struct {
  155. Acknowledged bool `json:"acknowledged,omitempty"`
  156. }