Commit 0638332ef262ffbd2dece8774e850e6f608a467d

Authored by Andrew Kane
1 parent eaf977d1

Added scroll [skip ci]

Showing 2 changed files with 24 additions and 1 deletions   Show diff stats
lib/searchkick/relation.rb
... ... @@ -9,7 +9,7 @@ module Searchkick
9 9 :took, :error, :model_name, :entry_name, :total_count, :total_entries,
10 10 :current_page, :per_page, :limit_value, :total_pages, :num_pages,
11 11 :offset_value, :previous_page, :prev_page, :next_page, :first_page?, :last_page?,
12   - :out_of_range?, :hits, :response, :to_a, :first, :scroll, :highlights
  12 + :out_of_range?, :hits, :response, :to_a, :first, :highlights
13 13  
14 14 def initialize(klass, term = "*", **options)
15 15 unknown_keywords = options.keys - [:aggs, :block, :body, :body_options, :boost,
... ... @@ -254,6 +254,19 @@ module Searchkick
254 254 self
255 255 end
256 256  
  257 + def scroll(value, &block)
  258 + spawn.scroll!(value, &block)
  259 + end
  260 +
  261 + def scroll!(value, &block)
  262 + options[:scroll] = value
  263 + if block
  264 + execute.scroll(&block)
  265 + else
  266 + self
  267 + end
  268 + end
  269 +
257 270 # same as Active Record
258 271 def inspect
259 272 entries = results.first(11).map!(&:inspect)
... ...
test/scroll_test.rb
... ... @@ -71,4 +71,14 @@ class ScrollTest < Minitest::Test
71 71 end
72 72 assert_equal 3, batches_count
73 73 end
  74 +
  75 + def test_scroll_block_relation
  76 + store_names ["Product A", "Product B", "Product C", "Product D", "Product E", "Product F"]
  77 + batches_count = 0
  78 + Product.search("*", relation: true).per_page(2).scroll("1m") do |batch|
  79 + assert_equal 2, batch.size
  80 + batches_count += 1
  81 + end
  82 + assert_equal 3, batches_count
  83 + end
74 84 end
... ...