2016-01-12 8 views

ответ

1

Создайте проблему поиска и специальный класс индексатора (я положил его в lib/indexer.rb), который работает с любой моделью ActiveRecord и находит по ID.

проблемы/searchable.rb

require 'elasticsearch/model' 

module searchable 
    extend activesupport::concern 

    included do 
    include elasticsearch::model 
    after_touch() {indexer.delay.update(self.class.name, id)} 

    after_commit lambda {indexer.delay.create(self.class.name, id)}, on: :create 
    after_commit lambda {indexer.delay.update(self.class.name, id)}, on: :update 
    after_destroy lambda {indexer.delay.delete(self.class.name, id)} 
    end 

end 

Библиотека/indexer.rb

class Indexer 
    def self.create(klass_name, id) 
    doc = klass_name.constantize.find(id) 
    doc.__elasticsearch__.index_document 
    end 

    def self.update(klass_name, id) 
    doc = klass_name.constantize.find(id) 
    doc.__elasticsearch__.update_document 
    end 

    def self.delete(klass_name, id) 
    # it's already deleted, so we have to delete it old school 
    doc = klass_name.constantize.new 
    doc.id = id 
    doc.__elasticsearch__.delete_document 
    end 

end