algoliasearch-rails icon indicating copy to clipboard operation
algoliasearch-rails copied to clipboard

index_name based apartment current tenant

Open paulomcnally opened this issue 6 years ago • 5 comments

I am using https://github.com/influitive/apartment and I would expect index names to be generated based on the schema name as a prefix or suffix.

Example: tenant_model or model_tenant.

Apartment::Tenant.current

It always returns public even when its value is different.

paulomcnally avatar Nov 13 '18 21:11 paulomcnally

Hi, also using an Apartment like implementation of Algolia. Consider storing: tenant: Apartment::tenant.current on the index.

See #138 for the full explanation.

You can then generate a secure search only API key on the back end which filters searches to that tenant only. It cannot be used for other tenants data. See https://www.algolia.com/doc/guides/security/api-keys/?language=php#generating-secured-api-keys

I think this is the best way to do it, otherwise you will need to manage the API keys for your indices which will be N+1 for every tenant. I also think there is a limit to the total indices you are allowed on certain packages.

simonireilly avatar Nov 14 '18 08:11 simonireilly

I was thinking of one application for each tenant.

captura de pantalla 2018-11-15 a la s 8 30 56 p m

In this way you could relate the data from algolia to tenant and separate the information.

paulomcnally avatar Nov 16 '18 02:11 paulomcnally

There are 2 solutions:

  • one index per tenant and secured API key with restriction on the index name
  • one index with all tenant and secured api keys with restriction on the tenant filter (solution by @simonireilly

In both case, make sure you use the Secured API Keys, not "standard" API keys.

One tenant per App won't be possible because there are no API to create apps.

I never used Aparment, can you share how you define the Index Name? I think it must be as string and can't be a method call.

julienbourdeau avatar Mar 21 '19 09:03 julienbourdeau

@julienbourdeau Apartment is a middleware for adding subdomains e.g. www.awesomeshop.commerce-platform.com. Similar to devise current_user you get an accessor on the thread that tells you the current tenant (returns the subdomain).

Apartment.current_tenant # awesomeshop

This is basically what this would look like for apartment, as suggested in https://github.com/algolia/algoliasearch-rails/issues/78#issuecomment-299958972

Easily implemented as my fork shows https://github.com/simonireilly/algoliasearch-rails/pull/1/files.

#341 might fix this anyway?

The main issue is that you have the string being specific to the thread - so it needs to be a Proc to be dynamic :+1:

Per-tenant indices

You can affix the index name with the current tenant from your application using:

class Contact < ActiveRecord::Base
 include AlgoliaSearch
  algoliasearch per_tenant: Proc.new { Apartment.current_tenant } do
 # index name will be "awesomeshop_Contact"
   attribute :first_name, :last_name, :email
 end
end

simonireilly avatar Mar 21 '19 16:03 simonireilly

This can probably be handled at the same time as #341

Spone avatar Mar 21 '19 18:03 Spone