Module: ElasticRecord::Relation::Batches
- Included in:
- ElasticRecord::Relation
- Defined in:
- lib/elastic_record/relation/batches.rb
Instance Method Summary collapse
- #find_each(options = {}) ⇒ Object
- #find_ids_in_batches(options = {}, &block) ⇒ Object
- #find_in_batches(options = {}) ⇒ Object
- #reindex ⇒ Object
Instance Method Details
#find_each(options = {}) ⇒ Object
4 5 6 7 8 |
# File 'lib/elastic_record/relation/batches.rb', line 4 def find_each( = {}) find_in_batches() do |records| records.each { |record| yield record } end end |
#find_ids_in_batches(options = {}, &block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/elastic_record/relation/batches.rb', line 16 def find_ids_in_batches( = {}, &block) .assert_valid_keys(:batch_size, :keep_alive) scroll_keep_alive = [:keep_alive] || ElasticRecord::Config.scroll_keep_alive size = [:batch_size] || 100 = { scroll: scroll_keep_alive, size: size, search_type: 'scan' }.update() search_result = klass.elastic_index.search(as_elastic, ) scroll_id = search_result['_scroll_id'] hit_count = 0 while (hit_ids = get_scroll_hit_ids(scroll_id, scroll_keep_alive)).any? hit_count += hit_ids.size hit_ids.each_slice(size, &block) end end |
#find_in_batches(options = {}) ⇒ Object
10 11 12 13 14 |
# File 'lib/elastic_record/relation/batches.rb', line 10 def find_in_batches( = {}) find_ids_in_batches() do |ids| yield klass.find(ids) end end |
#reindex ⇒ Object
38 39 40 41 42 |
# File 'lib/elastic_record/relation/batches.rb', line 38 def reindex relation.find_in_batches do |batch| elastic_index.bulk_add(batch) end end |