This gist shows how you could use apartment with searchkick, including a rake task that reindex all models/tenants.
This gist is used in this blog post: http://tiagoamaro.com.br/2014/12/11/multi-tenancy-with-searchkick/
This gist shows how you could use apartment with searchkick, including a rake task that reindex all models/tenants.
This gist is used in this blog post: http://tiagoamaro.com.br/2014/12/11/multi-tenancy-with-searchkick/
class Post | |
searchkick index_name: -> { [Apartment::Tenant.current, model_name.plural, Rails.env].join('_') } | |
end |
class Post | |
include SchemaSearchable | |
searchkick index_name: tenant_index_name | |
end |
module SchemaSearchable | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def tenant_index_name | |
-> { [Apartment::Tenant.current, model_name.plural, Rails.env].join('_') } | |
end | |
end | |
end |
namespace :searchkick do | |
desc 'Reindex all models on all tenants' | |
task reindex_tenants: :environment do | |
Rails.application.eager_load! | |
# You'll need to tell the rake task which tenant_names you are going to use | |
User.pluck(:database).each do |schema| | |
Apartment::Tenant.switch schema | |
(Searchkick::Reindex.instance_variable_get(:@descendents) || []).each do |model| | |
puts "Reindexing #{model.name} on #{schema}" | |
Apartment::Tenant.switch schema | |
model.reindex | |
end | |
end | |
end | |
end |