put_mapping.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. "log"
  9. "net/url"
  10. "strings"
  11. "github.com/olivere/elastic/uritemplates"
  12. )
  13. var (
  14. _ = fmt.Print
  15. _ = log.Print
  16. _ = strings.Index
  17. _ = uritemplates.Expand
  18. _ = url.Parse
  19. )
  20. // PutMappingService allows to register specific mapping definition
  21. // for a specific type.
  22. // See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-put-mapping.html.
  23. type PutMappingService struct {
  24. client *Client
  25. pretty bool
  26. typ string
  27. index []string
  28. masterTimeout string
  29. ignoreUnavailable *bool
  30. allowNoIndices *bool
  31. expandWildcards string
  32. ignoreConflicts *bool
  33. timeout string
  34. bodyJson map[string]interface{}
  35. bodyString string
  36. }
  37. // NewPutMappingService creates a new PutMappingService.
  38. func NewPutMappingService(client *Client) *PutMappingService {
  39. return &PutMappingService{
  40. client: client,
  41. index: make([]string, 0),
  42. }
  43. }
  44. // Index is a list of index names the mapping should be added to
  45. // (supports wildcards); use `_all` or omit to add the mapping on all indices.
  46. func (s *PutMappingService) Index(index ...string) *PutMappingService {
  47. s.index = append(s.index, index...)
  48. return s
  49. }
  50. // Type is the name of the document type.
  51. func (s *PutMappingService) Type(typ string) *PutMappingService {
  52. s.typ = typ
  53. return s
  54. }
  55. // Timeout is an explicit operation timeout.
  56. func (s *PutMappingService) Timeout(timeout string) *PutMappingService {
  57. s.timeout = timeout
  58. return s
  59. }
  60. // MasterTimeout specifies the timeout for connection to master.
  61. func (s *PutMappingService) MasterTimeout(masterTimeout string) *PutMappingService {
  62. s.masterTimeout = masterTimeout
  63. return s
  64. }
  65. // IgnoreUnavailable indicates whether specified concrete indices should be
  66. // ignored when unavailable (missing or closed).
  67. func (s *PutMappingService) IgnoreUnavailable(ignoreUnavailable bool) *PutMappingService {
  68. s.ignoreUnavailable = &ignoreUnavailable
  69. return s
  70. }
  71. // AllowNoIndices indicates whether to ignore if a wildcard indices
  72. // expression resolves into no concrete indices.
  73. // This includes `_all` string or when no indices have been specified.
  74. func (s *PutMappingService) AllowNoIndices(allowNoIndices bool) *PutMappingService {
  75. s.allowNoIndices = &allowNoIndices
  76. return s
  77. }
  78. // ExpandWildcards indicates whether to expand wildcard expression to
  79. // concrete indices that are open, closed or both.
  80. func (s *PutMappingService) ExpandWildcards(expandWildcards string) *PutMappingService {
  81. s.expandWildcards = expandWildcards
  82. return s
  83. }
  84. // IgnoreConflicts specifies whether to ignore conflicts while updating
  85. // the mapping (default: false).
  86. func (s *PutMappingService) IgnoreConflicts(ignoreConflicts bool) *PutMappingService {
  87. s.ignoreConflicts = &ignoreConflicts
  88. return s
  89. }
  90. // Pretty indicates that the JSON response be indented and human readable.
  91. func (s *PutMappingService) Pretty(pretty bool) *PutMappingService {
  92. s.pretty = pretty
  93. return s
  94. }
  95. // BodyJson contains the mapping definition.
  96. func (s *PutMappingService) BodyJson(mapping map[string]interface{}) *PutMappingService {
  97. s.bodyJson = mapping
  98. return s
  99. }
  100. // BodyString is the mapping definition serialized as a string.
  101. func (s *PutMappingService) BodyString(mapping string) *PutMappingService {
  102. s.bodyString = mapping
  103. return s
  104. }
  105. // buildURL builds the URL for the operation.
  106. func (s *PutMappingService) buildURL() (string, url.Values, error) {
  107. var err error
  108. var path string
  109. // Build URL: Typ MUST be specified and is verified in Validate.
  110. if len(s.index) > 0 {
  111. path, err = uritemplates.Expand("/{index}/_mapping/{type}", map[string]string{
  112. "index": strings.Join(s.index, ","),
  113. "type": s.typ,
  114. })
  115. } else {
  116. path, err = uritemplates.Expand("/_mapping/{type}", map[string]string{
  117. "type": s.typ,
  118. })
  119. }
  120. if err != nil {
  121. return "", url.Values{}, err
  122. }
  123. // Add query string parameters
  124. params := url.Values{}
  125. if s.pretty {
  126. params.Set("pretty", "1")
  127. }
  128. if s.ignoreUnavailable != nil {
  129. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  130. }
  131. if s.allowNoIndices != nil {
  132. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  133. }
  134. if s.expandWildcards != "" {
  135. params.Set("expand_wildcards", s.expandWildcards)
  136. }
  137. if s.ignoreConflicts != nil {
  138. params.Set("ignore_conflicts", fmt.Sprintf("%v", *s.ignoreConflicts))
  139. }
  140. if s.timeout != "" {
  141. params.Set("timeout", s.timeout)
  142. }
  143. if s.masterTimeout != "" {
  144. params.Set("master_timeout", s.masterTimeout)
  145. }
  146. return path, params, nil
  147. }
  148. // Validate checks if the operation is valid.
  149. func (s *PutMappingService) Validate() error {
  150. var invalid []string
  151. if s.typ == "" {
  152. invalid = append(invalid, "Type")
  153. }
  154. if s.bodyString == "" && s.bodyJson == nil {
  155. invalid = append(invalid, "BodyJson")
  156. }
  157. if len(invalid) > 0 {
  158. return fmt.Errorf("missing required fields: %v", invalid)
  159. }
  160. return nil
  161. }
  162. // Do executes the operation.
  163. func (s *PutMappingService) Do() (*PutMappingResponse, error) {
  164. // Check pre-conditions
  165. if err := s.Validate(); err != nil {
  166. return nil, err
  167. }
  168. // Get URL for request
  169. path, params, err := s.buildURL()
  170. if err != nil {
  171. return nil, err
  172. }
  173. // Setup HTTP request body
  174. var body interface{}
  175. if s.bodyJson != nil {
  176. body = s.bodyJson
  177. } else {
  178. body = s.bodyString
  179. }
  180. // Get HTTP response
  181. res, err := s.client.PerformRequest("PUT", path, params, body)
  182. if err != nil {
  183. return nil, err
  184. }
  185. // Return operation response
  186. ret := new(PutMappingResponse)
  187. if err := json.Unmarshal(res.Body, ret); err != nil {
  188. return nil, err
  189. }
  190. return ret, nil
  191. }
  192. // PutMappingResponse is the response of PutMappingService.Do.
  193. type PutMappingResponse struct {
  194. Acknowledged bool `json:"acknowledged"`
  195. }