Searchkick
Search made easy
Usage
Searches
Searchkick provides sensible search defaults out of the box. It handles:
- stemming -
tomatoes
matchestomato
- special characters -
jalapenos
matchesjalapeños
- extra whitespace -
dishwasher
matchesdish washer
- misspellings -
zuchini
matcheszucchini
Make Searches Better Over Time
Use analytics on search conversions to improve results
Zero Downtime Changes
Elasticsearch has a feature called aliases that allows you to change mappings with no downtime.
Book.tire.reindex
This creates a new index books_20130714181054
and points the books
alias to the new index when complete - an atomic operation :)
First time: If books is an existing index, it will be replaced by an alias.
Searchkick uses find_in_batches
to import documents. To filter documents or eagar load associations, use the tire_import
scope.
class Book < ActiveRecord::Base
scope :tire_import, where(active: true).includes(:author, :chapters)
end
There is also a rake task.
rake searchkick:reindex CLASS=Book
Thanks to Jaroslav Kalistsuk for the original source.
Clinton Gormley also has a good post on this.
Elasticsearch Gotchas
Mappings
When changing the mapping in a model, you must create a new index for the changes to take place. Elasticsearch does not support updates to the mapping. For zero downtime, use the reindex
method above which creates a new index and swaps it in once built. To see the current mapping, use:
curl http://localhost:9200/books/_mapping
Low Number of Documents
By default, Tire creates an index on 5 shards - even in development. With a low number of documents, you will get inconsistent relevance scores by default. You can read more about it here. To fix this, set the search type to dfs_query_and_fetch
. Alternatively, you can just use one shard settings: {number_of_shards: 1}
.
Installation
Add this line to your application's Gemfile:
gem "searchkick"
And then execute:
bundle
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request