Skip to main content

Index names

Index names are derived from the class names. To customize the index names, pass a string to the index_name option.
Ruby
class Contact < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch(index_name: "MyCustomName") do
    attribute :first_name, :last_name, :email
  end
end

Production and staging indices

To avoid changing your production index, you can append the current Rails environment to the index name with the per_environment option. This creates separate indices with names following the pattern MODEL_ENVIRONMENT.
Ruby
class Contact < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch(per_environment: true) do
    attribute :first_name, :last_name, :email
  end
end
To make it even harder to overwrite your production data, use different API keys in development and production. You can restrict access of your development API key to indices with names ending with _development. For more information, see API keys restrictions.

Basic relevance settings

To set a baseline for your index, define which attributes should be searchable and define a custom ranking.
Ruby
class Product < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch do
    # List of attributes used to build an Algolia record
    attributes :title, :subtitle, :description, :likes_count, :thumbnail_url, :release_date

    searchableAttributes ["title", "subtitle", "unordered(description)"]

    customRanking ["desc(likes_count)"]
  end

end
For more information, see:

Faceting and filtering

Add all attributes you want to use for filtering as facets.
Ruby
class Product < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch do
    # List of attributes used to build an Algolia record
    attributes :title, :subtitle, :likes_count, :ratings, :categories, :features, :sizes

    # ... Other settings removed for brevity

    attributesForFaceting ["searchable(categories)", "features", "sizes"]
    numericAttributesForFiltering ["likes_count", "equalOnly(ratings)"]
  end

end
For more information, see:

Synonyms

You can define regular synonyms where all words are considered equivalent.
Ruby
class Contact < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch do
    attributes :first_name, :email

    synonyms(
      [
        [
          "bob",
          "bobby" \
            "robert"
        ]
      ]
    )
  end
end
To define other types of synonyms, directly call the saveSynonyms method on the API client.

Sync your settings with Algolia

By default, your settings will be synced with Algolia.

Turn off automatic syncing

If you turn off automatic syncing, make sure to manually send updates to Algolia whenever you change your settings.
Ruby
class Musician < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch(check_settings: false) do
    searchableAttributes ["name", "band"]
  end
end

Apply all settings

To send settings updates for all indices, use the set_all_settings rake command. It sends updates to your primary indices, your replicas, and any indices you added with add_index. Consider adding this command to your deployment pipeline, especially if you turned off automatic settings updates.
rake algoliasearch:set_all_settings
I