Class: ActsAsFerret::BulkIndexer

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_ferret/bulk_indexer.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ BulkIndexer

Returns a new instance of BulkIndexer.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/acts_as_ferret/bulk_indexer.rb', line 3

def initialize(args = {})
  @batch_size = args[:batch_size] || 1000
  @logger = args[:logger]
  @model = args[:model]
  @work_done = 0
  @indexed_records = 0
  @total_time = 0.0
  @index = args[:index]
  if args[:reindex]
    @reindex = true
    @model_count  = @model.count.to_f
  else
    @model_count = args[:total]
  end
end

Instance Method Details

#index_records(records, offset) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/acts_as_ferret/bulk_indexer.rb', line 19

def index_records(records, offset)
  batch_time = measure_time {
    docs = []
    records.each { |rec| docs << [rec.to_doc, rec.ferret_analyzer] if rec.ferret_enabled?(true) }
    @index.update_batch(docs)
  }.to_f
  rec_count = records.size
  @indexed_records += rec_count
  @total_time += batch_time
  @work_done = @indexed_records.to_f / @model_count * 100.0 if @model_count > 0
  @logger.debug "took #{batch_time} to index last #{rec_count} records. #{records_waiting} records to go. Avg time per record: #{avg_time_per_record}"
  remaining_time = avg_time_per_record * records_waiting
  @logger.info "#{@reindex ? 're' : 'bulk '}index model #{@model.name} : #{'%.2f' % @work_done}% complete : #{'%.2f' % remaining_time} secs to finish"
end

#measure_timeObject



34
35
36
37
38
# File 'lib/acts_as_ferret/bulk_indexer.rb', line 34

def measure_time
  t1 = Time.now
  yield
  Time.now - t1
end