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 |
# `Searchkick.models` method is available on versions 0.8.6+ | |
namespace :searchkick do | |
desc 'Reindex all models on all tenants' | |
task reindex_tenants: :environment do | |
Rails.application.eager_load! | |
Apartment::Tenant.each do |schema| | |
Apartment::Tenant.switch!(schema) | |
Searchkick.models.each do |model| | |
puts "Reindexing #{model.name} on #{schema}" | |
model.reindex | |
end | |
end | |
end | |
end | |
# If you're using Searchkick 0.8.5 or older versions, you should use its `@descendants` instance variable to get the models. Example: | |
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 |
@Xosmond yes, Searchkick will continue to properly work. This does not affect Searchkick features at all, since it uses the "dynamic index name" feature it offers. (More on its reference docs: https://github.com/ankane/searchkick#reference).
Hello, thank you for this, one question, will this make searchkick work properly? So can search for all records on shared tables and only on it records on normal tables?