api.search.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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. "fmt"
  23. "io"
  24. "net/http"
  25. "strconv"
  26. "strings"
  27. "time"
  28. )
  29. func newSearchFunc(t Transport) Search {
  30. return func(o ...func(*SearchRequest)) (*Response, error) {
  31. var r = SearchRequest{}
  32. for _, f := range o {
  33. f(&r)
  34. }
  35. return r.Do(r.ctx, t)
  36. }
  37. }
  38. // ----- API Definition -------------------------------------------------------
  39. // Search returns results matching a query.
  40. //
  41. // See full documentation at https://www.elastic.co/guide/en/elasticsearch/reference/master/search-search.html.
  42. type Search func(o ...func(*SearchRequest)) (*Response, error)
  43. // SearchRequest configures the Search API request.
  44. type SearchRequest struct {
  45. Index []string
  46. DocumentType []string
  47. Body io.Reader
  48. AllowNoIndices *bool
  49. AllowPartialSearchResults *bool
  50. Analyzer string
  51. AnalyzeWildcard *bool
  52. BatchedReduceSize *int
  53. CcsMinimizeRoundtrips *bool
  54. DefaultOperator string
  55. Df string
  56. DocvalueFields []string
  57. ExpandWildcards string
  58. Explain *bool
  59. From *int
  60. IgnoreThrottled *bool
  61. IgnoreUnavailable *bool
  62. Lenient *bool
  63. MaxConcurrentShardRequests *int
  64. MinCompatibleShardNode string
  65. Preference string
  66. PreFilterShardSize *int
  67. Query string
  68. RequestCache *bool
  69. RestTotalHitsAsInt *bool
  70. Routing []string
  71. Scroll time.Duration
  72. SearchType string
  73. SeqNoPrimaryTerm *bool
  74. Size *int
  75. Sort []string
  76. Source []string
  77. SourceExcludes []string
  78. SourceIncludes []string
  79. Stats []string
  80. StoredFields []string
  81. SuggestField string
  82. SuggestMode string
  83. SuggestSize *int
  84. SuggestText string
  85. TerminateAfter *int
  86. Timeout time.Duration
  87. TrackScores *bool
  88. TrackTotalHits interface{}
  89. TypedKeys *bool
  90. Version *bool
  91. Pretty bool
  92. Human bool
  93. ErrorTrace bool
  94. FilterPath []string
  95. Header http.Header
  96. ctx context.Context
  97. }
  98. // Do executes the request and returns response or error.
  99. func (r SearchRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
  100. var (
  101. method string
  102. path strings.Builder
  103. params map[string]string
  104. )
  105. method = "POST"
  106. path.Grow(1 + len(strings.Join(r.Index, ",")) + 1 + len(strings.Join(r.DocumentType, ",")) + 1 + len("_search"))
  107. if len(r.Index) > 0 {
  108. path.WriteString("/")
  109. path.WriteString(strings.Join(r.Index, ","))
  110. }
  111. if len(r.DocumentType) > 0 {
  112. path.WriteString("/")
  113. path.WriteString(strings.Join(r.DocumentType, ","))
  114. }
  115. path.WriteString("/")
  116. path.WriteString("_search")
  117. params = make(map[string]string)
  118. if r.AllowNoIndices != nil {
  119. params["allow_no_indices"] = strconv.FormatBool(*r.AllowNoIndices)
  120. }
  121. if r.AllowPartialSearchResults != nil {
  122. params["allow_partial_search_results"] = strconv.FormatBool(*r.AllowPartialSearchResults)
  123. }
  124. if r.Analyzer != "" {
  125. params["analyzer"] = r.Analyzer
  126. }
  127. if r.AnalyzeWildcard != nil {
  128. params["analyze_wildcard"] = strconv.FormatBool(*r.AnalyzeWildcard)
  129. }
  130. if r.BatchedReduceSize != nil {
  131. params["batched_reduce_size"] = strconv.FormatInt(int64(*r.BatchedReduceSize), 10)
  132. }
  133. if r.CcsMinimizeRoundtrips != nil {
  134. params["ccs_minimize_roundtrips"] = strconv.FormatBool(*r.CcsMinimizeRoundtrips)
  135. }
  136. if r.DefaultOperator != "" {
  137. params["default_operator"] = r.DefaultOperator
  138. }
  139. if r.Df != "" {
  140. params["df"] = r.Df
  141. }
  142. if len(r.DocvalueFields) > 0 {
  143. params["docvalue_fields"] = strings.Join(r.DocvalueFields, ",")
  144. }
  145. if r.ExpandWildcards != "" {
  146. params["expand_wildcards"] = r.ExpandWildcards
  147. }
  148. if r.Explain != nil {
  149. params["explain"] = strconv.FormatBool(*r.Explain)
  150. }
  151. if r.From != nil {
  152. params["from"] = strconv.FormatInt(int64(*r.From), 10)
  153. }
  154. if r.IgnoreThrottled != nil {
  155. params["ignore_throttled"] = strconv.FormatBool(*r.IgnoreThrottled)
  156. }
  157. if r.IgnoreUnavailable != nil {
  158. params["ignore_unavailable"] = strconv.FormatBool(*r.IgnoreUnavailable)
  159. }
  160. if r.Lenient != nil {
  161. params["lenient"] = strconv.FormatBool(*r.Lenient)
  162. }
  163. if r.MaxConcurrentShardRequests != nil {
  164. params["max_concurrent_shard_requests"] = strconv.FormatInt(int64(*r.MaxConcurrentShardRequests), 10)
  165. }
  166. if r.MinCompatibleShardNode != "" {
  167. params["min_compatible_shard_node"] = r.MinCompatibleShardNode
  168. }
  169. if r.Preference != "" {
  170. params["preference"] = r.Preference
  171. }
  172. if r.PreFilterShardSize != nil {
  173. params["pre_filter_shard_size"] = strconv.FormatInt(int64(*r.PreFilterShardSize), 10)
  174. }
  175. if r.Query != "" {
  176. params["q"] = r.Query
  177. }
  178. if r.RequestCache != nil {
  179. params["request_cache"] = strconv.FormatBool(*r.RequestCache)
  180. }
  181. if r.RestTotalHitsAsInt != nil {
  182. params["rest_total_hits_as_int"] = strconv.FormatBool(*r.RestTotalHitsAsInt)
  183. }
  184. if len(r.Routing) > 0 {
  185. params["routing"] = strings.Join(r.Routing, ",")
  186. }
  187. if r.Scroll != 0 {
  188. params["scroll"] = formatDuration(r.Scroll)
  189. }
  190. if r.SearchType != "" {
  191. params["search_type"] = r.SearchType
  192. }
  193. if r.SeqNoPrimaryTerm != nil {
  194. params["seq_no_primary_term"] = strconv.FormatBool(*r.SeqNoPrimaryTerm)
  195. }
  196. if r.Size != nil {
  197. params["size"] = strconv.FormatInt(int64(*r.Size), 10)
  198. }
  199. if len(r.Sort) > 0 {
  200. params["sort"] = strings.Join(r.Sort, ",")
  201. }
  202. if len(r.Source) > 0 {
  203. params["_source"] = strings.Join(r.Source, ",")
  204. }
  205. if len(r.SourceExcludes) > 0 {
  206. params["_source_excludes"] = strings.Join(r.SourceExcludes, ",")
  207. }
  208. if len(r.SourceIncludes) > 0 {
  209. params["_source_includes"] = strings.Join(r.SourceIncludes, ",")
  210. }
  211. if len(r.Stats) > 0 {
  212. params["stats"] = strings.Join(r.Stats, ",")
  213. }
  214. if len(r.StoredFields) > 0 {
  215. params["stored_fields"] = strings.Join(r.StoredFields, ",")
  216. }
  217. if r.SuggestField != "" {
  218. params["suggest_field"] = r.SuggestField
  219. }
  220. if r.SuggestMode != "" {
  221. params["suggest_mode"] = r.SuggestMode
  222. }
  223. if r.SuggestSize != nil {
  224. params["suggest_size"] = strconv.FormatInt(int64(*r.SuggestSize), 10)
  225. }
  226. if r.SuggestText != "" {
  227. params["suggest_text"] = r.SuggestText
  228. }
  229. if r.TerminateAfter != nil {
  230. params["terminate_after"] = strconv.FormatInt(int64(*r.TerminateAfter), 10)
  231. }
  232. if r.Timeout != 0 {
  233. params["timeout"] = formatDuration(r.Timeout)
  234. }
  235. if r.TrackScores != nil {
  236. params["track_scores"] = strconv.FormatBool(*r.TrackScores)
  237. }
  238. if r.TrackTotalHits != nil {
  239. params["track_total_hits"] = fmt.Sprintf("%v", r.TrackTotalHits)
  240. }
  241. if r.TypedKeys != nil {
  242. params["typed_keys"] = strconv.FormatBool(*r.TypedKeys)
  243. }
  244. if r.Version != nil {
  245. params["version"] = strconv.FormatBool(*r.Version)
  246. }
  247. if r.Pretty {
  248. params["pretty"] = "true"
  249. }
  250. if r.Human {
  251. params["human"] = "true"
  252. }
  253. if r.ErrorTrace {
  254. params["error_trace"] = "true"
  255. }
  256. if len(r.FilterPath) > 0 {
  257. params["filter_path"] = strings.Join(r.FilterPath, ",")
  258. }
  259. req, err := newRequest(method, path.String(), r.Body)
  260. if err != nil {
  261. return nil, err
  262. }
  263. if len(params) > 0 {
  264. q := req.URL.Query()
  265. for k, v := range params {
  266. q.Set(k, v)
  267. }
  268. req.URL.RawQuery = q.Encode()
  269. }
  270. if len(r.Header) > 0 {
  271. if len(req.Header) == 0 {
  272. req.Header = r.Header
  273. } else {
  274. for k, vv := range r.Header {
  275. for _, v := range vv {
  276. req.Header.Add(k, v)
  277. }
  278. }
  279. }
  280. }
  281. if r.Body != nil && req.Header.Get(headerContentType) == "" {
  282. req.Header[headerContentType] = headerContentTypeJSON
  283. }
  284. if ctx != nil {
  285. req = req.WithContext(ctx)
  286. }
  287. res, err := transport.Perform(req)
  288. if err != nil {
  289. return nil, err
  290. }
  291. response := Response{
  292. StatusCode: res.StatusCode,
  293. Body: res.Body,
  294. Header: res.Header,
  295. }
  296. return &response, nil
  297. }
  298. // WithContext sets the request context.
  299. func (f Search) WithContext(v context.Context) func(*SearchRequest) {
  300. return func(r *SearchRequest) {
  301. r.ctx = v
  302. }
  303. }
  304. // WithBody - The search definition using the Query DSL.
  305. func (f Search) WithBody(v io.Reader) func(*SearchRequest) {
  306. return func(r *SearchRequest) {
  307. r.Body = v
  308. }
  309. }
  310. // WithIndex - a list of index names to search; use _all to perform the operation on all indices.
  311. func (f Search) WithIndex(v ...string) func(*SearchRequest) {
  312. return func(r *SearchRequest) {
  313. r.Index = v
  314. }
  315. }
  316. // WithDocumentType - a list of document types to search; leave empty to perform the operation on all types.
  317. func (f Search) WithDocumentType(v ...string) func(*SearchRequest) {
  318. return func(r *SearchRequest) {
  319. r.DocumentType = v
  320. }
  321. }
  322. // 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).
  323. func (f Search) WithAllowNoIndices(v bool) func(*SearchRequest) {
  324. return func(r *SearchRequest) {
  325. r.AllowNoIndices = &v
  326. }
  327. }
  328. // WithAllowPartialSearchResults - indicate if an error should be returned if there is a partial search failure or timeout.
  329. func (f Search) WithAllowPartialSearchResults(v bool) func(*SearchRequest) {
  330. return func(r *SearchRequest) {
  331. r.AllowPartialSearchResults = &v
  332. }
  333. }
  334. // WithAnalyzer - the analyzer to use for the query string.
  335. func (f Search) WithAnalyzer(v string) func(*SearchRequest) {
  336. return func(r *SearchRequest) {
  337. r.Analyzer = v
  338. }
  339. }
  340. // WithAnalyzeWildcard - specify whether wildcard and prefix queries should be analyzed (default: false).
  341. func (f Search) WithAnalyzeWildcard(v bool) func(*SearchRequest) {
  342. return func(r *SearchRequest) {
  343. r.AnalyzeWildcard = &v
  344. }
  345. }
  346. // WithBatchedReduceSize - the number of shard results that should be reduced at once on the coordinating node. this value should be used as a protection mechanism to reduce the memory overhead per search request if the potential number of shards in the request can be large..
  347. func (f Search) WithBatchedReduceSize(v int) func(*SearchRequest) {
  348. return func(r *SearchRequest) {
  349. r.BatchedReduceSize = &v
  350. }
  351. }
  352. // WithCcsMinimizeRoundtrips - indicates whether network round-trips should be minimized as part of cross-cluster search requests execution.
  353. func (f Search) WithCcsMinimizeRoundtrips(v bool) func(*SearchRequest) {
  354. return func(r *SearchRequest) {
  355. r.CcsMinimizeRoundtrips = &v
  356. }
  357. }
  358. // WithDefaultOperator - the default operator for query string query (and or or).
  359. func (f Search) WithDefaultOperator(v string) func(*SearchRequest) {
  360. return func(r *SearchRequest) {
  361. r.DefaultOperator = v
  362. }
  363. }
  364. // WithDf - the field to use as default where no field prefix is given in the query string.
  365. func (f Search) WithDf(v string) func(*SearchRequest) {
  366. return func(r *SearchRequest) {
  367. r.Df = v
  368. }
  369. }
  370. // WithDocvalueFields - a list of fields to return as the docvalue representation of a field for each hit.
  371. func (f Search) WithDocvalueFields(v ...string) func(*SearchRequest) {
  372. return func(r *SearchRequest) {
  373. r.DocvalueFields = v
  374. }
  375. }
  376. // WithExpandWildcards - whether to expand wildcard expression to concrete indices that are open, closed or both..
  377. func (f Search) WithExpandWildcards(v string) func(*SearchRequest) {
  378. return func(r *SearchRequest) {
  379. r.ExpandWildcards = v
  380. }
  381. }
  382. // WithExplain - specify whether to return detailed information about score computation as part of a hit.
  383. func (f Search) WithExplain(v bool) func(*SearchRequest) {
  384. return func(r *SearchRequest) {
  385. r.Explain = &v
  386. }
  387. }
  388. // WithFrom - starting offset (default: 0).
  389. func (f Search) WithFrom(v int) func(*SearchRequest) {
  390. return func(r *SearchRequest) {
  391. r.From = &v
  392. }
  393. }
  394. // WithIgnoreThrottled - whether specified concrete, expanded or aliased indices should be ignored when throttled.
  395. func (f Search) WithIgnoreThrottled(v bool) func(*SearchRequest) {
  396. return func(r *SearchRequest) {
  397. r.IgnoreThrottled = &v
  398. }
  399. }
  400. // WithIgnoreUnavailable - whether specified concrete indices should be ignored when unavailable (missing or closed).
  401. func (f Search) WithIgnoreUnavailable(v bool) func(*SearchRequest) {
  402. return func(r *SearchRequest) {
  403. r.IgnoreUnavailable = &v
  404. }
  405. }
  406. // WithLenient - specify whether format-based query failures (such as providing text to a numeric field) should be ignored.
  407. func (f Search) WithLenient(v bool) func(*SearchRequest) {
  408. return func(r *SearchRequest) {
  409. r.Lenient = &v
  410. }
  411. }
  412. // WithMaxConcurrentShardRequests - the number of concurrent shard requests per node this search executes concurrently. this value should be used to limit the impact of the search on the cluster in order to limit the number of concurrent shard requests.
  413. func (f Search) WithMaxConcurrentShardRequests(v int) func(*SearchRequest) {
  414. return func(r *SearchRequest) {
  415. r.MaxConcurrentShardRequests = &v
  416. }
  417. }
  418. // WithMinCompatibleShardNode - the minimum compatible version that all shards involved in search should have for this request to be successful.
  419. func (f Search) WithMinCompatibleShardNode(v string) func(*SearchRequest) {
  420. return func(r *SearchRequest) {
  421. r.MinCompatibleShardNode = v
  422. }
  423. }
  424. // WithPreference - specify the node or shard the operation should be performed on (default: random).
  425. func (f Search) WithPreference(v string) func(*SearchRequest) {
  426. return func(r *SearchRequest) {
  427. r.Preference = v
  428. }
  429. }
  430. // WithPreFilterShardSize - a threshold that enforces a pre-filter roundtrip to prefilter search shards based on query rewriting if the number of shards the search request expands to exceeds the threshold. this filter roundtrip can limit the number of shards significantly if for instance a shard can not match any documents based on its rewrite method ie. if date filters are mandatory to match but the shard bounds and the query are disjoint..
  431. func (f Search) WithPreFilterShardSize(v int) func(*SearchRequest) {
  432. return func(r *SearchRequest) {
  433. r.PreFilterShardSize = &v
  434. }
  435. }
  436. // WithQuery - query in the lucene query string syntax.
  437. func (f Search) WithQuery(v string) func(*SearchRequest) {
  438. return func(r *SearchRequest) {
  439. r.Query = v
  440. }
  441. }
  442. // WithRequestCache - specify if request cache should be used for this request or not, defaults to index level setting.
  443. func (f Search) WithRequestCache(v bool) func(*SearchRequest) {
  444. return func(r *SearchRequest) {
  445. r.RequestCache = &v
  446. }
  447. }
  448. // WithRestTotalHitsAsInt - indicates whether hits.total should be rendered as an integer or an object in the rest search response.
  449. func (f Search) WithRestTotalHitsAsInt(v bool) func(*SearchRequest) {
  450. return func(r *SearchRequest) {
  451. r.RestTotalHitsAsInt = &v
  452. }
  453. }
  454. // WithRouting - a list of specific routing values.
  455. func (f Search) WithRouting(v ...string) func(*SearchRequest) {
  456. return func(r *SearchRequest) {
  457. r.Routing = v
  458. }
  459. }
  460. // WithScroll - specify how long a consistent view of the index should be maintained for scrolled search.
  461. func (f Search) WithScroll(v time.Duration) func(*SearchRequest) {
  462. return func(r *SearchRequest) {
  463. r.Scroll = v
  464. }
  465. }
  466. // WithSearchType - search operation type.
  467. func (f Search) WithSearchType(v string) func(*SearchRequest) {
  468. return func(r *SearchRequest) {
  469. r.SearchType = v
  470. }
  471. }
  472. // WithSeqNoPrimaryTerm - specify whether to return sequence number and primary term of the last modification of each hit.
  473. func (f Search) WithSeqNoPrimaryTerm(v bool) func(*SearchRequest) {
  474. return func(r *SearchRequest) {
  475. r.SeqNoPrimaryTerm = &v
  476. }
  477. }
  478. // WithSize - number of hits to return (default: 10).
  479. func (f Search) WithSize(v int) func(*SearchRequest) {
  480. return func(r *SearchRequest) {
  481. r.Size = &v
  482. }
  483. }
  484. // WithSort - a list of <field>:<direction> pairs.
  485. func (f Search) WithSort(v ...string) func(*SearchRequest) {
  486. return func(r *SearchRequest) {
  487. r.Sort = v
  488. }
  489. }
  490. // WithSource - true or false to return the _source field or not, or a list of fields to return.
  491. func (f Search) WithSource(v ...string) func(*SearchRequest) {
  492. return func(r *SearchRequest) {
  493. r.Source = v
  494. }
  495. }
  496. // WithSourceExcludes - a list of fields to exclude from the returned _source field.
  497. func (f Search) WithSourceExcludes(v ...string) func(*SearchRequest) {
  498. return func(r *SearchRequest) {
  499. r.SourceExcludes = v
  500. }
  501. }
  502. // WithSourceIncludes - a list of fields to extract and return from the _source field.
  503. func (f Search) WithSourceIncludes(v ...string) func(*SearchRequest) {
  504. return func(r *SearchRequest) {
  505. r.SourceIncludes = v
  506. }
  507. }
  508. // WithStats - specific 'tag' of the request for logging and statistical purposes.
  509. func (f Search) WithStats(v ...string) func(*SearchRequest) {
  510. return func(r *SearchRequest) {
  511. r.Stats = v
  512. }
  513. }
  514. // WithStoredFields - a list of stored fields to return as part of a hit.
  515. func (f Search) WithStoredFields(v ...string) func(*SearchRequest) {
  516. return func(r *SearchRequest) {
  517. r.StoredFields = v
  518. }
  519. }
  520. // WithSuggestField - specify which field to use for suggestions.
  521. func (f Search) WithSuggestField(v string) func(*SearchRequest) {
  522. return func(r *SearchRequest) {
  523. r.SuggestField = v
  524. }
  525. }
  526. // WithSuggestMode - specify suggest mode.
  527. func (f Search) WithSuggestMode(v string) func(*SearchRequest) {
  528. return func(r *SearchRequest) {
  529. r.SuggestMode = v
  530. }
  531. }
  532. // WithSuggestSize - how many suggestions to return in response.
  533. func (f Search) WithSuggestSize(v int) func(*SearchRequest) {
  534. return func(r *SearchRequest) {
  535. r.SuggestSize = &v
  536. }
  537. }
  538. // WithSuggestText - the source text for which the suggestions should be returned.
  539. func (f Search) WithSuggestText(v string) func(*SearchRequest) {
  540. return func(r *SearchRequest) {
  541. r.SuggestText = v
  542. }
  543. }
  544. // WithTerminateAfter - the maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early..
  545. func (f Search) WithTerminateAfter(v int) func(*SearchRequest) {
  546. return func(r *SearchRequest) {
  547. r.TerminateAfter = &v
  548. }
  549. }
  550. // WithTimeout - explicit operation timeout.
  551. func (f Search) WithTimeout(v time.Duration) func(*SearchRequest) {
  552. return func(r *SearchRequest) {
  553. r.Timeout = v
  554. }
  555. }
  556. // WithTrackScores - whether to calculate and return scores even if they are not used for sorting.
  557. func (f Search) WithTrackScores(v bool) func(*SearchRequest) {
  558. return func(r *SearchRequest) {
  559. r.TrackScores = &v
  560. }
  561. }
  562. // WithTrackTotalHits - indicate if the number of documents that match the query should be tracked.
  563. func (f Search) WithTrackTotalHits(v interface{}) func(*SearchRequest) {
  564. return func(r *SearchRequest) {
  565. r.TrackTotalHits = v
  566. }
  567. }
  568. // WithTypedKeys - specify whether aggregation and suggester names should be prefixed by their respective types in the response.
  569. func (f Search) WithTypedKeys(v bool) func(*SearchRequest) {
  570. return func(r *SearchRequest) {
  571. r.TypedKeys = &v
  572. }
  573. }
  574. // WithVersion - specify whether to return document version as part of a hit.
  575. func (f Search) WithVersion(v bool) func(*SearchRequest) {
  576. return func(r *SearchRequest) {
  577. r.Version = &v
  578. }
  579. }
  580. // WithPretty makes the response body pretty-printed.
  581. func (f Search) WithPretty() func(*SearchRequest) {
  582. return func(r *SearchRequest) {
  583. r.Pretty = true
  584. }
  585. }
  586. // WithHuman makes statistical values human-readable.
  587. func (f Search) WithHuman() func(*SearchRequest) {
  588. return func(r *SearchRequest) {
  589. r.Human = true
  590. }
  591. }
  592. // WithErrorTrace includes the stack trace for errors in the response body.
  593. func (f Search) WithErrorTrace() func(*SearchRequest) {
  594. return func(r *SearchRequest) {
  595. r.ErrorTrace = true
  596. }
  597. }
  598. // WithFilterPath filters the properties of the response body.
  599. func (f Search) WithFilterPath(v ...string) func(*SearchRequest) {
  600. return func(r *SearchRequest) {
  601. r.FilterPath = v
  602. }
  603. }
  604. // WithHeader adds the headers to the HTTP request.
  605. func (f Search) WithHeader(h map[string]string) func(*SearchRequest) {
  606. return func(r *SearchRequest) {
  607. if r.Header == nil {
  608. r.Header = make(http.Header)
  609. }
  610. for k, v := range h {
  611. r.Header.Add(k, v)
  612. }
  613. }
  614. }
  615. // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
  616. func (f Search) WithOpaqueID(s string) func(*SearchRequest) {
  617. return func(r *SearchRequest) {
  618. if r.Header == nil {
  619. r.Header = make(http.Header)
  620. }
  621. r.Header.Set("X-Opaque-Id", s)
  622. }
  623. }