api.termvectors.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 newTermvectorsFunc(t Transport) Termvectors {
  28. return func(index string, o ...func(*TermvectorsRequest)) (*Response, error) {
  29. var r = TermvectorsRequest{Index: index}
  30. for _, f := range o {
  31. f(&r)
  32. }
  33. return r.Do(r.ctx, t)
  34. }
  35. }
  36. // ----- API Definition -------------------------------------------------------
  37. // Termvectors returns information and statistics about terms in the fields of a particular document.
  38. //
  39. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-termvectors.html.
  40. type Termvectors func(index string, o ...func(*TermvectorsRequest)) (*Response, error)
  41. // TermvectorsRequest configures the Termvectors API request.
  42. type TermvectorsRequest struct {
  43. Index string
  44. DocumentType string
  45. DocumentID string
  46. Body io.Reader
  47. Fields []string
  48. FieldStatistics *bool
  49. Offsets *bool
  50. Payloads *bool
  51. Positions *bool
  52. Preference string
  53. Realtime *bool
  54. Routing string
  55. TermStatistics *bool
  56. Version *int
  57. VersionType string
  58. Pretty bool
  59. Human bool
  60. ErrorTrace bool
  61. FilterPath []string
  62. Header http.Header
  63. ctx context.Context
  64. }
  65. // Do executes the request and returns response or error.
  66. func (r TermvectorsRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  67. var (
  68. method string
  69. path strings.Builder
  70. params map[string]string
  71. )
  72. method = "POST"
  73. if r.DocumentType == "" {
  74. r.DocumentType = "_doc"
  75. }
  76. path.Grow(1 + len(r.Index) + 1 + len(r.DocumentType) + 1 + len(r.DocumentID) + 1 + len("_termvectors"))
  77. path.WriteString("/")
  78. path.WriteString(r.Index)
  79. if r.DocumentType != "" {
  80. path.WriteString("/")
  81. path.WriteString(r.DocumentType)
  82. }
  83. if r.DocumentID != "" {
  84. path.WriteString("/")
  85. path.WriteString(r.DocumentID)
  86. }
  87. path.WriteString("/")
  88. path.WriteString("_termvectors")
  89. params = make(map[string]string)
  90. if len(r.Fields) > 0 {
  91. params["fields"] = strings.Join(r.Fields, ",")
  92. }
  93. if r.FieldStatistics != nil {
  94. params["field_statistics"] = strconv.FormatBool(*r.FieldStatistics)
  95. }
  96. if r.Offsets != nil {
  97. params["offsets"] = strconv.FormatBool(*r.Offsets)
  98. }
  99. if r.Payloads != nil {
  100. params["payloads"] = strconv.FormatBool(*r.Payloads)
  101. }
  102. if r.Positions != nil {
  103. params["positions"] = strconv.FormatBool(*r.Positions)
  104. }
  105. if r.Preference != "" {
  106. params["preference"] = r.Preference
  107. }
  108. if r.Realtime != nil {
  109. params["realtime"] = strconv.FormatBool(*r.Realtime)
  110. }
  111. if r.Routing != "" {
  112. params["routing"] = r.Routing
  113. }
  114. if r.TermStatistics != nil {
  115. params["term_statistics"] = strconv.FormatBool(*r.TermStatistics)
  116. }
  117. if r.Version != nil {
  118. params["version"] = strconv.FormatInt(int64(*r.Version), 10)
  119. }
  120. if r.VersionType != "" {
  121. params["version_type"] = r.VersionType
  122. }
  123. if r.Pretty {
  124. params["pretty"] = "true"
  125. }
  126. if r.Human {
  127. params["human"] = "true"
  128. }
  129. if r.ErrorTrace {
  130. params["error_trace"] = "true"
  131. }
  132. if len(r.FilterPath) > 0 {
  133. params["filter_path"] = strings.Join(r.FilterPath, ",")
  134. }
  135. req, err := newRequest(method, path.String(), r.Body)
  136. if err != nil {
  137. return nil, err
  138. }
  139. if len(params) > 0 {
  140. q := req.URL.Query()
  141. for k, v := range params {
  142. q.Set(k, v)
  143. }
  144. req.URL.RawQuery = q.Encode()
  145. }
  146. if len(r.Header) > 0 {
  147. if len(req.Header) == 0 {
  148. req.Header = r.Header
  149. } else {
  150. for k, vv := range r.Header {
  151. for _, v := range vv {
  152. req.Header.Add(k, v)
  153. }
  154. }
  155. }
  156. }
  157. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  158. req.Header[headerContentType] = headerContentTypeJSON
  159. }
  160. if ctx != nil {
  161. req = req.WithContext(ctx)
  162. }
  163. res, err := transport.Perform(req)
  164. if err != nil {
  165. return nil, err
  166. }
  167. response := Response{
  168. StatusCode: res.StatusCode,
  169. Body: res.Body,
  170. Header: res.Header,
  171. }
  172. return &response, nil
  173. }
  174. // WithContext sets the request context.
  175. func (f Termvectors) WithContext(v context.Context) func(*TermvectorsRequest) {
  176. return func(r *TermvectorsRequest) {
  177. r.ctx = v
  178. }
  179. }
  180. // WithBody - Define parameters and or supply a document to get termvectors for. See documentation..
  181. func (f Termvectors) WithBody(v io.Reader) func(*TermvectorsRequest) {
  182. return func(r *TermvectorsRequest) {
  183. r.Body = v
  184. }
  185. }
  186. // WithDocumentID - the ID of the document, when not specified a doc param should be supplied..
  187. func (f Termvectors) WithDocumentID(v string) func(*TermvectorsRequest) {
  188. return func(r *TermvectorsRequest) {
  189. r.DocumentID = v
  190. }
  191. }
  192. // WithDocumentType - the type of the document..
  193. func (f Termvectors) WithDocumentType(v string) func(*TermvectorsRequest) {
  194. return func(r *TermvectorsRequest) {
  195. r.DocumentType = v
  196. }
  197. }
  198. // WithFields - a list of fields to return..
  199. func (f Termvectors) WithFields(v ...string) func(*TermvectorsRequest) {
  200. return func(r *TermvectorsRequest) {
  201. r.Fields = v
  202. }
  203. }
  204. // WithFieldStatistics - specifies if document count, sum of document frequencies and sum of total term frequencies should be returned..
  205. func (f Termvectors) WithFieldStatistics(v bool) func(*TermvectorsRequest) {
  206. return func(r *TermvectorsRequest) {
  207. r.FieldStatistics = &v
  208. }
  209. }
  210. // WithOffsets - specifies if term offsets should be returned..
  211. func (f Termvectors) WithOffsets(v bool) func(*TermvectorsRequest) {
  212. return func(r *TermvectorsRequest) {
  213. r.Offsets = &v
  214. }
  215. }
  216. // WithPayloads - specifies if term payloads should be returned..
  217. func (f Termvectors) WithPayloads(v bool) func(*TermvectorsRequest) {
  218. return func(r *TermvectorsRequest) {
  219. r.Payloads = &v
  220. }
  221. }
  222. // WithPositions - specifies if term positions should be returned..
  223. func (f Termvectors) WithPositions(v bool) func(*TermvectorsRequest) {
  224. return func(r *TermvectorsRequest) {
  225. r.Positions = &v
  226. }
  227. }
  228. // WithPreference - specify the node or shard the operation should be performed on (default: random)..
  229. func (f Termvectors) WithPreference(v string) func(*TermvectorsRequest) {
  230. return func(r *TermvectorsRequest) {
  231. r.Preference = v
  232. }
  233. }
  234. // WithRealtime - specifies if request is real-time as opposed to near-real-time (default: true)..
  235. func (f Termvectors) WithRealtime(v bool) func(*TermvectorsRequest) {
  236. return func(r *TermvectorsRequest) {
  237. r.Realtime = &v
  238. }
  239. }
  240. // WithRouting - specific routing value..
  241. func (f Termvectors) WithRouting(v string) func(*TermvectorsRequest) {
  242. return func(r *TermvectorsRequest) {
  243. r.Routing = v
  244. }
  245. }
  246. // WithTermStatistics - specifies if total term frequency and document frequency should be returned..
  247. func (f Termvectors) WithTermStatistics(v bool) func(*TermvectorsRequest) {
  248. return func(r *TermvectorsRequest) {
  249. r.TermStatistics = &v
  250. }
  251. }
  252. // WithVersion - explicit version number for concurrency control.
  253. func (f Termvectors) WithVersion(v int) func(*TermvectorsRequest) {
  254. return func(r *TermvectorsRequest) {
  255. r.Version = &v
  256. }
  257. }
  258. // WithVersionType - specific version type.
  259. func (f Termvectors) WithVersionType(v string) func(*TermvectorsRequest) {
  260. return func(r *TermvectorsRequest) {
  261. r.VersionType = v
  262. }
  263. }
  264. // WithPretty makes the response body pretty-printed.
  265. func (f Termvectors) WithPretty() func(*TermvectorsRequest) {
  266. return func(r *TermvectorsRequest) {
  267. r.Pretty = true
  268. }
  269. }
  270. // WithHuman makes statistical values human-readable.
  271. func (f Termvectors) WithHuman() func(*TermvectorsRequest) {
  272. return func(r *TermvectorsRequest) {
  273. r.Human = true
  274. }
  275. }
  276. // WithErrorTrace includes the stack trace for errors in the response body.
  277. func (f Termvectors) WithErrorTrace() func(*TermvectorsRequest) {
  278. return func(r *TermvectorsRequest) {
  279. r.ErrorTrace = true
  280. }
  281. }
  282. // WithFilterPath filters the properties of the response body.
  283. func (f Termvectors) WithFilterPath(v ...string) func(*TermvectorsRequest) {
  284. return func(r *TermvectorsRequest) {
  285. r.FilterPath = v
  286. }
  287. }
  288. // WithHeader adds the headers to the HTTP request.
  289. func (f Termvectors) WithHeader(h map[string]string) func(*TermvectorsRequest) {
  290. return func(r *TermvectorsRequest) {
  291. if r.Header == nil {
  292. r.Header = make(http.Header)
  293. }
  294. for k, v := range h {
  295. r.Header.Add(k, v)
  296. }
  297. }
  298. }
  299. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  300. func (f Termvectors) WithOpaqueID(s string) func(*TermvectorsRequest) {
  301. return func(r *TermvectorsRequest) {
  302. if r.Header == nil {
  303. r.Header = make(http.Header)
  304. }
  305. r.Header.Set("X-Opaque-Id", s)
  306. }
  307. }