Module: ElasticRecord::Relation::Batches

Included in:
ElasticRecord::Relation
Defined in:
lib/elastic_record/relation/batches.rb

Instance Method Summary collapse

Instance Method Details

#find_each(options = {}) ⇒ Object



4
5
6
7
8
# File 'lib/elastic_record/relation/batches.rb', line 4

def find_each(options = {})
  find_in_batches(options) 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(options = {}, &block)
  options.assert_valid_keys(:batch_size, :keep_alive)

  scroll_keep_alive = options[:keep_alive] || ElasticRecord::Config.scroll_keep_alive
  size = options[:batch_size] || 100

  options = {
    scroll: scroll_keep_alive,
    size: size,
    search_type: 'scan'
  }.update(options)

  search_result = klass.elastic_index.search(as_elastic, options)
  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(options = {})
  find_ids_in_batches(options) do |ids|
    yield klass.find(ids)
  end
end

#reindexObject



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