api.create.go 7.5 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 newCreateFunc(t Transport) Create {
  29. return func(index string, id string, body io.Reader, o ...func(*CreateRequest)) (*Response, error) {
  30. var r = CreateRequest{Index: index, DocumentID: id, Body: body}
  31. for _, f := range o {
  32. f(&r)
  33. }
  34. return r.Do(r.ctx, t)
  35. }
  36. }
  37. // ----- API Definition -------------------------------------------------------
  38. // Create creates a new document in the index.
  39. //
  40. // Returns a 409 response when a document with a same ID already exists in the index.
  41. //
  42. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html.
  43. type Create func(index string, id string, body io.Reader, o ...func(*CreateRequest)) (*Response, error)
  44. // CreateRequest configures the Create API request.
  45. type CreateRequest struct {
  46. Index string
  47. DocumentType string
  48. DocumentID string
  49. Body io.Reader
  50. Pipeline string
  51. Refresh string
  52. Routing string
  53. Timeout time.Duration
  54. Version *int
  55. VersionType string
  56. WaitForActiveShards string
  57. Pretty bool
  58. Human bool
  59. ErrorTrace bool
  60. FilterPath []string
  61. Header http.Header
  62. ctx context.Context
  63. }
  64. // Do executes the request and returns response or error.
  65. func (r CreateRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  66. var (
  67. method string
  68. path strings.Builder
  69. params map[string]string
  70. )
  71. method = "PUT"
  72. if r.DocumentType == "" {
  73. r.DocumentType = "_doc"
  74. }
  75. path.Grow(1 + len(r.Index) + 1 + len(r.DocumentType) + 1 + len(r.DocumentID) + 1 + len("_create"))
  76. path.WriteString("/")
  77. path.WriteString(r.Index)
  78. if r.DocumentType != "" {
  79. path.WriteString("/")
  80. path.WriteString(r.DocumentType)
  81. }
  82. path.WriteString("/")
  83. path.WriteString(r.DocumentID)
  84. path.WriteString("/")
  85. path.WriteString("_create")
  86. params = make(map[string]string)
  87. if r.Pipeline != "" {
  88. params["pipeline"] = r.Pipeline
  89. }
  90. if r.Refresh != "" {
  91. params["refresh"] = r.Refresh
  92. }
  93. if r.Routing != "" {
  94. params["routing"] = r.Routing
  95. }
  96. if r.Timeout != 0 {
  97. params["timeout"] = formatDuration(r.Timeout)
  98. }
  99. if r.Version != nil {
  100. params["version"] = strconv.FormatInt(int64(*r.Version), 10)
  101. }
  102. if r.VersionType != "" {
  103. params["version_type"] = r.VersionType
  104. }
  105. if r.WaitForActiveShards != "" {
  106. params["wait_for_active_shards"] = r.WaitForActiveShards
  107. }
  108. if r.Pretty {
  109. params["pretty"] = "true"
  110. }
  111. if r.Human {
  112. params["human"] = "true"
  113. }
  114. if r.ErrorTrace {
  115. params["error_trace"] = "true"
  116. }
  117. if len(r.FilterPath) > 0 {
  118. params["filter_path"] = strings.Join(r.FilterPath, ",")
  119. }
  120. req, err := newRequest(method, path.String(), r.Body)
  121. if err != nil {
  122. return nil, err
  123. }
  124. if len(params) > 0 {
  125. q := req.URL.Query()
  126. for k, v := range params {
  127. q.Set(k, v)
  128. }
  129. req.URL.RawQuery = q.Encode()
  130. }
  131. if len(r.Header) > 0 {
  132. if len(req.Header) == 0 {
  133. req.Header = r.Header
  134. } else {
  135. for k, vv := range r.Header {
  136. for _, v := range vv {
  137. req.Header.Add(k, v)
  138. }
  139. }
  140. }
  141. }
  142. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  143. req.Header[headerContentType] = headerContentTypeJSON
  144. }
  145. if ctx != nil {
  146. req = req.WithContext(ctx)
  147. }
  148. res, err := transport.Perform(req)
  149. if err != nil {
  150. return nil, err
  151. }
  152. response := Response{
  153. StatusCode: res.StatusCode,
  154. Body: res.Body,
  155. Header: res.Header,
  156. }
  157. return &response, nil
  158. }
  159. // WithContext sets the request context.
  160. func (f Create) WithContext(v context.Context) func(*CreateRequest) {
  161. return func(r *CreateRequest) {
  162. r.ctx = v
  163. }
  164. }
  165. // WithDocumentType - the type of the document.
  166. func (f Create) WithDocumentType(v string) func(*CreateRequest) {
  167. return func(r *CreateRequest) {
  168. r.DocumentType = v
  169. }
  170. }
  171. // WithPipeline - the pipeline ID to preprocess incoming documents with.
  172. func (f Create) WithPipeline(v string) func(*CreateRequest) {
  173. return func(r *CreateRequest) {
  174. r.Pipeline = v
  175. }
  176. }
  177. // WithRefresh - if `true` then refresh the affected shards to make this operation visible to search, if `wait_for` then wait for a refresh to make this operation visible to search, if `false` (the default) then do nothing with refreshes..
  178. func (f Create) WithRefresh(v string) func(*CreateRequest) {
  179. return func(r *CreateRequest) {
  180. r.Refresh = v
  181. }
  182. }
  183. // WithRouting - specific routing value.
  184. func (f Create) WithRouting(v string) func(*CreateRequest) {
  185. return func(r *CreateRequest) {
  186. r.Routing = v
  187. }
  188. }
  189. // WithTimeout - explicit operation timeout.
  190. func (f Create) WithTimeout(v time.Duration) func(*CreateRequest) {
  191. return func(r *CreateRequest) {
  192. r.Timeout = v
  193. }
  194. }
  195. // WithVersion - explicit version number for concurrency control.
  196. func (f Create) WithVersion(v int) func(*CreateRequest) {
  197. return func(r *CreateRequest) {
  198. r.Version = &v
  199. }
  200. }
  201. // WithVersionType - specific version type.
  202. func (f Create) WithVersionType(v string) func(*CreateRequest) {
  203. return func(r *CreateRequest) {
  204. r.VersionType = v
  205. }
  206. }
  207. // WithWaitForActiveShards - sets the number of shard copies that must be active before proceeding with the index operation. defaults to 1, meaning the primary shard only. set to `all` for all shard copies, otherwise set to any non-negative value less than or equal to the total number of copies for the shard (number of replicas + 1).
  208. func (f Create) WithWaitForActiveShards(v string) func(*CreateRequest) {
  209. return func(r *CreateRequest) {
  210. r.WaitForActiveShards = v
  211. }
  212. }
  213. // WithPretty makes the response body pretty-printed.
  214. func (f Create) WithPretty() func(*CreateRequest) {
  215. return func(r *CreateRequest) {
  216. r.Pretty = true
  217. }
  218. }
  219. // WithHuman makes statistical values human-readable.
  220. func (f Create) WithHuman() func(*CreateRequest) {
  221. return func(r *CreateRequest) {
  222. r.Human = true
  223. }
  224. }
  225. // WithErrorTrace includes the stack trace for errors in the response body.
  226. func (f Create) WithErrorTrace() func(*CreateRequest) {
  227. return func(r *CreateRequest) {
  228. r.ErrorTrace = true
  229. }
  230. }
  231. // WithFilterPath filters the properties of the response body.
  232. func (f Create) WithFilterPath(v ...string) func(*CreateRequest) {
  233. return func(r *CreateRequest) {
  234. r.FilterPath = v
  235. }
  236. }
  237. // WithHeader adds the headers to the HTTP request.
  238. func (f Create) WithHeader(h map[string]string) func(*CreateRequest) {
  239. return func(r *CreateRequest) {
  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 Create) WithOpaqueID(s string) func(*CreateRequest) {
  250. return func(r *CreateRequest) {
  251. if r.Header == nil {
  252. r.Header = make(http.Header)
  253. }
  254. r.Header.Set("X-Opaque-Id", s)
  255. }
  256. }