🚀 Intelligent search made easy
Searchkick learns what your users are looking for. As more people search, it gets smarter and the results get better. It’s friendly for developers - and magical for your users.
Searchkick handles:
- stemming -
tomatoes
matchestomato
- special characters -
jalapeno
matchesjalapeño
- extra whitespace -
dishwasher
matchesdish washer
- misspellings -
zuchini
matcheszucchini
- custom synonyms -
qtip
matchescotton swab
Plus:
- query like SQL - no need to learn a new query language
- reindex without downtime
- easily personalize results for each user
- autocomplete
- “Did you mean” suggestions
- works with ActiveRecord and Mongoid
🍊 Battle-tested at Instacart
We highly recommend tracking queries and conversions
⚡ Searchjoy makes it easy
Install Elasticsearch. For Homebrew, use:
brew install elasticsearch
Add this line to your application’s Gemfile:
gem "searchkick"
Add searchkick to models you want to search.
class Product < ActiveRecord::Base
searchkick
end
Add data to the search index.
Product.reindex
And to query, use:
products = Product.search "2% Milk"
products.each do |product|
puts product.name
end
Query like SQL
Product.search "2% Milk", where: {in_stock: true}, limit: 10, offset: 50
Note: If you prefer the Elasticsearch DSL, see the Advanced section
Search specific fields
fields: [:name, :brand]
Where
where: {
expires_at: {gt: Time.now}, # lt, gte, lte also available
orders_count: 1..10, # equivalent to {gte: 1, lte: 10}
aisle_id: [25, 30], # in
store_id: {not: 2}, # not
aisle_id: {not: [25, 30]}, # not in
user_ids: {all: [1, 3]}, # all elements in array
or: [
[{in_stock: true}, {backordered: true}]
]
}
Order
order: {_score: :desc} # most relevant first - default
Limit / offset
limit: 20, offset: 40
Boost by a field
boost: "orders_count" # give popular documents a little boost
Use a *
for the query.
Product.search "*"
Plays nicely with kaminari and will_paginate.
# controller
@products = Product.search "milk", page: params[:page], per_page: 20
# view
<%= paginate @products %>
By default, results must match all words in the query.
Product.search "fresh honey" # fresh AND honey
To change this, use:
Product.search "fresh honey", operator: "or" # fresh OR honey
By default, results must match the entire word - back
will not match backpack
. You can change this behavior with:
class Product < ActiveRecord::Base
searchkick word_start: [:name]
end
And to search:
Product.search "back", fields: [{name: :word_start}]
Available options are:
:word # default
:word_start
:word_middle
:word_end
:text_start
:text_middle
:text_end
Searchkick defaults to English for stemming. To change this, use:
class Product < ActiveRecord::Base
searchkick language: "German"
end
class Product < ActiveRecord::Base
searchkick synonyms: [["scallion", "green onion"], ["qtip", "cotton swab"]]
end
Call Product.reindex
after changing synonyms.
By default, Searchkick handles misspelled queries by returning results with an edit distance of one. To turn off this feature, use:
Product.search "zuchini", misspellings: false # no zucchini
You can also change the edit distance with:
Product.search "zucini", misspellings: {distance: 2} # zucchini
Control what data is indexed with the search_data
method. Call Product.reindex
after changing this method.
class Product < ActiveRecord::Base
def search_data
as_json only: [:name, :active]
# or equivalently
{
name: name,
active: active
}
end
end
Searchkick uses find_in_batches
to import documents. To eager load associations, use the search_import
scope.
class Product < ActiveRecord::Base
scope :search_import, -> { includes(:searches) }
end
By default, all records are indexed. To control which records are indexed, use the should_index?
method.
class Product < ActiveRecord::Base
def should_index?
active # only index active records
end
end
- when you install or upgrade searchkick
- change the
search_data
method - change the
searchkick
method
- App starts
- Records are inserted, updated or deleted (syncs automatically)
Searchkick uses conversion data to learn what users are looking for. If a user searches for “ice cream” and adds Ben & Jerry’s Chunky Monkey to the cart (our conversion metric at Instacart), that item gets a little more weight for similar searches.
The first step is to define your conversion metric and start tracking conversions. The database works well for low volume, but feel free to use Redis or another datastore.
class Search < ActiveRecord::Base
belongs_to :product
# fields: id, query, searched_at, converted_at, product_id
end
You do not need to clean up the search queries. Searchkick automatically treats apple
and APPLES
the same.
Next, add conversions to the index. You must specify the conversions field as of version 0.2.0
.
class Product < ActiveRecord::Base
has_many :searches
searchkick conversions: "conversions" # name of field
def search_data
{
name: name,
conversions: searches.group("query").count
# {"ice cream" => 234, "chocolate" => 67, "cream" => 2}
}
end
end
Reindex and set up a cron job to add new conversions daily.
rake searchkick:reindex CLASS=Product
Order results differently for each user. For example, show a user’s previously purchased products before other results.
class Product < ActiveRecord::Base
searchkick personalize: "user_ids"
def search_data
{
name: name,
user_ids: orders.pluck(:user_id) # boost this product for these users
# [4, 8, 15, 16, 23, 42]
}
end
end
Reindex and search with:
Product.search "milk", user_id: 8
Autocomplete predicts what a user will type, making the search experience faster and easier.
First, specify which fields use this feature. This is necessary since autocomplete can increase the index size significantly, but don’t worry - this gives you blazing faster queries.
class City < ActiveRecord::Base
searchkick text_start: [:name]
end
Reindex and search with:
City.search "san fr", fields: [{name: :text_start}]
Typically, you want to use a Javascript library like typeahead.js or jQuery UI.
First, add a controller action.
# app/controllers/cities_controller.rb
class CitiesController < ApplicationController
def autocomplete
render json: City.search(params[:query], fields: [{name: :text_start}], limit: 10).map(&:name)
end
end
Then add the search box and Javascript code to a view.
<input type="text" id="query" name="query" />
<script src="jquery.js"></script>
<script src="typeahead.js"></script>
<script>
$("#query").typeahead({
name: "city",
remote: "/cities/autocomplete?query=%QUERY"
});
</script>
class Product < ActiveRecord::Base
searchkick suggest: ["name"] # fields to generate suggestions
end
Reindex and search with:
products = Product.search "peantu butta", suggest: true
products.suggestions # ["peanut butter"]
Facets provide aggregated search data.
products = Product.search "chuck taylor", facets: [:product_type, :gender, :brand]
p products.facets
Advanced
Product.search "2% Milk", facets: {store_id: {where: {in_stock: true}, limit: 10}}
Ranges
price_ranges = [{to: 20}, {from: 20, to: 50}, {from: 50}]
Product.search "*", facets: {price: {ranges: price_ranges}}
Highlight the search query in the results.
bands = Band.search "cinema", fields: [:name], highlight: true
Note: The fields
option is required.
View the highlighted fields with:
bands.with_details.each do |band, details|
puts details[:highlight][:name] # "Two Door <em>Cinema</em> Club"
end
To change the tag, use:
Band.search "cinema", fields: [:name], highlight: {tag: "<strong>"}
Find similar items.
product = Product.first
product.similar(fields: ["name"])
Note: Before 0.3.0
, locations were indexed incorrectly. When upgrading, be sure to reindex immediately.
class City < ActiveRecord::Base
searchkick locations: ["location"]
def search_data
attributes.merge location: [latitude, longitude]
end
end
Reindex and search with:
City.search "san", where: {location: {near: [37, -114], within: "100mi"}} # or 160km
Bounded by a box
City.search "san", where: {location: {top_left: [38, -123], bottom_right: [37, -122]}}
Searchkick supports single table inheritance.
class Dog < Animal
end
The parent and child model can both reindex.
Animal.reindex
Dog.reindex # equivalent
And to search, use:
Animal.search "*" # all animals
Dog.search "*" # just dogs
Animal.search "*", type: [Dog, Cat] # just cats and dogs
Note: The suggest
option retrieves suggestions from the parent at the moment.
Dog.search "airbudd", suggest: true # suggestions for all animals
Searchkick uses ENV["ELASTICSEARCH_URL"]
for the Elasticsearch server. This defaults to http://localhost:9200
.
Choose an add-on: SearchBox, Bonsai, or Found.
# SearchBox
heroku addons:add searchbox:starter
heroku config:add ELASTICSEARCH_URL=`heroku config:get SEARCHBOX_URL`
# Bonsai
heroku addons:add bonsai
heroku config:add ELASTICSEARCH_URL=`heroku config:get BONSAI_URL`
# Found
heroku addons:add foundelasticsearch
heroku config:add ELASTICSEARCH_URL=`heroku config:get FOUNDELASTICSEARCH_URL`
Then deploy and reindex:
heroku run rake searchkick:reindex CLASS=Product
Create an initializer config/initializers/elasticsearch.rb
with:
ENV["ELASTICSEARCH_URL"] = "http://username:password@api.searchbox.io"
Then deploy and reindex:
rake searchkick:reindex CLASS=Product
Prefer to use the Elasticsearch DSL but still want awesome features like zero-downtime reindexing?
Create a custom mapping:
class Product < ActiveRecord::Base
searchkick mappings: {
product: {
properties: {
name: {type: "string", analyzer: "keyword"}
}
}
}
end
And use the query
option to search:
products = Product.search query: {match: {name: "milk"}}
View the response with:
products.response
To keep the mappings and settings generated by Searchkick, use:
class Product < ActiveRecord::Base
searchkick merge_mappings: true, mappings: {...}
end
To modify the query generated by Searchkick, use:
query = Product.search "2% Milk", execute: false
query.body[:query] = {match_all: {}}
products = query.execute
Searchkick requires Elasticsearch 0.90.0
or higher.
Reindex one record
product = Product.find 10
product.reindex
Remove old indices
Product.clean_indices
Use a different index name
class Product < ActiveRecord::Base
searchkick index_name: "products_v2"
end
Prefix the index name
class Product < ActiveRecord::Base
searchkick index_prefix: "datakick"
end
Turn off callbacks permanently
class Product < ActiveRecord::Base
searchkick callbacks: false
end
or temporarily
Product.disable_search_callbacks # or use Searchkick.disable_callbacks for all models
ExpensiveProductsTask.execute
Product.enable_search_callbacks # or use Searchkick.enable_callbacks for all models
Product.reindex
Eager load associations
Product.search "milk", include: [:brand, :stores]
Do not load models
Product.search "milk", load: false
Turn off special characters
class Product < ActiveRecord::Base
# A will not match Ä
searchkick special_characters: false
end
Change import batch size
class Product < ActiveRecord::Base
searchkick batch_size: 200 # defaults to 1000
end
Asynchronous reindexing
class Product < ActiveRecord::Base
searchkick callbacks: false
# add the callbacks manually
# ActiveRecord - one callback
after_commit :reindex_async
# Mongoid - two callbacks
after_save :reindex_async
after_destroy :reindex_async
def reindex_async
# delayed job
delay.reindex
end
end
Reindex conditionally
Note: With ActiveRecord, use this feature with caution - transaction rollbacks can cause data inconstencies
class Product < ActiveRecord::Base
searchkick callbacks: false
# add the callbacks manually
after_save :reindex, if: proc{|model| model.name_changed? } # use your own condition
after_destroy :reindex
end
Reindex all models (Rails only)
rake searchkick:reindex:all
- Change
search
methods totire.search
and add index name in existing search calls
Product.search "fruit"
should be replaced with
Product.tire.search "fruit", index: "products"
- Replace tire mapping w/ searchkick method
class Product < ActiveRecord::Base
searchkick
end
- Deploy and reindex
rake searchkick:reindex CLASS=Product # or Product.reindex in the console
- Once it finishes, replace search calls w/ searchkick calls
If running Searchkick 0.6.0
and Elasticsearch 0.90
, we recommend upgrading to Searchkick 0.6.1
to fix an issue that causes downtime when reindexing.
Due to the distributed nature of Elasticsearch, you can get incorrect results when the number of documents in the index is low. You can read more about it here. To fix this, do:
class Product < ActiveRecord::Base
searchkick settings: {number_of_shards: 1}
end
For convenience, this is set by default in the test environment.
Thanks to Karel Minarik for Elasticsearch Ruby and Tire, Jaroslav Kalistsuk for zero downtime reindexing, and Alex Leschenko for Elasticsearch autocomplete.
- Search multiple fields for different terms
- Search across models
- Search nested objects
- Add section on testing
- Much finer customization
- More transparency into generated queries (for advanced use)
View the changelog
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
To get started with development and testing:
- Clone the repo
- Install PostgreSQL and create a database called
searchkick_test
(psql -d postgres -c "create database searchkick_test"
) - Install Elasticsearch
bundle
rake test