Hi Simon

So it's certainly possible to have Thinking Sphinx generate separate indices & 
sources for different databases. Given you're using TS v2, it'd look something 
like this:

  class Article < ActiveRecord::Base
    Tenant.all.each do |tenant|
      ActiveRecord::Base.establish_connection tenant.database_credentials
      define_index "#{tenant.name}_article" do
        # …
      end
    end

    ActiveRecord::Base.establish_connection default_credentials
  end

And then you could filter on specific indices. An alternative would be one 
index per model, but many sources:

  class Article < ActiveRecord::Base
    define_index do
      Tenant.all.each do |tenant|
        ActiveRecord::Base.establish_connection tenant.database_credentials
        define_source do  
          # … fields, attributes
          
          has "'#{tenant.id}'", :as => :tenant_id, :type => :integer
        end
      end
    end

    ActiveRecord::Base.establish_connection default_credentials
  end  

In this scenario, you'd want to ensure each Sphinx search filtered by the 
appropriate tenant_id.

Either way, Thinking Sphinx will translate Sphinx results into ActiveRecord 
objects, so anything not available in the current database context would be 
returned as nil.

Hope this provides some clarity.

Cheers

-- 
Pat

On 04/06/2013, at 6:11 PM, Simon Hürlimann wrote:

> Hi there
> 
> I'm currently in the process of implementing multi-tenancy to an open source 
> project of mine. I'm using apartment 
> (https://github.com/influitive/apartment) to separate tenants by using one 
> database per tenant. The project currently uses thinking sphinx for search.
> 
> While I have some ideas how to use thinking sphinx to handle multi-tenancy, 
> they all look hackish and I'm not sure they would work.
> 
> I'd like your input on how to handle the following:
> * One database per tenant
> * Search should be restricted to all data in a single database depending
> 
> The database selection is implemented using apartment and a subdomain 
> matcher. You can find current code in the tenancy branch of Bookyt 
> (https://github.com/huerlisi/bookyt/tree/tenancy). Installation docs 
> (INSTALL.textile might be a little out of date regarding tenancy, just ping 
> me).
> 
> Thanx and cheers
> Simon
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Thinking Sphinx" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/thinking-sphinx?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  


-- 
You received this message because you are subscribed to the Google Groups 
"Thinking Sphinx" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/thinking-sphinx?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to