Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple indexes? #138

Closed
9mm opened this issue Dec 1, 2016 · 7 comments
Closed

Multiple indexes? #138

9mm opened this issue Dec 1, 2016 · 7 comments

Comments

@9mm
Copy link

9mm commented Dec 1, 2016

I have a SAAS app where each client/customer needs their own index (so basically multiple indexes of the same model).

Imagine each paying client has their own index of many Book records. This model belongs_to :company so that each company gets their own database of books.

Basically I want to create 1 index per company using this Rails gem on the Book model.

How might I go about doing this? I see a few issues that might be tricky:

  • Creating the appropriate index name (easiest would be to prefix the book index with the company_id), so... 1_Book, 2_Book, etc.
  • Updating the appropriate index when a record is saved

Associated attributes are already taken care of properly simply because it pulls the association properly.

Searching the right index is easy, based on just the UI.

Thanks!

@redox
Copy link
Contributor

redox commented Dec 1, 2016

Hi @9mm,

having multiple indices is definitely an option to handle such a multi-tenant SAAS app but it actually slows down the indexing time (because we need to build N indices). Also, it's indeed not something supported by our Rails integration for now. There is actually a better approach: keeping both indexing time low & security.

Instead of having 1 index per company, I would recommend you going for 1 single index for ALL companies and tag all records with their company_id. At search-time, you would then use our Secured API key to restrict the search to only what a company has access to.

So a record will look like:

{
  "objectID": "42",
  "name": "My book",
  [...]
  "_tags": ["company_1"]
}

And at query time, you will generate an API key that is only able to search for records tagged with such company ID. The resulting API key can then be used in your JS frontend code.

public_key = Algolia.generate_secured_api_key('SearchOnlyAPIKeyKeptPrivate', {filters: 'company_1'})

What do you think about that?

@9mm
Copy link
Author

9mm commented Dec 1, 2016

Ah interesting, that also works quite nice then.

2 fast questions, if you wouldnt mind!

  1. What is the main difference between adding a _tag vs making it a property on the object like company_id? Can a filter only apply to a tag but not a regular property? Are there any other differences?

  2. Whats the Algolia-preferred way to update associations? (See example below). I've added in _tags too per your suggestion.

  algoliasearch per_environment: true do
    attribute :year, :price
    attribute(:author){ author.full_name }

    tags do
      ["company_#{company.id}"]
    end
  end

Even though I have no direct index for Author (I dont ever need to search them directly), how can I make sure that when I edit an Author, those specific Book records could be re-indexed?

Thanks so much for your help.

@redox
Copy link
Contributor

redox commented Dec 1, 2016

Cool :)

What is the main difference between adding a _tag vs making it a property on the object like company_id? Can a filter only apply to a tag but not a regular property? Are there any other differences?

Tag are designed for filtering only. It could be done using a regular attribute + facet filters, but it would we slightly less efficient.

Whats the Algolia-preferred way to update associations? (See example below). I've added in _tags too per your suggestion.

Yeah, that's something we need to work on because we don't have a strong answer. I think we should be able to reindex the parent object on touch like described in #74. In the meantime, I think the easiest is to trigger the Book indexing with an after_save callback on the Author.

If you want to work on #74, I would love to merge your contribution 👍

@9mm
Copy link
Author

9mm commented Dec 1, 2016

Ahh.. this makes sense. I'll keep that in mind, thanks again for your help!

@9mm 9mm closed this as completed Dec 1, 2016
@redox
Copy link
Contributor

redox commented Dec 1, 2016

Ahh.. this makes sense. I'll keep that in mind, thanks again for your help!

👍 You're welcome 💯

@9mm
Copy link
Author

9mm commented Dec 1, 2016 via email

@9mm
Copy link
Author

9mm commented Dec 1, 2016

Nevermind, this is possible just did some testing... sweet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants