diff --git a/lib/searchkick/relation.rb b/lib/searchkick/relation.rb index b08cbf5..2377172 100644 --- a/lib/searchkick/relation.rb +++ b/lib/searchkick/relation.rb @@ -406,6 +406,26 @@ module Searchkick self end + # TODO decide if makes sense to keep + def find_in_batches(batch_size: 1000) + page = 1 # see if page set? + loop do + result = page(page).per(batch_size).execute + yield result.results + page = result.next_page + break if result.last_page? + end + end + + # TODO decide if makes sense to keep + def find_each + find_in_batches do |batch| + batch.each do |record| + yield record + end + end + end + # same as Active Record def inspect entries = results.first(11).map!(&:inspect) diff --git a/test/pagination_test.rb b/test/pagination_test.rb index f6be682..cba7f42 100644 --- a/test/pagination_test.rb +++ b/test/pagination_test.rb @@ -69,6 +69,28 @@ class PaginationTest < Minitest::Test assert products.any? end + def test_find_in_batches + names = ["Product A", "Product B", "Product C", "Product D", "Product E", "Product F"] + store_names names + batches = [] + Product.search("product").order(name: :asc).find_in_batches(batch_size: 2) do |batch| + batches << batch.map(&:name) + end + expected = names.each_slice(2).to_a + assert_equal expected, batches + end + + def test_find_each + names = ["Product A", "Product B", "Product C", "Product D", "Product E", "Product F"] + store_names names + batches = [] + Product.search("product").order(name: :asc).find_each do |record| + batches << [record].map(&:name) + end + expected = names.each_slice(1).to_a + assert_equal expected, batches + end + def test_pagination_body store_names ["Product A", "Product B", "Product C", "Product D", "Product E", "Product F"] products = Product.search("product", body: {query: {match_all: {}}, sort: [{name: "asc"}]}, page: 2, per_page: 2, padding: 1) -- libgit2 0.21.0