api.search_mvt.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. "fmt"
  24. "io"
  25. "net/http"
  26. "strconv"
  27. "strings"
  28. )
  29. func newSearchMvtFunc(t Transport) SearchMvt {
  30. return func(index []string, field string, zoom *int, x *int, y *int, o ...func(*SearchMvtRequest)) (*Response, error) {
  31. var r = SearchMvtRequest{Index: index, Y: y, Field: field, Zoom: zoom, X: x}
  32. for _, f := range o {
  33. f(&r)
  34. }
  35. return r.Do(r.ctx, t)
  36. }
  37. }
  38. // ----- API Definition -------------------------------------------------------
  39. // SearchMvt searches a vector tile for geospatial values. Returns results as a binary Mapbox vector tile.
  40. //
  41. // This API is experimental.
  42. //
  43. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/search-vector-tile-api.html.
  44. type SearchMvt func(index []string, field string, zoom *int, x *int, y *int, o ...func(*SearchMvtRequest)) (*Response, error)
  45. // SearchMvtRequest configures the Search Mvt API request.
  46. type SearchMvtRequest struct {
  47. Index []string
  48. Body io.Reader
  49. Field string
  50. X *int
  51. Y *int
  52. Zoom *int
  53. ExactBounds *bool
  54. Extent *int
  55. GridPrecision *int
  56. GridType string
  57. Size *int
  58. TrackTotalHits interface{}
  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 SearchMvtRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  68. var (
  69. method string
  70. path strings.Builder
  71. params map[string]string
  72. )
  73. method = "POST"
  74. if len(r.Index) == 0 {
  75. return nil, errors.New("index is required and cannot be nil or empty")
  76. }
  77. if r.Zoom == nil {
  78. return nil, errors.New("zoom is required and cannot be nil")
  79. }
  80. if r.X == nil {
  81. return nil, errors.New("x is required and cannot be nil")
  82. }
  83. if r.Y == nil {
  84. return nil, errors.New("y is required and cannot be nil")
  85. }
  86. path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len("_mvt") + 1 + len(r.Field) + 1 + len(strconv.Itoa(*r.Zoom)) + 1 + len(strconv.Itoa(*r.X)) + 1 + len(strconv.Itoa(*r.Y)))
  87. path.WriteString("/")
  88. path.WriteString(strings.Join(r.Index, ","))
  89. path.WriteString("/")
  90. path.WriteString("_mvt")
  91. path.WriteString("/")
  92. path.WriteString(r.Field)
  93. path.WriteString("/")
  94. path.WriteString(strconv.Itoa(*r.Zoom))
  95. path.WriteString("/")
  96. path.WriteString(strconv.Itoa(*r.X))
  97. path.WriteString("/")
  98. path.WriteString(strconv.Itoa(*r.Y))
  99. params = make(map[string]string)
  100. if r.ExactBounds != nil {
  101. params["exact_bounds"] = strconv.FormatBool(*r.ExactBounds)
  102. }
  103. if r.Extent != nil {
  104. params["extent"] = strconv.FormatInt(int64(*r.Extent), 10)
  105. }
  106. if r.GridPrecision != nil {
  107. params["grid_precision"] = strconv.FormatInt(int64(*r.GridPrecision), 10)
  108. }
  109. if r.GridType != "" {
  110. params["grid_type"] = r.GridType
  111. }
  112. if r.Size != nil {
  113. params["size"] = strconv.FormatInt(int64(*r.Size), 10)
  114. }
  115. if r.TrackTotalHits != nil {
  116. params["track_total_hits"] = fmt.Sprintf("%v", r.TrackTotalHits)
  117. }
  118. if r.Pretty {
  119. params["pretty"] = "true"
  120. }
  121. if r.Human {
  122. params["human"] = "true"
  123. }
  124. if r.ErrorTrace {
  125. params["error_trace"] = "true"
  126. }
  127. if len(r.FilterPath) > 0 {
  128. params["filter_path"] = strings.Join(r.FilterPath, ",")
  129. }
  130. req, err := newRequest(method, path.String(), r.Body)
  131. if err != nil {
  132. return nil, err
  133. }
  134. if len(params) > 0 {
  135. q := req.URL.Query()
  136. for k, v := range params {
  137. q.Set(k, v)
  138. }
  139. req.URL.RawQuery = q.Encode()
  140. }
  141. if len(r.Header) > 0 {
  142. if len(req.Header) == 0 {
  143. req.Header = r.Header
  144. } else {
  145. for k, vv := range r.Header {
  146. for _, v := range vv {
  147. req.Header.Add(k, v)
  148. }
  149. }
  150. }
  151. }
  152. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  153. req.Header[headerContentType] = headerContentTypeJSON
  154. }
  155. if ctx != nil {
  156. req = req.WithContext(ctx)
  157. }
  158. res, err := transport.Perform(req)
  159. if err != nil {
  160. return nil, err
  161. }
  162. response := Response{
  163. StatusCode: res.StatusCode,
  164. Body: res.Body,
  165. Header: res.Header,
  166. }
  167. return &response, nil
  168. }
  169. // WithContext sets the request context.
  170. func (f SearchMvt) WithContext(v context.Context) func(*SearchMvtRequest) {
  171. return func(r *SearchMvtRequest) {
  172. r.ctx = v
  173. }
  174. }
  175. // WithBody - Search request body..
  176. func (f SearchMvt) WithBody(v io.Reader) func(*SearchMvtRequest) {
  177. return func(r *SearchMvtRequest) {
  178. r.Body = v
  179. }
  180. }
  181. // WithExactBounds - if false, the meta layer's feature is the bounding box of the tile. if true, the meta layer's feature is a bounding box resulting from a `geo_bounds` aggregation..
  182. func (f SearchMvt) WithExactBounds(v bool) func(*SearchMvtRequest) {
  183. return func(r *SearchMvtRequest) {
  184. r.ExactBounds = &v
  185. }
  186. }
  187. // WithExtent - size, in pixels, of a side of the vector tile..
  188. func (f SearchMvt) WithExtent(v int) func(*SearchMvtRequest) {
  189. return func(r *SearchMvtRequest) {
  190. r.Extent = &v
  191. }
  192. }
  193. // WithGridPrecision - additional zoom levels available through the aggs layer. accepts 0-8..
  194. func (f SearchMvt) WithGridPrecision(v int) func(*SearchMvtRequest) {
  195. return func(r *SearchMvtRequest) {
  196. r.GridPrecision = &v
  197. }
  198. }
  199. // WithGridType - determines the geometry type for features in the aggs layer..
  200. func (f SearchMvt) WithGridType(v string) func(*SearchMvtRequest) {
  201. return func(r *SearchMvtRequest) {
  202. r.GridType = v
  203. }
  204. }
  205. // WithSize - maximum number of features to return in the hits layer. accepts 0-10000..
  206. func (f SearchMvt) WithSize(v int) func(*SearchMvtRequest) {
  207. return func(r *SearchMvtRequest) {
  208. r.Size = &v
  209. }
  210. }
  211. // WithTrackTotalHits - indicate if the number of documents that match the query should be tracked. a number can also be specified, to accurately track the total hit count up to the number..
  212. func (f SearchMvt) WithTrackTotalHits(v interface{}) func(*SearchMvtRequest) {
  213. return func(r *SearchMvtRequest) {
  214. r.TrackTotalHits = v
  215. }
  216. }
  217. // WithPretty makes the response body pretty-printed.
  218. func (f SearchMvt) WithPretty() func(*SearchMvtRequest) {
  219. return func(r *SearchMvtRequest) {
  220. r.Pretty = true
  221. }
  222. }
  223. // WithHuman makes statistical values human-readable.
  224. func (f SearchMvt) WithHuman() func(*SearchMvtRequest) {
  225. return func(r *SearchMvtRequest) {
  226. r.Human = true
  227. }
  228. }
  229. // WithErrorTrace includes the stack trace for errors in the response body.
  230. func (f SearchMvt) WithErrorTrace() func(*SearchMvtRequest) {
  231. return func(r *SearchMvtRequest) {
  232. r.ErrorTrace = true
  233. }
  234. }
  235. // WithFilterPath filters the properties of the response body.
  236. func (f SearchMvt) WithFilterPath(v ...string) func(*SearchMvtRequest) {
  237. return func(r *SearchMvtRequest) {
  238. r.FilterPath = v
  239. }
  240. }
  241. // WithHeader adds the headers to the HTTP request.
  242. func (f SearchMvt) WithHeader(h map[string]string) func(*SearchMvtRequest) {
  243. return func(r *SearchMvtRequest) {
  244. if r.Header == nil {
  245. r.Header = make(http.Header)
  246. }
  247. for k, v := range h {
  248. r.Header.Add(k, v)
  249. }
  250. }
  251. }
  252. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  253. func (f SearchMvt) WithOpaqueID(s string) func(*SearchMvtRequest) {
  254. return func(r *SearchMvtRequest) {
  255. if r.Header == nil {
  256. r.Header = make(http.Header)
  257. }
  258. r.Header.Set("X-Opaque-Id", s)
  259. }
  260. }