api.field_caps.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. )
  27. func newFieldCapsFunc(t Transport) FieldCaps {
  28. return func(o ...func(*FieldCapsRequest)) (*Response, error) {
  29. var r = FieldCapsRequest{}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // FieldCaps returns the information about the capabilities of fields among multiple indices.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/search-field-caps.html.
  40. type FieldCaps func(o ...func(*FieldCapsRequest)) (*Response, error)
  41. // FieldCapsRequest configures the Field Caps API request.
  42. type FieldCapsRequest struct {
  43. Index []string
  44. Body io.Reader
  45. AllowNoIndices *bool
  46. ExpandWildcards string
  47. Fields []string
  48. IgnoreUnavailable *bool
  49. IncludeUnmapped *bool
  50. Pretty bool
  51. Human bool
  52. ErrorTrace bool
  53. FilterPath []string
  54. Header http.Header
  55. ctx context.Context
  56. }
  57. // Do executes the request and returns response or error.
  58. func (r FieldCapsRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  59. var (
  60. method string
  61. path strings.Builder
  62. params map[string]string
  63. )
  64. method = "POST"
  65. path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len("_field_caps"))
  66. if len(r.Index) > 0 {
  67. path.WriteString("/")
  68. path.WriteString(strings.Join(r.Index, ","))
  69. }
  70. path.WriteString("/")
  71. path.WriteString("_field_caps")
  72. params = make(map[string]string)
  73. if r.AllowNoIndices != nil {
  74. params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices)
  75. }
  76. if r.ExpandWildcards != "" {
  77. params["expand_wildcards"] = r.ExpandWildcards
  78. }
  79. if len(r.Fields) > 0 {
  80. params["fields"] = strings.Join(r.Fields, ",")
  81. }
  82. if r.IgnoreUnavailable != nil {
  83. params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
  84. }
  85. if r.IncludeUnmapped != nil {
  86. params["include_unmapped"] = strconv.FormatBool(*r.IncludeUnmapped)
  87. }
  88. if r.Pretty {
  89. params["pretty"] = "true"
  90. }
  91. if r.Human {
  92. params["human"] = "true"
  93. }
  94. if r.ErrorTrace {
  95. params["error_trace"] = "true"
  96. }
  97. if len(r.FilterPath) > 0 {
  98. params["filter_path"] = strings.Join(r.FilterPath, ",")
  99. }
  100. req, err := newRequest(method, path.String(), r.Body)
  101. if err != nil {
  102. return nil, err
  103. }
  104. if len(params) > 0 {
  105. q := req.URL.Query()
  106. for k, v := range params {
  107. q.Set(k, v)
  108. }
  109. req.URL.RawQuery = q.Encode()
  110. }
  111. if len(r.Header) > 0 {
  112. if len(req.Header) == 0 {
  113. req.Header = r.Header
  114. } else {
  115. for k, vv := range r.Header {
  116. for _, v := range vv {
  117. req.Header.Add(k, v)
  118. }
  119. }
  120. }
  121. }
  122. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  123. req.Header[headerContentType] = headerContentTypeJSON
  124. }
  125. if ctx != nil {
  126. req = req.WithContext(ctx)
  127. }
  128. res, err := transport.Perform(req)
  129. if err != nil {
  130. return nil, err
  131. }
  132. response := Response{
  133. StatusCode: res.StatusCode,
  134. Body: res.Body,
  135. Header: res.Header,
  136. }
  137. return &response, nil
  138. }
  139. // WithContext sets the request context.
  140. func (f FieldCaps) WithContext(v context.Context) func(*FieldCapsRequest) {
  141. return func(r *FieldCapsRequest) {
  142. r.ctx = v
  143. }
  144. }
  145. // WithBody - An index filter specified with the Query DSL.
  146. func (f FieldCaps) WithBody(v io.Reader) func(*FieldCapsRequest) {
  147. return func(r *FieldCapsRequest) {
  148. r.Body = v
  149. }
  150. }
  151. // WithIndex - a list of index names; use _all to perform the operation on all indices.
  152. func (f FieldCaps) WithIndex(v ...string) func(*FieldCapsRequest) {
  153. return func(r *FieldCapsRequest) {
  154. r.Index = v
  155. }
  156. }
  157. // 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).
  158. func (f FieldCaps) WithAllowNoIndices(v bool) func(*FieldCapsRequest) {
  159. return func(r *FieldCapsRequest) {
  160. r.AllowNoIndices = &v
  161. }
  162. }
  163. // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
  164. func (f FieldCaps) WithExpandWildcards(v string) func(*FieldCapsRequest) {
  165. return func(r *FieldCapsRequest) {
  166. r.ExpandWildcards = v
  167. }
  168. }
  169. // WithFields - a list of field names.
  170. func (f FieldCaps) WithFields(v ...string) func(*FieldCapsRequest) {
  171. return func(r *FieldCapsRequest) {
  172. r.Fields = v
  173. }
  174. }
  175. // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed).
  176. func (f FieldCaps) WithIgnoreUnavailable(v bool) func(*FieldCapsRequest) {
  177. return func(r *FieldCapsRequest) {
  178. r.IgnoreUnavailable = &v
  179. }
  180. }
  181. // WithIncludeUnmapped - indicates whether unmapped fields should be included in the response..
  182. func (f FieldCaps) WithIncludeUnmapped(v bool) func(*FieldCapsRequest) {
  183. return func(r *FieldCapsRequest) {
  184. r.IncludeUnmapped = &v
  185. }
  186. }
  187. // WithPretty makes the response body pretty-printed.
  188. func (f FieldCaps) WithPretty() func(*FieldCapsRequest) {
  189. return func(r *FieldCapsRequest) {
  190. r.Pretty = true
  191. }
  192. }
  193. // WithHuman makes statistical values human-readable.
  194. func (f FieldCaps) WithHuman() func(*FieldCapsRequest) {
  195. return func(r *FieldCapsRequest) {
  196. r.Human = true
  197. }
  198. }
  199. // WithErrorTrace includes the stack trace for errors in the response body.
  200. func (f FieldCaps) WithErrorTrace() func(*FieldCapsRequest) {
  201. return func(r *FieldCapsRequest) {
  202. r.ErrorTrace = true
  203. }
  204. }
  205. // WithFilterPath filters the properties of the response body.
  206. func (f FieldCaps) WithFilterPath(v ...string) func(*FieldCapsRequest) {
  207. return func(r *FieldCapsRequest) {
  208. r.FilterPath = v
  209. }
  210. }
  211. // WithHeader adds the headers to the HTTP request.
  212. func (f FieldCaps) WithHeader(h map[string]string) func(*FieldCapsRequest) {
  213. return func(r *FieldCapsRequest) {
  214. if r.Header == nil {
  215. r.Header = make(http.Header)
  216. }
  217. for k, v := range h {
  218. r.Header.Add(k, v)
  219. }
  220. }
  221. }
  222. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  223. func (f FieldCaps) WithOpaqueID(s string) func(*FieldCapsRequest) {
  224. return func(r *FieldCapsRequest) {
  225. if r.Header == nil {
  226. r.Header = make(http.Header)
  227. }
  228. r.Header.Set("X-Opaque-Id", s)
  229. }
  230. }