From 0638332ef262ffbd2dece8774e850e6f608a467d Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Tue, 17 Mar 2020 21:33:36 -0700 Subject: [PATCH] Added scroll [skip ci] --- lib/searchkick/relation.rb | 15 ++++++++++++++- test/scroll_test.rb | 10 ++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/searchkick/relation.rb b/lib/searchkick/relation.rb index b95516c..95addb8 100644 --- a/lib/searchkick/relation.rb +++ b/lib/searchkick/relation.rb @@ -9,7 +9,7 @@ module Searchkick :took, :error, :model_name, :entry_name, :total_count, :total_entries, :current_page, :per_page, :limit_value, :total_pages, :num_pages, :offset_value, :previous_page, :prev_page, :next_page, :first_page?, :last_page?, - :out_of_range?, :hits, :response, :to_a, :first, :scroll, :highlights + :out_of_range?, :hits, :response, :to_a, :first, :highlights def initialize(klass, term = "*", **options) unknown_keywords = options.keys - [:aggs, :block, :body, :body_options, :boost, @@ -254,6 +254,19 @@ module Searchkick self end + def scroll(value, &block) + spawn.scroll!(value, &block) + end + + def scroll!(value, &block) + options[:scroll] = value + if block + execute.scroll(&block) + else + self + end + end + # same as Active Record def inspect entries = results.first(11).map!(&:inspect) diff --git a/test/scroll_test.rb b/test/scroll_test.rb index 4d43091..e27879d 100644 --- a/test/scroll_test.rb +++ b/test/scroll_test.rb @@ -71,4 +71,14 @@ class ScrollTest < Minitest::Test end assert_equal 3, batches_count end + + def test_scroll_block_relation + store_names ["Product A", "Product B", "Product C", "Product D", "Product E", "Product F"] + batches_count = 0 + Product.search("*", relation: true).per_page(2).scroll("1m") do |batch| + assert_equal 2, batch.size + batches_count += 1 + end + assert_equal 3, batches_count + end end -- libgit2 0.21.0