Warning
Chewy is built on top of the official Elasticsearch Ruby client, which is not supported on Bonsai after version 7.13. This is due to a change introduced in the 7.14 release of the gem. This change prevents the Ruby client from communicating with open-sourced versions of Elasticsearch 7.x, as well as any version of OpenSearch. The table below indicates compatibility:
Engine | Version | Highest Compatible Gem Version |
Elasticsearch | 5.x | 7.13 |
Elasticsearch | 6.x | 7.14+ ( sic) |
Elasticsearch | 7.x | 7.13 |
OpenSearch | 1.x | 7.13 |
If you are receiving a
Elasticsearch::UnsupportedProductError
, then you'll need to ensure you're using a supported version of the Elasticsearch Ruby client.
Note
As of January 2021, Chewy supports up to Elasticsearch 5.x. Users wanting to use Chewy will need to ensure they are not running anything later than 5.x. Support for later versions is planned.
Add Chewy to the Gemfile
gem 'chewy'
bundle install
to install it.
Write an Initializer
BONSAI_URL
.
# Substitute with your own Bonsai cluster URL: export BONSAI_URL="https://abcd123:efg456@my-cluster-123456.us-west-2.bonsaisearch.net:443"
config/initializers/chewy.rb
. With the environment variable in place, save the initializer with the following:
Chewy.settings = { host: ENV['BONSAI_URL'] }
Configure Your Models
app/chewy/users_index.rb
:
class UsersIndex < Chewy::Index define_type User end
class User < ApplicationRecord update_index('users#user') { self } end
Indexing Your Documents
bundle exec rake chewy:deploy
Next Steps
Chewy is a mature ODM with plenty of great features. Toptal (the maintainers of Chewy) have some great documentation exploring some of what Chewy can do: Elasticsearch for Ruby on Rails: A Tutorial to the Chewy Gem.