Class: Indexer

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
lib/rails/templates/indexer.rb

Overview

Indexer class for <sidekiq.org>

Run me with:

$ bundle exec sidekiq --queue elasticsearch --verbose

Constant Summary collapse

Logger =
Sidekiq.logger.level == Logger::DEBUG ? Sidekiq.logger : nil
Client =
Elasticsearch::Client.new host: (ENV['ELASTICSEARCH_URL'] || 'http://localhost:9200'), logger: Logger

Instance Method Summary collapse

Instance Method Details

#perform(operation, klass, record_id, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rails/templates/indexer.rb', line 31

def perform(operation, klass, record_id, options={})
  logger.debug [operation, "#{klass}##{record_id} #{options.inspect}"]

  case operation.to_s
    when /index|update/
      record = klass.constantize.find(record_id)
      record.__elasticsearch__.client = Client
      record.__elasticsearch__.__send__ "#{operation}_document"
    when /delete/
      Client.delete index: klass.constantize.index_name, id: record_id
    else raise ArgumentError, "Unknown operation '#{operation}'"
  end
end