api.indices.put_mapping.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Licensed to Elasticsearch B.V. under one or more contributor
  2. // license agreements. See the NOTICE file distributed with
  3. // this work for additional information regarding copyright
  4. // ownership. Elasticsearch B.V. licenses this file to you under
  5. // the Apache License, Version 2.0 (the "License"); you may
  6. // not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. //
  18. // Code generated from specification version 7.17.10: DO NOT EDIT
  19. package esapi
  20. import (
  21. "context"
  22. "io"
  23. "net/http"
  24. "strconv"
  25. "strings"
  26. "time"
  27. )
  28. func newIndicesPutMappingFunc(t Transport) IndicesPutMapping {
  29. return func(body io.Reader, o ...func(*IndicesPutMappingRequest)) (*Response, error) {
  30. var r = IndicesPutMappingRequest{Body: body}
  31. for _, f := range o {
  32. f(&r)
  33. }
  34. return r.Do(r.ctx, t)
  35. }
  36. }
  37. // ----- API Definition -------------------------------------------------------
  38. // IndicesPutMapping updates the index mappings.
  39. //
  40. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-put-mapping.html.
  41. type IndicesPutMapping func(body io.Reader, o ...func(*IndicesPutMappingRequest)) (*Response, error)
  42. // IndicesPutMappingRequest configures the Indices Put Mapping API request.
  43. type IndicesPutMappingRequest struct {
  44. Index []string
  45. DocumentType string
  46. Body io.Reader
  47. AllowNoIndices *bool
  48. ExpandWildcards string
  49. IgnoreUnavailable *bool
  50. IncludeTypeName *bool
  51. MasterTimeout time.Duration
  52. Timeout time.Duration
  53. WriteIndexOnly *bool
  54. Pretty bool
  55. Human bool
  56. ErrorTrace bool
  57. FilterPath []string
  58. Header http.Header
  59. ctx context.Context
  60. }
  61. // Do executes the request and returns response or error.
  62. func (r IndicesPutMappingRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  63. var (
  64. method string
  65. path strings.Builder
  66. params map[string]string
  67. )
  68. method = "PUT"
  69. path.Grow(len(strings.Join(r.Index, ",")) + len("/_mapping") + len(r.DocumentType) + 2)
  70. if len(r.Index) > 0 {
  71. path.WriteString("/")
  72. path.WriteString(strings.Join(r.Index, ","))
  73. }
  74. path.WriteString("/")
  75. path.WriteString("_mapping")
  76. if r.DocumentType != "" {
  77. path.WriteString("/")
  78. path.WriteString(r.DocumentType)
  79. }
  80. params = make(map[string]string)
  81. if r.AllowNoIndices != nil {
  82. params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices)
  83. }
  84. if r.ExpandWildcards != "" {
  85. params["expand_wildcards"] = r.ExpandWildcards
  86. }
  87. if r.IgnoreUnavailable != nil {
  88. params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
  89. }
  90. if r.IncludeTypeName != nil {
  91. params["include_type_name"] = strconv.FormatBool(*r.IncludeTypeName)
  92. }
  93. if r.MasterTimeout != 0 {
  94. params["master_timeout"] = formatDuration(r.MasterTimeout)
  95. }
  96. if r.Timeout != 0 {
  97. params["timeout"] = formatDuration(r.Timeout)
  98. }
  99. if r.WriteIndexOnly != nil {
  100. params["write_index_only"] = strconv.FormatBool(*r.WriteIndexOnly)
  101. }
  102. if r.Pretty {
  103. params["pretty"] = "true"
  104. }
  105. if r.Human {
  106. params["human"] = "true"
  107. }
  108. if r.ErrorTrace {
  109. params["error_trace"] = "true"
  110. }
  111. if len(r.FilterPath) > 0 {
  112. params["filter_path"] = strings.Join(r.FilterPath, ",")
  113. }
  114. req, err := newRequest(method, path.String(), r.Body)
  115. if err != nil {
  116. return nil, err
  117. }
  118. if len(params) > 0 {
  119. q := req.URL.Query()
  120. for k, v := range params {
  121. q.Set(k, v)
  122. }
  123. req.URL.RawQuery = q.Encode()
  124. }
  125. if len(r.Header) > 0 {
  126. if len(req.Header) == 0 {
  127. req.Header = r.Header
  128. } else {
  129. for k, vv := range r.Header {
  130. for _, v := range vv {
  131. req.Header.Add(k, v)
  132. }
  133. }
  134. }
  135. }
  136. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  137. req.Header[headerContentType] = headerContentTypeJSON
  138. }
  139. if ctx != nil {
  140. req = req.WithContext(ctx)
  141. }
  142. res, err := transport.Perform(req)
  143. if err != nil {
  144. return nil, err
  145. }
  146. response := Response{
  147. StatusCode: res.StatusCode,
  148. Body: res.Body,
  149. Header: res.Header,
  150. }
  151. return &response, nil
  152. }
  153. // WithContext sets the request context.
  154. func (f IndicesPutMapping) WithContext(v context.Context) func(*IndicesPutMappingRequest) {
  155. return func(r *IndicesPutMappingRequest) {
  156. r.ctx = v
  157. }
  158. }
  159. // WithIndex - a list of index names the mapping should be added to (supports wildcards); use `_all` or omit to add the mapping on all indices..
  160. func (f IndicesPutMapping) WithIndex(v ...string) func(*IndicesPutMappingRequest) {
  161. return func(r *IndicesPutMappingRequest) {
  162. r.Index = v
  163. }
  164. }
  165. // WithDocumentType - the name of the document type.
  166. func (f IndicesPutMapping) WithDocumentType(v string) func(*IndicesPutMappingRequest) {
  167. return func(r *IndicesPutMappingRequest) {
  168. r.DocumentType = v
  169. }
  170. }
  171. // WithAllowNoIndices - whether to ignore if a wildcard indices expression resolves into no concrete indices. (this includes `_all` string or when no indices have been specified).
  172. func (f IndicesPutMapping) WithAllowNoIndices(v bool) func(*IndicesPutMappingRequest) {
  173. return func(r *IndicesPutMappingRequest) {
  174. r.AllowNoIndices = &v
  175. }
  176. }
  177. // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
  178. func (f IndicesPutMapping) WithExpandWildcards(v string) func(*IndicesPutMappingRequest) {
  179. return func(r *IndicesPutMappingRequest) {
  180. r.ExpandWildcards = v
  181. }
  182. }
  183. // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed).
  184. func (f IndicesPutMapping) WithIgnoreUnavailable(v bool) func(*IndicesPutMappingRequest) {
  185. return func(r *IndicesPutMappingRequest) {
  186. r.IgnoreUnavailable = &v
  187. }
  188. }
  189. // WithIncludeTypeName - whether a type should be expected in the body of the mappings..
  190. func (f IndicesPutMapping) WithIncludeTypeName(v bool) func(*IndicesPutMappingRequest) {
  191. return func(r *IndicesPutMappingRequest) {
  192. r.IncludeTypeName = &v
  193. }
  194. }
  195. // WithMasterTimeout - specify timeout for connection to master.
  196. func (f IndicesPutMapping) WithMasterTimeout(v time.Duration) func(*IndicesPutMappingRequest) {
  197. return func(r *IndicesPutMappingRequest) {
  198. r.MasterTimeout = v
  199. }
  200. }
  201. // WithTimeout - explicit operation timeout.
  202. func (f IndicesPutMapping) WithTimeout(v time.Duration) func(*IndicesPutMappingRequest) {
  203. return func(r *IndicesPutMappingRequest) {
  204. r.Timeout = v
  205. }
  206. }
  207. // WithWriteIndexOnly - when true, applies mappings only to the write index of an alias or data stream.
  208. func (f IndicesPutMapping) WithWriteIndexOnly(v bool) func(*IndicesPutMappingRequest) {
  209. return func(r *IndicesPutMappingRequest) {
  210. r.WriteIndexOnly = &v
  211. }
  212. }
  213. // WithPretty makes the response body pretty-printed.
  214. func (f IndicesPutMapping) WithPretty() func(*IndicesPutMappingRequest) {
  215. return func(r *IndicesPutMappingRequest) {
  216. r.Pretty = true
  217. }
  218. }
  219. // WithHuman makes statistical values human-readable.
  220. func (f IndicesPutMapping) WithHuman() func(*IndicesPutMappingRequest) {
  221. return func(r *IndicesPutMappingRequest) {
  222. r.Human = true
  223. }
  224. }
  225. // WithErrorTrace includes the stack trace for errors in the response body.
  226. func (f IndicesPutMapping) WithErrorTrace() func(*IndicesPutMappingRequest) {
  227. return func(r *IndicesPutMappingRequest) {
  228. r.ErrorTrace = true
  229. }
  230. }
  231. // WithFilterPath filters the properties of the response body.
  232. func (f IndicesPutMapping) WithFilterPath(v ...string) func(*IndicesPutMappingRequest) {
  233. return func(r *IndicesPutMappingRequest) {
  234. r.FilterPath = v
  235. }
  236. }
  237. // WithHeader adds the headers to the HTTP request.
  238. func (f IndicesPutMapping) WithHeader(h map[string]string) func(*IndicesPutMappingRequest) {
  239. return func(r *IndicesPutMappingRequest) {
  240. if r.Header == nil {
  241. r.Header = make(http.Header)
  242. }
  243. for k, v := range h {
  244. r.Header.Add(k, v)
  245. }
  246. }
  247. }
  248. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  249. func (f IndicesPutMapping) WithOpaqueID(s string) func(*IndicesPutMappingRequest) {
  250. return func(r *IndicesPutMappingRequest) {
  251. if r.Header == nil {
  252. r.Header = make(http.Header)
  253. }
  254. r.Header.Set("X-Opaque-Id", s)
  255. }
  256. }