algoliasearch-rails
algoliasearch-rails copied to clipboard
index_name based apartment current tenant
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.
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.
I was thinking of one application for each tenant.
In this way you could relate the data from algolia to tenant and separate the information.
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 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
This can probably be handled at the same time as #341