api.xpack.rollup.rollup_search.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. "errors"
  23. "io"
  24. "net/http"
  25. "strconv"
  26. "strings"
  27. )
  28. func newRollupRollupSearchFunc(t Transport) RollupRollupSearch {
  29. return func(index []string, body io.Reader, o ...func(*RollupRollupSearchRequest)) (*Response, error) {
  30. var r = RollupRollupSearchRequest{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. // RollupRollupSearch - Enables searching rolled-up data using the standard query DSL.
  39. //
  40. // This API is experimental.
  41. //
  42. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/rollup-search.html.
  43. type RollupRollupSearch func(index []string, body io.Reader, o ...func(*RollupRollupSearchRequest)) (*Response, error)
  44. // RollupRollupSearchRequest configures the Rollup Rollup Search API request.
  45. type RollupRollupSearchRequest struct {
  46. Index []string
  47. DocumentType string
  48. Body io.Reader
  49. RestTotalHitsAsInt *bool
  50. TypedKeys *bool
  51. Pretty bool
  52. Human bool
  53. ErrorTrace bool
  54. FilterPath []string
  55. Header http.Header
  56. ctx context.Context
  57. }
  58. // Do executes the request and returns response or error.
  59. func (r RollupRollupSearchRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  60. var (
  61. method string
  62. path strings.Builder
  63. params map[string]string
  64. )
  65. method = "POST"
  66. if len(r.Index) == 0 {
  67. return nil, errors.New("index is required and cannot be nil or empty")
  68. }
  69. path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len(r.DocumentType) + 1 + len("_rollup_search"))
  70. path.WriteString("/")
  71. path.WriteString(strings.Join(r.Index, ","))
  72. if r.DocumentType != "" {
  73. path.WriteString("/")
  74. path.WriteString(r.DocumentType)
  75. }
  76. path.WriteString("/")
  77. path.WriteString("_rollup_search")
  78. params = make(map[string]string)
  79. if r.RestTotalHitsAsInt != nil {
  80. params["rest_total_hits_as_int"] = strconv.FormatBool(*r.RestTotalHitsAsInt)
  81. }
  82. if r.TypedKeys != nil {
  83. params["typed_keys"] = strconv.FormatBool(*r.TypedKeys)
  84. }
  85. if r.Pretty {
  86. params["pretty"] = "true"
  87. }
  88. if r.Human {
  89. params["human"] = "true"
  90. }
  91. if r.ErrorTrace {
  92. params["error_trace"] = "true"
  93. }
  94. if len(r.FilterPath) > 0 {
  95. params["filter_path"] = strings.Join(r.FilterPath, ",")
  96. }
  97. req, err := newRequest(method, path.String(), r.Body)
  98. if err != nil {
  99. return nil, err
  100. }
  101. if len(params) > 0 {
  102. q := req.URL.Query()
  103. for k, v := range params {
  104. q.Set(k, v)
  105. }
  106. req.URL.RawQuery = q.Encode()
  107. }
  108. if len(r.Header) > 0 {
  109. if len(req.Header) == 0 {
  110. req.Header = r.Header
  111. } else {
  112. for k, vv := range r.Header {
  113. for _, v := range vv {
  114. req.Header.Add(k, v)
  115. }
  116. }
  117. }
  118. }
  119. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  120. req.Header[headerContentType] = headerContentTypeJSON
  121. }
  122. if ctx != nil {
  123. req = req.WithContext(ctx)
  124. }
  125. res, err := transport.Perform(req)
  126. if err != nil {
  127. return nil, err
  128. }
  129. response := Response{
  130. StatusCode: res.StatusCode,
  131. Body: res.Body,
  132. Header: res.Header,
  133. }
  134. return &response, nil
  135. }
  136. // WithContext sets the request context.
  137. func (f RollupRollupSearch) WithContext(v context.Context) func(*RollupRollupSearchRequest) {
  138. return func(r *RollupRollupSearchRequest) {
  139. r.ctx = v
  140. }
  141. }
  142. // WithDocumentType - the doc type inside the index.
  143. func (f RollupRollupSearch) WithDocumentType(v string) func(*RollupRollupSearchRequest) {
  144. return func(r *RollupRollupSearchRequest) {
  145. r.DocumentType = v
  146. }
  147. }
  148. // WithRestTotalHitsAsInt - indicates whether hits.total should be rendered as an integer or an object in the rest search response.
  149. func (f RollupRollupSearch) WithRestTotalHitsAsInt(v bool) func(*RollupRollupSearchRequest) {
  150. return func(r *RollupRollupSearchRequest) {
  151. r.RestTotalHitsAsInt = &v
  152. }
  153. }
  154. // WithTypedKeys - specify whether aggregation and suggester names should be prefixed by their respective types in the response.
  155. func (f RollupRollupSearch) WithTypedKeys(v bool) func(*RollupRollupSearchRequest) {
  156. return func(r *RollupRollupSearchRequest) {
  157. r.TypedKeys = &v
  158. }
  159. }
  160. // WithPretty makes the response body pretty-printed.
  161. func (f RollupRollupSearch) WithPretty() func(*RollupRollupSearchRequest) {
  162. return func(r *RollupRollupSearchRequest) {
  163. r.Pretty = true
  164. }
  165. }
  166. // WithHuman makes statistical values human-readable.
  167. func (f RollupRollupSearch) WithHuman() func(*RollupRollupSearchRequest) {
  168. return func(r *RollupRollupSearchRequest) {
  169. r.Human = true
  170. }
  171. }
  172. // WithErrorTrace includes the stack trace for errors in the response body.
  173. func (f RollupRollupSearch) WithErrorTrace() func(*RollupRollupSearchRequest) {
  174. return func(r *RollupRollupSearchRequest) {
  175. r.ErrorTrace = true
  176. }
  177. }
  178. // WithFilterPath filters the properties of the response body.
  179. func (f RollupRollupSearch) WithFilterPath(v ...string) func(*RollupRollupSearchRequest) {
  180. return func(r *RollupRollupSearchRequest) {
  181. r.FilterPath = v
  182. }
  183. }
  184. // WithHeader adds the headers to the HTTP request.
  185. func (f RollupRollupSearch) WithHeader(h map[string]string) func(*RollupRollupSearchRequest) {
  186. return func(r *RollupRollupSearchRequest) {
  187. if r.Header == nil {
  188. r.Header = make(http.Header)
  189. }
  190. for k, v := range h {
  191. r.Header.Add(k, v)
  192. }
  193. }
  194. }
  195. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  196. func (f RollupRollupSearch) WithOpaqueID(s string) func(*RollupRollupSearchRequest) {
  197. return func(r *RollupRollupSearchRequest) {
  198. if r.Header == nil {
  199. r.Header = make(http.Header)
  200. }
  201. r.Header.Set("X-Opaque-Id", s)
  202. }
  203. }