3

I currently am trying to use searchkick within my multi-tenant Rails 4 app that uses Apartment and pgSQL schemas to handle the tenancy. However, I am unable to get searchkick to work. I am not exactly sure what I am doing wrong, but I am thinking that it might be that when I reindex, it only reindexes on one schema.

I am thinking that I need to cycle through each schema, but I am unsure how to do it in Apartment. They seem to do it automatically for migrations, but I am trying to get it to work with searchkick. I want to edit the below rake task from searchkick to do so:

require 'rake'

namespace :searchkick do 

    task :reindex => :environment do
    if ENV["CLASS"]
      klass = ENV["CLASS"].constantize rescue nil
      if klass
        klass.reindex
      else
        abort "Could not find class: #{ENV["CLASS"]}"
      end
    else
      abort "USAGE: rake searchkick:reindex CLASS=Product"
    end
  end

end

Please let me know if you have any suggestions

David Mckee
  • 1,100
  • 3
  • 19
  • 35
  • Hi @david-mckee, I'm trying to do the same as you, a rails multi-tenant app + searchkick. Also using apartment gem. I've a question: how do you compartmentalise the search indexes for the different tenants? The problem here is that we might have completely different objects in the the tenants that share the same ID. – tommi Aug 25 '14 at 09:34
  • @tommi it seems to do that automatically. I'm doing some more testing on that, but it doesn't seem to be a problem for me so far. Let me know if you are able to figure out if that can happen. – David Mckee Aug 25 '14 at 20:51
  • @tommi I'm doing some more checking and it seems as though it is a problem. The results will not show up, but they will appear in the pagination. I'm thinking that I can somehow change the search method I am using for searchkick or maybe figure out a way to ignore the irrelevant searches in my pagination – David Mckee Aug 25 '14 at 21:12
  • 1
    Thanks for your help! I received a reply from the creator of searchkick that is is not possible. https://github.com/ankane/searchkick/issues/268#issuecomment-53672954 Now I might look for some monkey patching alternatives. – tommi Aug 28 '14 at 04:59

1 Answers1

0

I was able to figure it out. I had to do the following and add in the Apartment task to here:

require 'rake'
require 'apartment/migrator'

namespace :searchkick do 
    task :reindex => :environment do
        tenants.each do |tenant|

            Apartment::Tenant.switch(tenant)
            p 'switched'
        if ENV["CLASS"]
          klass = ENV["CLASS"].constantize rescue nil
          if klass
            klass.reindex
          else
            abort "Could not find class: #{ENV["CLASS"]}"
          end
        else
          abort "USAGE: rake searchkick:reindex CLASS=Product"
        end
    end
    end

end
David Mckee
  • 1,100
  • 3
  • 19
  • 35