Ruby on Rails

The official meilisearch-rails gem indexes your Active Record models and searches them. Point it at your CelerSearch instance and it works as it would with any Meilisearch.

1. Install

Gemfile
RUBY
gem 'meilisearch-rails'
Shell
bundle install

2. Configure

Copy the Endpoint URL and API key from your instance's Connection details at app.celersearch.com, and expose them as environment variables (MEILISEARCH_HOST, MEILISEARCH_API_KEY) or Rails credentials.

config/initializers/meilisearch.rb
RUBY
Meilisearch::Rails.configuration = {
  meilisearch_url: ENV.fetch('MEILISEARCH_HOST'),
  meilisearch_api_key: ENV.fetch('MEILISEARCH_API_KEY')
}

Server-side only

The API key has full access to your instance. Keep it in your server's environment or credentials. Never render it into views or JavaScript.

3. Index a model

app/models/book.rb
RUBY
class Book < ApplicationRecord
  include Meilisearch::Rails

  meilisearch enqueue: true do
    attribute :title, :author, :genre
    filterable_attributes [:genre]
    sortable_attributes [:title]
  end
end

enqueue: true moves indexing into Active Job (on a meilisearch queue), so saving a record never waits on a network call. Drop it to index inline.

Import the records you already have:

RUBY
Book.reindex!

New, changed and deleted records are synced automatically after that.

RUBY
# Typo-tolerant search, returning Book records
Book.search('harry pottre')

# Filter, sort and page size
Book.search('harry', filter: ['genre = fantasy'], sort: ['title:asc'], hits_per_page: 10)

Plans and limits

Each indexed record is one document toward your plan's document limit. Searches are always unlimited. At the limit, new writes are refused until you delete documents or move to a larger plan. See Connect Any App.

Next steps