0

I'm not sure if I'm going in the right direction with the algolia-rails gem (and Algolia in general):

Currently I have these two models:

class Contact < ActiveRecord::Base
  include AlgoliaSearch

  belongs_to :account

  algoliasearch do
    attribute :account_id, :full_name, :email, :tax_id, :contact_name

    searchableAttributes ['full_name', 'email', 'tax_id', 'contact_name']

    customRanking ['asc(full_name)']
  end
end

class Account < ActiveRecord::Base
  has_many :contacts
end

I would like to retrieve only matching records for a given account_id on the contacts indexes so the resulting query won't look for contact IDs that don't belong to an specific account. So current_account.contacts.algolia_search("Tom") should not fetch IDs from other accounts contacts.

Looks like it has something to do with facets but I'm not sure.

John
  • 588
  • 1
  • 6
  • 22

1 Answers1

2

Yep, It had a lot to do with facets. I modified the Contact model to add this inside the algoliasearch block:

attributesForFaceting ['account_id']

Then to filter contacts matching "Tom" by an specific account_id (let's suppose 3) it's as easy as:

Contact.algolia_search("Tom", {facets: 'account_id', facetFilters: "account_id:3"})

John
  • 588
  • 1
  • 6
  • 22