algoliasearch-rails
algoliasearch-rails copied to clipboard
Add configuration-based index_name prefix or suffix
We have per_environment
option that allows us to add environment-based suffix to indices names. However, in a typical scenario, several developers work in the same 'development' environment, but have different local databases, and probably would want different indices. Or we may have several review apps, that have same 'staging' environment, but do not share the same database.
Probably it would be great to have something like global_index_name_prefix
configuration option, that could be defined in ENV
variable (per developer/per review app). Then use this option as a prefix in algolia_index_name
method.
Looks like django version have something like this.
If that sounds okay, I may actually create pr for this.
Hi, here is how we do it for now, in our projects.
- Add these 2
ENV
variables:
ALGOLIA_DISABLE_INDEXING=false
ALGOLIA_ENV=john_doe
Each developer puts their name in ALGOLIA_ENV
.
If they don't work on the search part of the app, they can set ALGOLIA_DISABLE_INDEXING
to true
so their data is not indexed (saves operations and makes seeds faster).
- In the algolia initializer, we have:
AlgoliaSearch.configuration = {
application_id: ENV['ALGOLIA_APP_ID'],
api_key: ENV['ALGOLIA_ADMIN_API_KEY']
disable_indexing: ENV['ALGOLIA_DISABLE_INDEXING'] == "true" || Rails.env.test?,
env: ENV.fetch('ALGOLIA_ENV', Rails.env)
}
puts "Algolia indexing is disabled" if AlgoliaSearch.configuration[:disable_indexing]
- Finally in the models:
class Page < ApplicationRecord
include AlgoliaSearch
# ...
unless AlgoliaSearch.configuration[:disable_indexing]
algoliasearch index_name: "Page_#{AlgoliaSearch.configuration[:env]}_#{I18n.default_locale}" do
attribute :title, :content
end
end
end
What I'd love is to be able to set a global "pattern" for index names (such as "#{model_name}_#{AlgoliaSearch.configuration[:env]}_#{I18n.default_locale}"
in our case), that can be overridden for any given model. I did not find a DRY way to do it for now.
Thank you for posting this, that's an interesting solution. Can't you do this instead?
module ApplicationAlgoliaSearch
def self.included(base)
base.include AlgoliaSearch
base.extend ClassMethods
end
module ClassMethods do
def application_algoliasearch(options = {}, &block)
return if AlgoliaSearch.configuration[:disable_indexing]
options[:index_name] = "#{self.name}_#{AlgoliaSearch.configuration[:env]}_#{I18n.default_locale}" if options[:index_name].blank?
algoliasearch(options, &block)
end
end
end
class Page < ApplicationRecord
include ApplicationAlgoliaSearch
application_algoliasearch do
attribute :title, :content
end
end
I have to try it, but I guess that's a good way to make it DRY :+1:
Thank you for sharing your solutions 🙏
So ideallly, from what I understand, we'd need to have a function which:
- takes a the
:index_name
as a parameter - returns the fully qualified index name (that will be used for Algolia requests)
- has a default implementation to follow the already existing option
per_environment
- can be overridden by the user (you)
- is not depending on the model, all models should use the same function
Is this correct?
Yes @julienbourdeau that would be awesome!
@julienbourdeau customizable index_name modification function may be great, but simply having additional initializer configuration options (suffix/prefix, per_environment) may work fine as well (at least for me). Probably related to https://github.com/algolia/algoliasearch-rails/issues/85.
The really simple change I'd like to see is for per_environment: true
to look first for an Algolia.configuration[:env]
option, which should default to ENV['ALGOLIA_ENV']
and then fall back to Rails.env
only if the former doesn't exist.
That would be the easiest way to provide more envs without having to create additional "real" Rails Envs. In my case I just want this for a quick temporary staging environment (like production, but with a different DB), but I could see this working for multiple developers (who use RAILS_ENV=development, but have separate databases).
In the meantime, I'm disable per_environment
and doing this:
algoliasearch index_name: "Article_#{ENV.fetch('ALGOLIA_ENV', Rails.env)}", per_environment: false, ...