Module: ElasticRecord::Batches

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

Instance Method Summary collapse

Instance Method Details

#find_eachObject



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

def find_each
  find_in_batches do |records|
    records.each { |record| yield record }
  end
end

#find_in_batches(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/elastic_record/relation/batches.rb', line 9

def find_in_batches(options = {})
  options[:size] ||= 100
  options[:scroll]  ||= '20m'
  options[:search_type] = 'scan'

  hits = klass.elastic_connection.search(as_elastic, options)

  klass.elastic_connection.scroll(hits.scroll_id, scroll: options[:scroll], ids_only: true) do |hits|
    yield klass.find(hits.to_a)
  end
end

#reindexObject



21
22
23
24
25
# File 'lib/elastic_record/relation/batches.rb', line 21

def reindex
  relation.find_in_batches do |batch|
    elastic_index.bulk_add(batch)
  end
end