Skip to main content
Check the algoliasearch-rails-example repository on GitHub for a sample Rails app with Autocomplete and InstantSearch results pages.

Compatibility

This gem supports:
  • Ruby versions 2.5 and later
  • Ruby on Rails versions 6 and 7.
This gem is compatible with:

Install

Install the gem directly:
gem install algoliasearch-rails
Or add it to your Gemfile:
gem "algoliasearch-rails"
And run:
bundle install

Configure

To configure the gem, create a new file config/initializers/algoliasearch.rb. Add your Algolia credentials, the application ID and the API key. You can find both in the Algolia dashboard.
Ruby
AlgoliaSearch.configuration = {
  application_id: "ALGOLIA_APPLICATION_ID",
  api_key: "ALGOLIA_API_KEY"
}

API client with custom settings

To customize settings such as timeout for the API client, you can make the Algolia gem use your instance of an API client. You can do this by setting the AlgoliaSearch.client option to your initialized client:
Ruby
# your options
opts = {}
AlgoliaSearch.client = Algolia::SearchClient.create("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY", opts)

The algoliasearch block

To index your model into Algolia, include the AlgoliaSearch module and the algoliasearch block. This block sets up your index and options. If you want to use the default options, declare an empty block.
Ruby
class Contact < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch do
    # Use all default configuration
  end
end

Example configuration

To see a full configuration example, see the repository for the HN Search app.

Method name and aliases

All methods injected by the AlgoliaSearch module have the prefix algolia_ and are aliased to short names if they aren’t already defined by Rails:
Ruby
# Both calls do the same
Contact.algolia_reindex!
Contact.reindex!

# Both calls to the same
Contact.algolia_search("jon doe")
Contact.search("jon doe")
I