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

Add configuration-based index_name prefix or suffix #341

Open
daniilsunyaev opened this issue Mar 5, 2019 · 7 comments
Open

Add configuration-based index_name prefix or suffix #341

daniilsunyaev opened this issue Mar 5, 2019 · 7 comments

Comments

@daniilsunyaev
Copy link

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.

@Spone
Copy link
Contributor

Spone commented Mar 5, 2019

Hi,
here is how we do it for now, in our projects.

  1. 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).

  1. 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]
  1. 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.

@daniilsunyaev
Copy link
Author

daniilsunyaev commented Mar 6, 2019

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

@Spone
Copy link
Contributor

Spone commented Mar 6, 2019

I have to try it, but I guess that's a good way to make it DRY 👍

@julienbourdeau
Copy link
Contributor

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?

@Spone
Copy link
Contributor

Spone commented Mar 7, 2019

Yes @julienbourdeau that would be awesome!

@daniilsunyaev
Copy link
Author

@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 #85.

@richardonrails
Copy link

richardonrails commented Dec 31, 2019

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, ...

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

4 participants