scroll.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "context"
  7. "fmt"
  8. "io"
  9. "net/url"
  10. "strings"
  11. "sync"
  12. "gopkg.in/olivere/elastic.v5/uritemplates"
  13. )
  14. const (
  15. // DefaultScrollKeepAlive is the default time a scroll cursor will be kept alive.
  16. DefaultScrollKeepAlive = "5m"
  17. )
  18. // ScrollService iterates over pages of search results from Elasticsearch.
  19. type ScrollService struct {
  20. client *Client
  21. indices []string
  22. types []string
  23. keepAlive string
  24. body interface{}
  25. ss *SearchSource
  26. size *int
  27. pretty bool
  28. routing string
  29. preference string
  30. ignoreUnavailable *bool
  31. allowNoIndices *bool
  32. expandWildcards string
  33. mu sync.RWMutex
  34. scrollId string
  35. }
  36. // NewScrollService initializes and returns a new ScrollService.
  37. func NewScrollService(client *Client) *ScrollService {
  38. builder := &ScrollService{
  39. client: client,
  40. ss: NewSearchSource(),
  41. keepAlive: DefaultScrollKeepAlive,
  42. }
  43. return builder
  44. }
  45. // Index sets the name of one or more indices to iterate over.
  46. func (s *ScrollService) Index(indices ...string) *ScrollService {
  47. if s.indices == nil {
  48. s.indices = make([]string, 0)
  49. }
  50. s.indices = append(s.indices, indices...)
  51. return s
  52. }
  53. // Type sets the name of one or more types to iterate over.
  54. func (s *ScrollService) Type(types ...string) *ScrollService {
  55. if s.types == nil {
  56. s.types = make([]string, 0)
  57. }
  58. s.types = append(s.types, types...)
  59. return s
  60. }
  61. // Scroll is an alias for KeepAlive, the time to keep
  62. // the cursor alive (e.g. "5m" for 5 minutes).
  63. func (s *ScrollService) Scroll(keepAlive string) *ScrollService {
  64. s.keepAlive = keepAlive
  65. return s
  66. }
  67. // KeepAlive sets the maximum time after which the cursor will expire.
  68. // It is "2m" by default.
  69. func (s *ScrollService) KeepAlive(keepAlive string) *ScrollService {
  70. s.keepAlive = keepAlive
  71. return s
  72. }
  73. // Size specifies the number of documents Elasticsearch should return
  74. // from each shard, per page.
  75. func (s *ScrollService) Size(size int) *ScrollService {
  76. s.size = &size
  77. return s
  78. }
  79. // Body sets the raw body to send to Elasticsearch. This can be e.g. a string,
  80. // a map[string]interface{} or anything that can be serialized into JSON.
  81. // Notice that setting the body disables the use of SearchSource and many
  82. // other properties of the ScanService.
  83. func (s *ScrollService) Body(body interface{}) *ScrollService {
  84. s.body = body
  85. return s
  86. }
  87. // SearchSource sets the search source builder to use with this iterator.
  88. // Notice that only a certain number of properties can be used when scrolling,
  89. // e.g. query and sorting.
  90. func (s *ScrollService) SearchSource(searchSource *SearchSource) *ScrollService {
  91. s.ss = searchSource
  92. if s.ss == nil {
  93. s.ss = NewSearchSource()
  94. }
  95. return s
  96. }
  97. // Query sets the query to perform, e.g. a MatchAllQuery.
  98. func (s *ScrollService) Query(query Query) *ScrollService {
  99. s.ss = s.ss.Query(query)
  100. return s
  101. }
  102. // PostFilter is executed as the last filter. It only affects the
  103. // search hits but not facets. See
  104. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-request-post-filter.html
  105. // for details.
  106. func (s *ScrollService) PostFilter(postFilter Query) *ScrollService {
  107. s.ss = s.ss.PostFilter(postFilter)
  108. return s
  109. }
  110. // Slice allows slicing the scroll request into several batches.
  111. // This is supported in Elasticsearch 5.0 or later.
  112. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-request-scroll.html#sliced-scroll
  113. // for details.
  114. func (s *ScrollService) Slice(sliceQuery Query) *ScrollService {
  115. s.ss = s.ss.Slice(sliceQuery)
  116. return s
  117. }
  118. // FetchSource indicates whether the response should contain the stored
  119. // _source for every hit.
  120. func (s *ScrollService) FetchSource(fetchSource bool) *ScrollService {
  121. s.ss = s.ss.FetchSource(fetchSource)
  122. return s
  123. }
  124. // FetchSourceContext indicates how the _source should be fetched.
  125. func (s *ScrollService) FetchSourceContext(fetchSourceContext *FetchSourceContext) *ScrollService {
  126. s.ss = s.ss.FetchSourceContext(fetchSourceContext)
  127. return s
  128. }
  129. // Version can be set to true to return a version for each search hit.
  130. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-request-version.html.
  131. func (s *ScrollService) Version(version bool) *ScrollService {
  132. s.ss = s.ss.Version(version)
  133. return s
  134. }
  135. // Sort adds a sort order. This can have negative effects on the performance
  136. // of the scroll operation as Elasticsearch needs to sort first.
  137. func (s *ScrollService) Sort(field string, ascending bool) *ScrollService {
  138. s.ss = s.ss.Sort(field, ascending)
  139. return s
  140. }
  141. // SortWithInfo specifies a sort order. Notice that sorting can have a
  142. // negative impact on scroll performance.
  143. func (s *ScrollService) SortWithInfo(info SortInfo) *ScrollService {
  144. s.ss = s.ss.SortWithInfo(info)
  145. return s
  146. }
  147. // SortBy specifies a sort order. Notice that sorting can have a
  148. // negative impact on scroll performance.
  149. func (s *ScrollService) SortBy(sorter ...Sorter) *ScrollService {
  150. s.ss = s.ss.SortBy(sorter...)
  151. return s
  152. }
  153. // Pretty asks Elasticsearch to pretty-print the returned JSON.
  154. func (s *ScrollService) Pretty(pretty bool) *ScrollService {
  155. s.pretty = pretty
  156. return s
  157. }
  158. // Routing is a list of specific routing values to control the shards
  159. // the search will be executed on.
  160. func (s *ScrollService) Routing(routings ...string) *ScrollService {
  161. s.routing = strings.Join(routings, ",")
  162. return s
  163. }
  164. // Preference sets the preference to execute the search. Defaults to
  165. // randomize across shards ("random"). Can be set to "_local" to prefer
  166. // local shards, "_primary" to execute on primary shards only,
  167. // or a custom value which guarantees that the same order will be used
  168. // across different requests.
  169. func (s *ScrollService) Preference(preference string) *ScrollService {
  170. s.preference = preference
  171. return s
  172. }
  173. // IgnoreUnavailable indicates whether the specified concrete indices
  174. // should be ignored when unavailable (missing or closed).
  175. func (s *ScrollService) IgnoreUnavailable(ignoreUnavailable bool) *ScrollService {
  176. s.ignoreUnavailable = &ignoreUnavailable
  177. return s
  178. }
  179. // AllowNoIndices indicates whether to ignore if a wildcard indices
  180. // expression resolves into no concrete indices. (This includes `_all` string
  181. // or when no indices have been specified).
  182. func (s *ScrollService) AllowNoIndices(allowNoIndices bool) *ScrollService {
  183. s.allowNoIndices = &allowNoIndices
  184. return s
  185. }
  186. // ExpandWildcards indicates whether to expand wildcard expression to
  187. // concrete indices that are open, closed or both.
  188. func (s *ScrollService) ExpandWildcards(expandWildcards string) *ScrollService {
  189. s.expandWildcards = expandWildcards
  190. return s
  191. }
  192. // ScrollId specifies the identifier of a scroll in action.
  193. func (s *ScrollService) ScrollId(scrollId string) *ScrollService {
  194. s.mu.Lock()
  195. s.scrollId = scrollId
  196. s.mu.Unlock()
  197. return s
  198. }
  199. // Do returns the next search result. It will return io.EOF as error if there
  200. // are no more search results.
  201. func (s *ScrollService) Do(ctx context.Context) (*SearchResult, error) {
  202. s.mu.RLock()
  203. nextScrollId := s.scrollId
  204. s.mu.RUnlock()
  205. if len(nextScrollId) == 0 {
  206. return s.first(ctx)
  207. }
  208. return s.next(ctx)
  209. }
  210. // Clear cancels the current scroll operation. If you don't do this manually,
  211. // the scroll will be expired automatically by Elasticsearch. You can control
  212. // how long a scroll cursor is kept alive with the KeepAlive func.
  213. func (s *ScrollService) Clear(ctx context.Context) error {
  214. s.mu.RLock()
  215. scrollId := s.scrollId
  216. s.mu.RUnlock()
  217. if len(scrollId) == 0 {
  218. return nil
  219. }
  220. path := "/_search/scroll"
  221. params := url.Values{}
  222. body := struct {
  223. ScrollId []string `json:"scroll_id,omitempty"`
  224. }{
  225. ScrollId: []string{scrollId},
  226. }
  227. _, err := s.client.PerformRequest(ctx, "DELETE", path, params, body)
  228. if err != nil {
  229. return err
  230. }
  231. return nil
  232. }
  233. // -- First --
  234. // first takes the first page of search results.
  235. func (s *ScrollService) first(ctx context.Context) (*SearchResult, error) {
  236. // Get URL and parameters for request
  237. path, params, err := s.buildFirstURL()
  238. if err != nil {
  239. return nil, err
  240. }
  241. // Get HTTP request body
  242. body, err := s.bodyFirst()
  243. if err != nil {
  244. return nil, err
  245. }
  246. // Get HTTP response
  247. res, err := s.client.PerformRequest(ctx, "POST", path, params, body)
  248. if err != nil {
  249. return nil, err
  250. }
  251. // Return operation response
  252. ret := new(SearchResult)
  253. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  254. return nil, err
  255. }
  256. s.mu.Lock()
  257. s.scrollId = ret.ScrollId
  258. s.mu.Unlock()
  259. if ret.Hits == nil || len(ret.Hits.Hits) == 0 {
  260. return nil, io.EOF
  261. }
  262. return ret, nil
  263. }
  264. // buildFirstURL builds the URL for retrieving the first page.
  265. func (s *ScrollService) buildFirstURL() (string, url.Values, error) {
  266. // Build URL
  267. var err error
  268. var path string
  269. if len(s.indices) == 0 && len(s.types) == 0 {
  270. path = "/_search"
  271. } else if len(s.indices) > 0 && len(s.types) == 0 {
  272. path, err = uritemplates.Expand("/{index}/_search", map[string]string{
  273. "index": strings.Join(s.indices, ","),
  274. })
  275. } else if len(s.indices) == 0 && len(s.types) > 0 {
  276. path, err = uritemplates.Expand("/_all/{typ}/_search", map[string]string{
  277. "typ": strings.Join(s.types, ","),
  278. })
  279. } else {
  280. path, err = uritemplates.Expand("/{index}/{typ}/_search", map[string]string{
  281. "index": strings.Join(s.indices, ","),
  282. "typ": strings.Join(s.types, ","),
  283. })
  284. }
  285. if err != nil {
  286. return "", url.Values{}, err
  287. }
  288. // Add query string parameters
  289. params := url.Values{}
  290. if s.pretty {
  291. params.Set("pretty", "1")
  292. }
  293. if s.size != nil && *s.size > 0 {
  294. params.Set("size", fmt.Sprintf("%d", *s.size))
  295. }
  296. if len(s.keepAlive) > 0 {
  297. params.Set("scroll", s.keepAlive)
  298. }
  299. if len(s.routing) > 0 {
  300. params.Set("routing", s.routing)
  301. }
  302. if len(s.preference) > 0 {
  303. params.Set("preference", s.preference)
  304. }
  305. if s.allowNoIndices != nil {
  306. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  307. }
  308. if len(s.expandWildcards) > 0 {
  309. params.Set("expand_wildcards", s.expandWildcards)
  310. }
  311. if s.ignoreUnavailable != nil {
  312. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  313. }
  314. return path, params, nil
  315. }
  316. // bodyFirst returns the request to fetch the first batch of results.
  317. func (s *ScrollService) bodyFirst() (interface{}, error) {
  318. var err error
  319. var body interface{}
  320. if s.body != nil {
  321. body = s.body
  322. } else {
  323. // Use _doc sort by default if none is specified
  324. if !s.ss.hasSort() {
  325. // Use efficient sorting when no user-defined query/body is specified
  326. s.ss = s.ss.SortBy(SortByDoc{})
  327. }
  328. // Body from search source
  329. body, err = s.ss.Source()
  330. if err != nil {
  331. return nil, err
  332. }
  333. }
  334. return body, nil
  335. }
  336. // -- Next --
  337. func (s *ScrollService) next(ctx context.Context) (*SearchResult, error) {
  338. // Get URL for request
  339. path, params, err := s.buildNextURL()
  340. if err != nil {
  341. return nil, err
  342. }
  343. // Setup HTTP request body
  344. body, err := s.bodyNext()
  345. if err != nil {
  346. return nil, err
  347. }
  348. // Get HTTP response
  349. //兼容处理
  350. params.Set("scroll", "5m")
  351. res, err := s.client.PerformRequest(ctx, "POST", path, params, s.scrollId)
  352. _ = body
  353. //res, err := s.client.PerformRequest(ctx, "POST", path, params, body)
  354. if err != nil {
  355. return nil, err
  356. }
  357. // Return operation response
  358. ret := new(SearchResult)
  359. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  360. return nil, err
  361. }
  362. s.mu.Lock()
  363. s.scrollId = ret.ScrollId
  364. s.mu.Unlock()
  365. if ret.Hits == nil || len(ret.Hits.Hits) == 0 {
  366. return nil, io.EOF
  367. }
  368. return ret, nil
  369. }
  370. // buildNextURL builds the URL for the operation.
  371. func (s *ScrollService) buildNextURL() (string, url.Values, error) {
  372. path := "/_search/scroll"
  373. // Add query string parameters
  374. params := url.Values{}
  375. if s.pretty {
  376. params.Set("pretty", "1")
  377. }
  378. return path, params, nil
  379. }
  380. // body returns the request to fetch the next batch of results.
  381. func (s *ScrollService) bodyNext() (interface{}, error) {
  382. s.mu.RLock()
  383. body := struct {
  384. Scroll string `json:"scroll"`
  385. ScrollId string `json:"scroll_id,omitempty"`
  386. }{
  387. Scroll: s.keepAlive,
  388. ScrollId: s.scrollId,
  389. }
  390. s.mu.RUnlock()
  391. return body, nil
  392. }