api.count.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 newCountFunc(t Transport) Count {
  28. return func(o ...func(*CountRequest)) (*Response, error) {
  29. var r = CountRequest{}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // Count returns number of documents matching a query.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/search-count.html.
  40. type Count func(o ...func(*CountRequest)) (*Response, error)
  41. // CountRequest configures the Count API request.
  42. type CountRequest struct {
  43. Index []string
  44. DocumentType []string
  45. Body io.Reader
  46. AllowNoIndices *bool
  47. Analyzer string
  48. AnalyzeWildcard *bool
  49. DefaultOperator string
  50. Df string
  51. ExpandWildcards string
  52. IgnoreThrottled *bool
  53. IgnoreUnavailable *bool
  54. Lenient *bool
  55. MinScore *int
  56. Preference string
  57. Query string
  58. Routing []string
  59. TerminateAfter *int
  60. Pretty bool
  61. Human bool
  62. ErrorTrace bool
  63. FilterPath []string
  64. Header http.Header
  65. ctx context.Context
  66. }
  67. // Do executes the request and returns response or error.
  68. func (r CountRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  69. var (
  70. method string
  71. path strings.Builder
  72. params map[string]string
  73. )
  74. method = "POST"
  75. path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len(strings.Join(r.DocumentType, ",")) + 1 + len("_count"))
  76. if len(r.Index) > 0 {
  77. path.WriteString("/")
  78. path.WriteString(strings.Join(r.Index, ","))
  79. }
  80. if len(r.DocumentType) > 0 {
  81. path.WriteString("/")
  82. path.WriteString(strings.Join(r.DocumentType, ","))
  83. }
  84. path.WriteString("/")
  85. path.WriteString("_count")
  86. params = make(map[string]string)
  87. if r.AllowNoIndices != nil {
  88. params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices)
  89. }
  90. if r.Analyzer != "" {
  91. params["analyzer"] = r.Analyzer
  92. }
  93. if r.AnalyzeWildcard != nil {
  94. params["analyze_wildcard"] = strconv.FormatBool(*r.AnalyzeWildcard)
  95. }
  96. if r.DefaultOperator != "" {
  97. params["default_operator"] = r.DefaultOperator
  98. }
  99. if r.Df != "" {
  100. params["df"] = r.Df
  101. }
  102. if r.ExpandWildcards != "" {
  103. params["expand_wildcards"] = r.ExpandWildcards
  104. }
  105. if r.IgnoreThrottled != nil {
  106. params["ignore_throttled"] = strconv.FormatBool(*r.IgnoreThrottled)
  107. }
  108. if r.IgnoreUnavailable != nil {
  109. params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
  110. }
  111. if r.Lenient != nil {
  112. params["lenient"] = strconv.FormatBool(*r.Lenient)
  113. }
  114. if r.MinScore != nil {
  115. params["min_score"] = strconv.FormatInt(int64(*r.MinScore), 10)
  116. }
  117. if r.Preference != "" {
  118. params["preference"] = r.Preference
  119. }
  120. if r.Query != "" {
  121. params["q"] = r.Query
  122. }
  123. if len(r.Routing) > 0 {
  124. params["routing"] = strings.Join(r.Routing, ",")
  125. }
  126. if r.TerminateAfter != nil {
  127. params["terminate_after"] = strconv.FormatInt(int64(*r.TerminateAfter), 10)
  128. }
  129. if r.Pretty {
  130. params["pretty"] = "true"
  131. }
  132. if r.Human {
  133. params["human"] = "true"
  134. }
  135. if r.ErrorTrace {
  136. params["error_trace"] = "true"
  137. }
  138. if len(r.FilterPath) > 0 {
  139. params["filter_path"] = strings.Join(r.FilterPath, ",")
  140. }
  141. req, err := newRequest(method, path.String(), r.Body)
  142. if err != nil {
  143. return nil, err
  144. }
  145. if len(params) > 0 {
  146. q := req.URL.Query()
  147. for k, v := range params {
  148. q.Set(k, v)
  149. }
  150. req.URL.RawQuery = q.Encode()
  151. }
  152. if len(r.Header) > 0 {
  153. if len(req.Header) == 0 {
  154. req.Header = r.Header
  155. } else {
  156. for k, vv := range r.Header {
  157. for _, v := range vv {
  158. req.Header.Add(k, v)
  159. }
  160. }
  161. }
  162. }
  163. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  164. req.Header[headerContentType] = headerContentTypeJSON
  165. }
  166. if ctx != nil {
  167. req = req.WithContext(ctx)
  168. }
  169. res, err := transport.Perform(req)
  170. if err != nil {
  171. return nil, err
  172. }
  173. response := Response{
  174. StatusCode: res.StatusCode,
  175. Body: res.Body,
  176. Header: res.Header,
  177. }
  178. return &response, nil
  179. }
  180. // WithContext sets the request context.
  181. func (f Count) WithContext(v context.Context) func(*CountRequest) {
  182. return func(r *CountRequest) {
  183. r.ctx = v
  184. }
  185. }
  186. // WithBody - A query to restrict the results specified with the Query DSL (optional).
  187. func (f Count) WithBody(v io.Reader) func(*CountRequest) {
  188. return func(r *CountRequest) {
  189. r.Body = v
  190. }
  191. }
  192. // WithIndex - a list of indices to restrict the results.
  193. func (f Count) WithIndex(v ...string) func(*CountRequest) {
  194. return func(r *CountRequest) {
  195. r.Index = v
  196. }
  197. }
  198. // WithDocumentType - a list of types to restrict the results.
  199. func (f Count) WithDocumentType(v ...string) func(*CountRequest) {
  200. return func(r *CountRequest) {
  201. r.DocumentType = v
  202. }
  203. }
  204. // 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).
  205. func (f Count) WithAllowNoIndices(v bool) func(*CountRequest) {
  206. return func(r *CountRequest) {
  207. r.AllowNoIndices = &v
  208. }
  209. }
  210. // WithAnalyzer - the analyzer to use for the query string.
  211. func (f Count) WithAnalyzer(v string) func(*CountRequest) {
  212. return func(r *CountRequest) {
  213. r.Analyzer = v
  214. }
  215. }
  216. // WithAnalyzeWildcard - specify whether wildcard and prefix queries should be analyzed (default: false).
  217. func (f Count) WithAnalyzeWildcard(v bool) func(*CountRequest) {
  218. return func(r *CountRequest) {
  219. r.AnalyzeWildcard = &v
  220. }
  221. }
  222. // WithDefaultOperator - the default operator for query string query (and or or).
  223. func (f Count) WithDefaultOperator(v string) func(*CountRequest) {
  224. return func(r *CountRequest) {
  225. r.DefaultOperator = v
  226. }
  227. }
  228. // WithDf - the field to use as default where no field prefix is given in the query string.
  229. func (f Count) WithDf(v string) func(*CountRequest) {
  230. return func(r *CountRequest) {
  231. r.Df = v
  232. }
  233. }
  234. // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
  235. func (f Count) WithExpandWildcards(v string) func(*CountRequest) {
  236. return func(r *CountRequest) {
  237. r.ExpandWildcards = v
  238. }
  239. }
  240. // WithIgnoreThrottled - whether specified concrete, expanded or aliased indices should be ignored when throttled.
  241. func (f Count) WithIgnoreThrottled(v bool) func(*CountRequest) {
  242. return func(r *CountRequest) {
  243. r.IgnoreThrottled = &v
  244. }
  245. }
  246. // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed).
  247. func (f Count) WithIgnoreUnavailable(v bool) func(*CountRequest) {
  248. return func(r *CountRequest) {
  249. r.IgnoreUnavailable = &v
  250. }
  251. }
  252. // WithLenient - specify whether format-based query failures (such as providing text to a numeric field) should be ignored.
  253. func (f Count) WithLenient(v bool) func(*CountRequest) {
  254. return func(r *CountRequest) {
  255. r.Lenient = &v
  256. }
  257. }
  258. // WithMinScore - include only documents with a specific `_score` value in the result.
  259. func (f Count) WithMinScore(v int) func(*CountRequest) {
  260. return func(r *CountRequest) {
  261. r.MinScore = &v
  262. }
  263. }
  264. // WithPreference - specify the node or shard the operation should be performed on (default: random).
  265. func (f Count) WithPreference(v string) func(*CountRequest) {
  266. return func(r *CountRequest) {
  267. r.Preference = v
  268. }
  269. }
  270. // WithQuery - query in the lucene query string syntax.
  271. func (f Count) WithQuery(v string) func(*CountRequest) {
  272. return func(r *CountRequest) {
  273. r.Query = v
  274. }
  275. }
  276. // WithRouting - a list of specific routing values.
  277. func (f Count) WithRouting(v ...string) func(*CountRequest) {
  278. return func(r *CountRequest) {
  279. r.Routing = v
  280. }
  281. }
  282. // WithTerminateAfter - the maximum count for each shard, upon reaching which the query execution will terminate early.
  283. func (f Count) WithTerminateAfter(v int) func(*CountRequest) {
  284. return func(r *CountRequest) {
  285. r.TerminateAfter = &v
  286. }
  287. }
  288. // WithPretty makes the response body pretty-printed.
  289. func (f Count) WithPretty() func(*CountRequest) {
  290. return func(r *CountRequest) {
  291. r.Pretty = true
  292. }
  293. }
  294. // WithHuman makes statistical values human-readable.
  295. func (f Count) WithHuman() func(*CountRequest) {
  296. return func(r *CountRequest) {
  297. r.Human = true
  298. }
  299. }
  300. // WithErrorTrace includes the stack trace for errors in the response body.
  301. func (f Count) WithErrorTrace() func(*CountRequest) {
  302. return func(r *CountRequest) {
  303. r.ErrorTrace = true
  304. }
  305. }
  306. // WithFilterPath filters the properties of the response body.
  307. func (f Count) WithFilterPath(v ...string) func(*CountRequest) {
  308. return func(r *CountRequest) {
  309. r.FilterPath = v
  310. }
  311. }
  312. // WithHeader adds the headers to the HTTP request.
  313. func (f Count) WithHeader(h map[string]string) func(*CountRequest) {
  314. return func(r *CountRequest) {
  315. if r.Header == nil {
  316. r.Header = make(http.Header)
  317. }
  318. for k, v := range h {
  319. r.Header.Add(k, v)
  320. }
  321. }
  322. }
  323. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  324. func (f Count) WithOpaqueID(s string) func(*CountRequest) {
  325. return func(r *CountRequest) {
  326. if r.Header == nil {
  327. r.Header = make(http.Header)
  328. }
  329. r.Header.Set("X-Opaque-Id", s)
  330. }
  331. }