Searchkick: Support for multi tenancy

Created on 28 Aug 2014  路  7Comments  路  Source: ankane/searchkick

Hi @ankane , I'm currently using Apartment gem for a multi tenancy app. Can searchkick support multi tenancy? By default, I can see that it is overwriting indexes whenever the reindexing runs in different tenants.

Most helpful comment

Support for multi-tenancy has improved since 2014 :) Check out https://github.com/ankane/searchkick#multi-tenancy

All 7 comments

No, unfortunately this isn't a priority for Searchkick at the moment.

Ah! In any case, thanks for the reply!

@tommi-lew and anyone who is struggling with searchkick and multi-tenancy apps: I found my way out overriding the self.searchkick_index method created by the searchkick method here.

The searchkick_index class method is used all along searchkick. I'm using the searchkick capabilities without any problems.

This way you can switch tenants and use the class method as you wish.

Example:

# models/product.rb
class Product < ActiveRecord::Base
  searchkick

  def self.searchkick_index
    index_name = [Apartment::Tenant.current, model_name.plural, Rails.env].join('_')
    Searchkick::Index.new(index_name)
  end
  ...
end

I found myself repeating this on many models, so I extracted this to a concern:

# models/concerns/schema_searcheable.rb
module SchemaSearcheable
  extend ActiveSupport::Concern

  included do |base|
    base.instance_eval do
      def searchkick_index
        index_name = [Apartment::Tenant.current, model_name.plural, Rails.env].join('_')
        Searchkick::Index.new(index_name)
      end
    end
  end
  ...
end

# models/product.rb
class Product < ActiveRecord::Base
  searchkick
  include SchemaSearcheable
  ...
end

Hope that helps :)

:+1: Thank you! I did something similar too - monkey patching search_index, but I find your solution more elegant.

Note: This is only targeting classes which uses searchkick.

[ClassA, ClassB, Class B].each do |klass|
  klass.class_eval do
    def self.searchkick_index
      index = [Apartment::Tenant.current, class_variable_get(:@@searchkick_index)].join('_')
      index = index.call if index.respond_to? :call
      Searchkick::Index.new(index)
    end
  end
end

I've was fiddling around with this issue again and I checked something: we should not monke patch the searchkick_index method. Using the index_name option and a concern will work just fine.

I've wrote a gist that shows how I did it without overriding the searchkick_index method.

https://gist.github.com/tiagoamaro/aa8f776b3217f2974f69

@tiagoamaro thank you. @ankane please think about this. This feature would make a great advance for big software solution.

Support for multi-tenancy has improved since 2014 :) Check out https://github.com/ankane/searchkick#multi-tenancy

Was this page helpful?
0 / 5 - 0 ratings

Related issues

netwire88 picture netwire88  路  3Comments

carolliu picture carolliu  路  3Comments

yurgon picture yurgon  路  4Comments

tomajask picture tomajask  路  4Comments

cubaraphael picture cubaraphael  路  3Comments