algolia / algoliasearch-rails

AlgoliaSearch integration to your favorite ORM
MIT License
409 stars 119 forks source link

index_name based apartment current tenant #320

Open paulomcnally opened 5 years ago

paulomcnally commented 5 years ago

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.

simonireilly commented 5 years ago

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.

paulomcnally commented 5 years ago

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.

julienbourdeau commented 5 years ago

There are 2 solutions:

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.

simonireilly commented 5 years ago

@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
Spone commented 5 years ago

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