api.index.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 newIndexFunc(t Transport) Index {
  29. return func(index string, body io.Reader, o ...func(*IndexRequest)) (*Response, error) {
  30. var r = IndexRequest{Index: index, Body: body}
  31. for _, f := range o {
  32. f(&r)
  33. }
  34. return r.Do(r.ctx, t)
  35. }
  36. }
  37. // ----- API Definition -------------------------------------------------------
  38. // Index creates or updates a document in an index.
  39. //
  40. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html.
  41. type Index func(index string, body io.Reader, o ...func(*IndexRequest)) (*Response, error)
  42. // IndexRequest configures the Index API request.
  43. type IndexRequest struct {
  44. Index string
  45. DocumentType string
  46. DocumentID string
  47. Body io.Reader
  48. IfPrimaryTerm *int
  49. IfSeqNo *int
  50. OpType string
  51. Pipeline string
  52. Refresh string
  53. RequireAlias *bool
  54. Routing string
  55. Timeout time.Duration
  56. Version *int
  57. VersionType string
  58. WaitForActiveShards string
  59. Pretty bool
  60. Human bool
  61. ErrorTrace bool
  62. FilterPath []string
  63. Header http.Header
  64. ctx context.Context
  65. }
  66. // Do executes the request and returns response or error.
  67. func (r IndexRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  68. var (
  69. method string
  70. path strings.Builder
  71. params map[string]string
  72. )
  73. if r.DocumentID != "" {
  74. method = "PUT"
  75. } else {
  76. method = "POST"
  77. }
  78. if r.DocumentType == "" {
  79. r.DocumentType = "_doc"
  80. }
  81. path.Grow(1 + len(r.Index) + 1 + len(r.DocumentType) + 1 + len(r.DocumentID))
  82. path.WriteString("/")
  83. path.WriteString(r.Index)
  84. if r.DocumentType != "" {
  85. path.WriteString("/")
  86. path.WriteString(r.DocumentType)
  87. }
  88. if r.DocumentID != "" {
  89. path.WriteString("/")
  90. path.WriteString(r.DocumentID)
  91. }
  92. params = make(map[string]string)
  93. if r.IfPrimaryTerm != nil {
  94. params["if_primary_term"] = strconv.FormatInt(int64(*r.IfPrimaryTerm), 10)
  95. }
  96. if r.IfSeqNo != nil {
  97. params["if_seq_no"] = strconv.FormatInt(int64(*r.IfSeqNo), 10)
  98. }
  99. if r.OpType != "" {
  100. params["op_type"] = r.OpType
  101. }
  102. if r.Pipeline != "" {
  103. params["pipeline"] = r.Pipeline
  104. }
  105. if r.Refresh != "" {
  106. params["refresh"] = r.Refresh
  107. }
  108. if r.RequireAlias != nil {
  109. params["require_alias"] = strconv.FormatBool(*r.RequireAlias)
  110. }
  111. if r.Routing != "" {
  112. params["routing"] = r.Routing
  113. }
  114. if r.Timeout != 0 {
  115. params["timeout"] = formatDuration(r.Timeout)
  116. }
  117. if r.Version != nil {
  118. params["version"] = strconv.FormatInt(int64(*r.Version), 10)
  119. }
  120. if r.VersionType != "" {
  121. params["version_type"] = r.VersionType
  122. }
  123. if r.WaitForActiveShards != "" {
  124. params["wait_for_active_shards"] = r.WaitForActiveShards
  125. }
  126. if r.Pretty {
  127. params["pretty"] = "true"
  128. }
  129. if r.Human {
  130. params["human"] = "true"
  131. }
  132. if r.ErrorTrace {
  133. params["error_trace"] = "true"
  134. }
  135. if len(r.FilterPath) > 0 {
  136. params["filter_path"] = strings.Join(r.FilterPath, ",")
  137. }
  138. req, err := newRequest(method, path.String(), r.Body)
  139. if err != nil {
  140. return nil, err
  141. }
  142. if len(params) > 0 {
  143. q := req.URL.Query()
  144. for k, v := range params {
  145. q.Set(k, v)
  146. }
  147. req.URL.RawQuery = q.Encode()
  148. }
  149. if len(r.Header) > 0 {
  150. if len(req.Header) == 0 {
  151. req.Header = r.Header
  152. } else {
  153. for k, vv := range r.Header {
  154. for _, v := range vv {
  155. req.Header.Add(k, v)
  156. }
  157. }
  158. }
  159. }
  160. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  161. req.Header[headerContentType] = headerContentTypeJSON
  162. }
  163. if ctx != nil {
  164. req = req.WithContext(ctx)
  165. }
  166. res, err := transport.Perform(req)
  167. if err != nil {
  168. return nil, err
  169. }
  170. response := Response{
  171. StatusCode: res.StatusCode,
  172. Body: res.Body,
  173. Header: res.Header,
  174. }
  175. return &response, nil
  176. }
  177. // WithContext sets the request context.
  178. func (f Index) WithContext(v context.Context) func(*IndexRequest) {
  179. return func(r *IndexRequest) {
  180. r.ctx = v
  181. }
  182. }
  183. // WithDocumentID - document ID.
  184. func (f Index) WithDocumentID(v string) func(*IndexRequest) {
  185. return func(r *IndexRequest) {
  186. r.DocumentID = v
  187. }
  188. }
  189. // WithDocumentType - the type of the document.
  190. func (f Index) WithDocumentType(v string) func(*IndexRequest) {
  191. return func(r *IndexRequest) {
  192. r.DocumentType = v
  193. }
  194. }
  195. // WithIfPrimaryTerm - only perform the index operation if the last operation that has changed the document has the specified primary term.
  196. func (f Index) WithIfPrimaryTerm(v int) func(*IndexRequest) {
  197. return func(r *IndexRequest) {
  198. r.IfPrimaryTerm = &v
  199. }
  200. }
  201. // WithIfSeqNo - only perform the index operation if the last operation that has changed the document has the specified sequence number.
  202. func (f Index) WithIfSeqNo(v int) func(*IndexRequest) {
  203. return func(r *IndexRequest) {
  204. r.IfSeqNo = &v
  205. }
  206. }
  207. // WithOpType - explicit operation type. defaults to `index` for requests with an explicit document ID, and to `create`for requests without an explicit document ID.
  208. func (f Index) WithOpType(v string) func(*IndexRequest) {
  209. return func(r *IndexRequest) {
  210. r.OpType = v
  211. }
  212. }
  213. // WithPipeline - the pipeline ID to preprocess incoming documents with.
  214. func (f Index) WithPipeline(v string) func(*IndexRequest) {
  215. return func(r *IndexRequest) {
  216. r.Pipeline = v
  217. }
  218. }
  219. // 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..
  220. func (f Index) WithRefresh(v string) func(*IndexRequest) {
  221. return func(r *IndexRequest) {
  222. r.Refresh = v
  223. }
  224. }
  225. // WithRequireAlias - when true, requires destination to be an alias. default is false.
  226. func (f Index) WithRequireAlias(v bool) func(*IndexRequest) {
  227. return func(r *IndexRequest) {
  228. r.RequireAlias = &v
  229. }
  230. }
  231. // WithRouting - specific routing value.
  232. func (f Index) WithRouting(v string) func(*IndexRequest) {
  233. return func(r *IndexRequest) {
  234. r.Routing = v
  235. }
  236. }
  237. // WithTimeout - explicit operation timeout.
  238. func (f Index) WithTimeout(v time.Duration) func(*IndexRequest) {
  239. return func(r *IndexRequest) {
  240. r.Timeout = v
  241. }
  242. }
  243. // WithVersion - explicit version number for concurrency control.
  244. func (f Index) WithVersion(v int) func(*IndexRequest) {
  245. return func(r *IndexRequest) {
  246. r.Version = &v
  247. }
  248. }
  249. // WithVersionType - specific version type.
  250. func (f Index) WithVersionType(v string) func(*IndexRequest) {
  251. return func(r *IndexRequest) {
  252. r.VersionType = v
  253. }
  254. }
  255. // 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).
  256. func (f Index) WithWaitForActiveShards(v string) func(*IndexRequest) {
  257. return func(r *IndexRequest) {
  258. r.WaitForActiveShards = v
  259. }
  260. }
  261. // WithPretty makes the response body pretty-printed.
  262. func (f Index) WithPretty() func(*IndexRequest) {
  263. return func(r *IndexRequest) {
  264. r.Pretty = true
  265. }
  266. }
  267. // WithHuman makes statistical values human-readable.
  268. func (f Index) WithHuman() func(*IndexRequest) {
  269. return func(r *IndexRequest) {
  270. r.Human = true
  271. }
  272. }
  273. // WithErrorTrace includes the stack trace for errors in the response body.
  274. func (f Index) WithErrorTrace() func(*IndexRequest) {
  275. return func(r *IndexRequest) {
  276. r.ErrorTrace = true
  277. }
  278. }
  279. // WithFilterPath filters the properties of the response body.
  280. func (f Index) WithFilterPath(v ...string) func(*IndexRequest) {
  281. return func(r *IndexRequest) {
  282. r.FilterPath = v
  283. }
  284. }
  285. // WithHeader adds the headers to the HTTP request.
  286. func (f Index) WithHeader(h map[string]string) func(*IndexRequest) {
  287. return func(r *IndexRequest) {
  288. if r.Header == nil {
  289. r.Header = make(http.Header)
  290. }
  291. for k, v := range h {
  292. r.Header.Add(k, v)
  293. }
  294. }
  295. }
  296. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  297. func (f Index) WithOpaqueID(s string) func(*IndexRequest) {
  298. return func(r *IndexRequest) {
  299. if r.Header == nil {
  300. r.Header = make(http.Header)
  301. }
  302. r.Header.Set("X-Opaque-Id", s)
  303. }
  304. }