api.xpack.graph.explore.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. "strings"
  26. "time"
  27. )
  28. func newGraphExploreFunc(t Transport) GraphExplore {
  29. return func(index []string, o ...func(*GraphExploreRequest)) (*Response, error) {
  30. var r = GraphExploreRequest{Index: index}
  31. for _, f := range o {
  32. f(&r)
  33. }
  34. return r.Do(r.ctx, t)
  35. }
  36. }
  37. // ----- API Definition -------------------------------------------------------
  38. // GraphExplore - Explore extracted and summarized information about the documents and terms in an index.
  39. //
  40. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/graph-explore-api.html.
  41. type GraphExplore func(index []string, o ...func(*GraphExploreRequest)) (*Response, error)
  42. // GraphExploreRequest configures the Graph Explore API request.
  43. type GraphExploreRequest struct {
  44. Index []string
  45. DocumentType []string
  46. Body io.Reader
  47. Routing string
  48. Timeout time.Duration
  49. Pretty bool
  50. Human bool
  51. ErrorTrace bool
  52. FilterPath []string
  53. Header http.Header
  54. ctx context.Context
  55. }
  56. // Do executes the request and returns response or error.
  57. func (r GraphExploreRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  58. var (
  59. method string
  60. path strings.Builder
  61. params map[string]string
  62. )
  63. method = "POST"
  64. if len(r.Index) == 0 {
  65. return nil, errors.New("index is required and cannot be nil or empty")
  66. }
  67. path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len(strings.Join(r.DocumentType, ",")) + 1 + len("_graph") + 1 + len("explore"))
  68. path.WriteString("/")
  69. path.WriteString(strings.Join(r.Index, ","))
  70. if len(r.DocumentType) > 0 {
  71. path.WriteString("/")
  72. path.WriteString(strings.Join(r.DocumentType, ","))
  73. }
  74. path.WriteString("/")
  75. path.WriteString("_graph")
  76. path.WriteString("/")
  77. path.WriteString("explore")
  78. params = make(map[string]string)
  79. if r.Routing != "" {
  80. params["routing"] = r.Routing
  81. }
  82. if r.Timeout != 0 {
  83. params["timeout"] = formatDuration(r.Timeout)
  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 GraphExplore) WithContext(v context.Context) func(*GraphExploreRequest) {
  138. return func(r *GraphExploreRequest) {
  139. r.ctx = v
  140. }
  141. }
  142. // WithBody - Graph Query DSL.
  143. func (f GraphExplore) WithBody(v io.Reader) func(*GraphExploreRequest) {
  144. return func(r *GraphExploreRequest) {
  145. r.Body = v
  146. }
  147. }
  148. // WithDocumentType - a list of document types to search; leave empty to perform the operation on all types.
  149. func (f GraphExplore) WithDocumentType(v ...string) func(*GraphExploreRequest) {
  150. return func(r *GraphExploreRequest) {
  151. r.DocumentType = v
  152. }
  153. }
  154. // WithRouting - specific routing value.
  155. func (f GraphExplore) WithRouting(v string) func(*GraphExploreRequest) {
  156. return func(r *GraphExploreRequest) {
  157. r.Routing = v
  158. }
  159. }
  160. // WithTimeout - explicit operation timeout.
  161. func (f GraphExplore) WithTimeout(v time.Duration) func(*GraphExploreRequest) {
  162. return func(r *GraphExploreRequest) {
  163. r.Timeout = v
  164. }
  165. }
  166. // WithPretty makes the response body pretty-printed.
  167. func (f GraphExplore) WithPretty() func(*GraphExploreRequest) {
  168. return func(r *GraphExploreRequest) {
  169. r.Pretty = true
  170. }
  171. }
  172. // WithHuman makes statistical values human-readable.
  173. func (f GraphExplore) WithHuman() func(*GraphExploreRequest) {
  174. return func(r *GraphExploreRequest) {
  175. r.Human = true
  176. }
  177. }
  178. // WithErrorTrace includes the stack trace for errors in the response body.
  179. func (f GraphExplore) WithErrorTrace() func(*GraphExploreRequest) {
  180. return func(r *GraphExploreRequest) {
  181. r.ErrorTrace = true
  182. }
  183. }
  184. // WithFilterPath filters the properties of the response body.
  185. func (f GraphExplore) WithFilterPath(v ...string) func(*GraphExploreRequest) {
  186. return func(r *GraphExploreRequest) {
  187. r.FilterPath = v
  188. }
  189. }
  190. // WithHeader adds the headers to the HTTP request.
  191. func (f GraphExplore) WithHeader(h map[string]string) func(*GraphExploreRequest) {
  192. return func(r *GraphExploreRequest) {
  193. if r.Header == nil {
  194. r.Header = make(http.Header)
  195. }
  196. for k, v := range h {
  197. r.Header.Add(k, v)
  198. }
  199. }
  200. }
  201. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  202. func (f GraphExplore) WithOpaqueID(s string) func(*GraphExploreRequest) {
  203. return func(r *GraphExploreRequest) {
  204. if r.Header == nil {
  205. r.Header = make(http.Header)
  206. }
  207. r.Header.Set("X-Opaque-Id", s)
  208. }
  209. }