indices_put_mapping.go 6.2 KB

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