Commit f395d24287a4f2d0f0486d55fd413468ebb34877
1 parent
ca421600
Exists in
find_in_batches
Added find_in_batches and find_each
Showing
2 changed files
with
42 additions
and
0 deletions
Show diff stats
lib/searchkick/relation.rb
... | ... | @@ -406,6 +406,26 @@ module Searchkick |
406 | 406 | self |
407 | 407 | end |
408 | 408 | |
409 | + # TODO decide if makes sense to keep | |
410 | + def find_in_batches(batch_size: 1000) | |
411 | + page = 1 # see if page set? | |
412 | + loop do | |
413 | + result = page(page).per(batch_size).execute | |
414 | + yield result.results | |
415 | + page = result.next_page | |
416 | + break if result.last_page? | |
417 | + end | |
418 | + end | |
419 | + | |
420 | + # TODO decide if makes sense to keep | |
421 | + def find_each | |
422 | + find_in_batches do |batch| | |
423 | + batch.each do |record| | |
424 | + yield record | |
425 | + end | |
426 | + end | |
427 | + end | |
428 | + | |
409 | 429 | # same as Active Record |
410 | 430 | def inspect |
411 | 431 | entries = results.first(11).map!(&:inspect) | ... | ... |
test/pagination_test.rb
... | ... | @@ -69,6 +69,28 @@ class PaginationTest < Minitest::Test |
69 | 69 | assert products.any? |
70 | 70 | end |
71 | 71 | |
72 | + def test_find_in_batches | |
73 | + names = ["Product A", "Product B", "Product C", "Product D", "Product E", "Product F"] | |
74 | + store_names names | |
75 | + batches = [] | |
76 | + Product.search("product").order(name: :asc).find_in_batches(batch_size: 2) do |batch| | |
77 | + batches << batch.map(&:name) | |
78 | + end | |
79 | + expected = names.each_slice(2).to_a | |
80 | + assert_equal expected, batches | |
81 | + end | |
82 | + | |
83 | + def test_find_each | |
84 | + names = ["Product A", "Product B", "Product C", "Product D", "Product E", "Product F"] | |
85 | + store_names names | |
86 | + batches = [] | |
87 | + Product.search("product").order(name: :asc).find_each do |record| | |
88 | + batches << [record].map(&:name) | |
89 | + end | |
90 | + expected = names.each_slice(1).to_a | |
91 | + assert_equal expected, batches | |
92 | + end | |
93 | + | |
72 | 94 | def test_pagination_body |
73 | 95 | store_names ["Product A", "Product B", "Product C", "Product D", "Product E", "Product F"] |
74 | 96 | products = Product.search("product", body: {query: {match_all: {}}, sort: [{name: "asc"}]}, page: 2, per_page: 2, padding: 1) | ... | ... |