Created
January 7, 2018 07:40
-
-
Save aymorgan/67654013e275a37bc727bd66897ad84d to your computer and use it in GitHub Desktop.
Guidance for implementing Algolia.generate_secured_api_key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
$(document).ready(function() { | |
var hitTemplate = '<a href="reports/{{{slug}}}" class="hit-report-card">' + | |
'<div id="preview_image" class="hit-report-img" style="content:url(<%= @attachments.find(6).image_url(:preview) %>);">ATTACHMENT ID: {{attachment.id}}</div>' + | |
'<div class="hit-report-title dont-break-out">{{{_highlightResult.title.value}}}</div>' + | |
'<div class="hit-report-date">{{{date_in_words}}}</div>' + | |
'<div class="hit-report-user">{{user.first_name}} {{user.last_name}}</div>' + | |
'<div class="hit-report-type">A/B Test</div>' + | |
'</a>'; | |
var search = instantsearch({ | |
appId: '<%= ENV[ 'ALGOLIASEARCH_APPLICATION_ID'] %>', | |
apiKey: '<%= ENV['ALGOLIASEARCH_API_KEY_SEARCH'] %>', | |
indexName: '<%= Report %>', | |
urlSync: true | |
}); | |
search.addWidget( | |
instantsearch.widgets.searchBox({ | |
container: '#q', | |
placeholder: 'Search for reports', | |
autofocus: false, | |
poweredBy: true | |
}) | |
); | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DISCLAIMER: | |
Sorry if this is a little long winded but I just want to ensure I communicate it properly (also I'm still learning Ruby & Rails so sorry if some issues are Rails questions than Algolia specific). | |
MY APP: | |
My Rails app has the following models: | |
- Company (has_many :users, has_many :reports) | |
- User (belongs_to :company, has_many :reports) | |
- Report (belongs_to :company, belongs_to :user) | |
I have managed to successfull indexed my Report model along with some nested attributes as shown below in the report.rb file. | |
I read this: https://discourse.algolia.com/t/multi-tenant-algolia-index/147 and a few other articals that kind of gave me the impression | |
that the best way to multitenancy with Algolia was to add the company_id to the indexed Reports model and then user the generate_secured_api_key | |
to filter results from the backend so a user only see's their companies reports in the search results. | |
QUESTIONS: | |
- Where does public_key = Algolia.generate_secured_api_key 'YourSearchOnlyApiKey', {'filters'=> '_tags:user_42'} go? In my Report model? | |
- Do I need to add a migration to my Report model to include a public_key? | |
- To display Reports where and indexed Report.company_id = current_user.company.id what do I put in the 'filters' section? Or is it not a filter? | |
- Do I need to change anything in my index.html.erb view to display? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
# Algolia Integration - https://www.algolia.com/doc/api-client/rails/usage/ | |
include AlgoliaSearch | |
algoliasearch do | |
# all attributes will be sent | |
attribute :id, :slug, :title, :overview, :result, :company_id, :user_id, :created_at, :updated_at, :date_in_words | |
attribute :user do | |
# Adds a nested model for User but restricts to first_name + last_name + email | |
{ first_name: user.first_name, last_name: user.last_name, email: user.email } | |
end | |
attribute :attachment do | |
# Adds a nested model for Attachment | |
{ id: attachment_ids } | |
end | |
end | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment