Searchkick is an alternative to the official Elasticsearch Rails client. It provides some out of the box support for features that the official client does not (autocomplete, spelling suggestions, stemming, etc).
This documentation covers the basics of using the Searchkick client for Elasticsearch and is not meant to be exhaustive. For complete documentation, please see the project’s GitHub page.
Users who are using the official Elasticsearch Rails client should read the documentation here.
Searchkick looks for an environment variable called
ELASTICSEARCH_URL to determine how to connect to your Elasticsearch cluster. If this variable is not found, Searchkick will default to
http://localhost:9200. The first step is to make sure that this environment variable is present so Searchkick can communicate with Elasticsearch.
Heroku users: When Bonsai is added to your application, it automatically creates an environment variable called
BONSAI_URL and populates it with your cluster URL. You can initialize the
ELASTICSEARCH_URL variable by running the following in a terminal:
heroku config:set ELASTICSEARCH_URL=`heroku config:get BONSAI_URL`
Direct users Users who sign up for Bonsai through our website will need to create the environment variable manually. For Linux/BSD/OSX, this will probably look something like the following:
export ELASTICSEARCH_URL="<URL copy/pasted from your Bonsai dashboard>"
Setting up Rails
Add the searchkick gem to your Gemfile:
Make sure to run
bundle install after modifying your Gemfile. Next, add Searchkick to the models you want to search, like so:
class Product < ActiveRecord::Base searchkick end
You should now be able to create an index on your cluster and populate it with your data by running
Product.reindex within a Rails console. Once your data is indexed, you can search it from a console as well:
products = Product.search "apples" products.each do |product| puts product.name end
Once you have confirmed that your application is able to communicate with your Bonsai cluster, you can begin the work of configuring your other models and search options. Please consult the Searchkick documentation for more details: https://github.com/ankane/searchkick
If you have any issues, please don’t hesitate to open a support ticket by emailing email@example.com.