Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

searchkick

> 后端框架
Open source

Intelligent search made easy

6.7K stars0 likes3 views
WebsiteGitHub

About

Intelligent search made easy

Searchkick

:rocket: 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 matches tomato
  • special characters - jalapeno matches jalapeño
  • extra whitespace - dishwasher matches dish washer
  • misspellings - zuchini matches zucchini
  • custom synonyms - pop matches soda

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
  • supports many languages
  • works with Active Record and Mongoid

Check out Searchjoy for analytics and Autosuggest for query suggestions

:tangerine: Battle-tested at Instacart

Contents

  • Getting Started
  • Querying
  • Indexing
  • Intelligent Search
  • Instant Search / Autocomplete
  • Aggregations
  • Testing
  • Deployment
  • Performance
  • Advanced Search
  • Reference
  • Contributing

Searchkick 6.0 was recently released! See how to upgrade

Getting Started

Install Elasticsearch or OpenSearch. For Homebrew, use:

bash
brew install opensearch
brew services start opensearch

Add these lines to your application’s Gemfile:

ruby
gem "searchkick"

gem "elasticsearch"   # select one
gem "opensearch-ruby" # select one

The latest version works with Elasticsearch 8 and 9 and OpenSearch 2 and 3. For Elasticsearch 7 and OpenSearch 1, use version 5.5.2 and this readme.

Add searchkick to models you want to search.

ruby
class Product < ApplicationRecord
  searchkick
end

Add data to the search index.

ruby
Product.reindex

And to query, use:

ruby
products = Product.search("apples")
products.each do |product|
  puts product.name
end

Searchkick supports the complete Elasticsearch Search API and OpenSearch Search API. As your search becomes more advanced, we recommend you use the search server DSL for maximum flexibility.

Querying

Query like SQL

ruby
Product.search("apples").where(in_stock: true).limit(10).offset(50)

Search specific fields

ruby
fields(:name, :brand)

Where

ruby
where(store_id: 1, expires_at: Time.now..)

These types of filters are supported

Order

ruby
order(_score: :desc) # most relevant first - default

All of these sort options are supported

Limit / offset

ruby
limit(20).offset(40)

Select

ruby
select(:name)

These source filtering options are supported

Results

Searches return a Searchkick::Relation object. This responds like an array to most methods.

ruby
results = Product.search("milk")
results.size
results.any?
results.each { |result| ... }

By default, ids are fetched from the search server and records are fetched from your database. To fetch everything from the search server, use:

ruby
Product.search("apples").load(false)

Get total results

ruby
results.total_count

Get the time the search took (in milliseconds)

ruby
results.took

Get the full response from the search server

ruby
results.response

Note: By default, Elasticsearch and OpenSearch limit paging to the first 10,000 results for performance. This applies to the total count as well.

Filtering

Equal

ruby
where(store_id: 1)

Not equal

ruby
where.not(store_id: 2)

Greater than (gt), less than (lt), greater than or equal (gte), less than or equal (lte)

ruby
where(expires_at: {gt: Time.now})

Range

ruby
where(orders_count: 1..10)

In

ruby
where(aisle_id: [25, 30])

Not in

ruby
where.not(aisle_id: [25, 30])

Contains all

ruby
where(user_ids: {all: [1, 3]})

Like

ruby
where(category: {like: "%frozen%"})

Case-insensitive like

ruby
where(category: {ilike: "%frozen%"})

Regular expression

ruby
where(category: /frozen .+/)

Prefix

ruby
where(category: {prefix: "frozen"})

Exists

ruby
where(store_id: {exists: true})

Combine filters with OR

ruby
where(_or: [{in_stock: true}, {backordered: true}])

Boosting

Boost important fields

ruby
fields("title^10", "description")

Boost by the value of a field (field must be numeric)

ruby
boost_by(:orders_count) # give popular documents a little boost
boost_by(orders_count: {factor: 10}) # default factor is 1

Boost matching documents

ruby
boost_where(user_id: 1)
boost_where(user_id: {value: 1, factor: 100}) # default factor is 1000
boost_where(user_id: [{value: 1, factor: 100}, {value: 2, factor: 200}])

Boost by recency

ruby
boost_by_recency(created_at: {scale: "7d", decay: 0.5})

You can also boost by:

  • Conversions
  • Distance

Get Everything

Use a * for the query.

ruby
Product.search("*")

Pagination

Plays nicely with kaminari and will_paginate.

ruby
# controller
@products = Product.search("milk").page(params[:page]).per_page(20)

View with kaminari

erb
<%= paginate @products %>

View with will_paginate

erb
<%= will_paginate @products %>

Partial Matches

By default, results must match all words in the query.

ruby
Product.search("fresh honey") # fresh AND honey

To change this, use:

ruby
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:

ruby
class Product < ApplicationRecord
  searchkick word_start: [:name]
end

And to search (after you reindex):

ruby
Product.search("back").fields(:name).match(:word_start)

Available options are:

Option Matches Example
:word entire word apple matches apple
:word_start start of word app matches apple
:word_middle any part of word ppl matches apple
:word_end end of word ple matches apple
:text_start start of text gre matches green apple, app does not match
:text_middle any part of text een app matches green apple
:text_end end of text ple matches green apple, een does not match

The default is :word. The most matches will happen with :word_middle.

To specify different matching for different fields, use:

ruby
Product.search(query).fields({name: :word_start}, {brand: :word_middle})

Exact Matches

To match a field exactly (case-sensitive), use:

ruby
Product.search(query).fields({name: :exact})

Phrase Matches

To only match the exact order, use:

ruby
Product.search("fresh honey").match(:phrase)

Stemming and Language

Searchkick stems words by default for better matching. apple and apples both stem to appl, so searches for either term will have the same matches.

Searchkick defaults to English for stemming. To change this, use:

ruby
class Product < ApplicationRecord
  searchkick language: "german"
end

See the list of languages. A few languages require plugins:

  • chinese - analysis-ik plugin
  • chinese2 - analysis-smartcn plugin
  • japanese - analysis-kuromoji plugin
  • korean - analysis-openkoreantext plugin
  • korean2 - analysis-nori plugin
  • polish - analysis-stempel plugin
  • ukrainian - analysis-ukrainian plugin
  • vietnamese - analysis-vietnamese plugin

You can also use a Hunspell dictionary for stemming.

ruby
class Product < ApplicationRecord
  searchkick stemmer: {type: "hunspell", locale: "en_US"}
end

Disable stemming with:

ruby
class Image < ApplicationRecord
  searchkick stem: false
end

Exclude certain words from stemming with:

ruby
class Image < ApplicationRecord
  searchkick stem_exclusion: ["apples"]
end

Or change how words are stemmed:

ruby
class Image < ApplicationRecord
  searchkick stemmer_override: ["apples => other"]
end

Synonyms

ruby
class Product < ApplicationRecord
  searchkick search_synonyms: [["pop", "soda"], ["burger", "hamburger"]]
end

Call Product.reindex after changing synonyms. Synonyms are applied at search time before stemming, and can be a single word or multiple words.

For directional synonyms, use:

ruby
search_synonyms: ["lightbulb => halogenlamp"]

Dynamic Synonyms

The above approach works well when your synonym list is static, but in practice, this is often not the case. When you analyze search conversions, you often want to add new synonyms without a full reindex. We recommend placing synonyms in a file on the search server (in the config directory). This allows you to reload synonyms without reindexing.

txt
pop, soda
burger, hamburger

Then use:

ruby
class Product < ApplicationRecord
  searchkick search_synonyms: "synonyms.txt"
end

And reload with:

ruby
Product.search_index.reload_synonyms

Misspellings

By default, Searchkick handles misspelled queries by returning results with an edit distance of one.

You can change this with:

ruby
Product.search("zucini").misspellings(edit_distance: 2) # zucchini

To prevent poor precision and improve performance for correctly spelled queries (which should be a majority for most applications), Searchkick can first perform a search without misspellings, and if there are too few results, perform another with them.

ruby
Product.search("zuchini").misspellings(below: 5)

If there are fewer than 5 results, a 2nd search is performed with misspellings enabled. The result of this query is returned.

Turn off misspellings with:

ruby
Product.search("zuchini").misspellings(false) # no zucchini

Specify which fields can include misspellings with:

ruby
Product.search("zucini").fields(:name, :color).misspellings(fields: [:name])

When doing this, you must also specify fields to search

Bad Matches

If a user searches butter, they may also get results for peanut butter. To prevent this, use:

ruby
Product.search("butter").exclude("peanut butter")

You can map queries and terms to exclude with:

ruby
exclude_queries = {
  "butter" => ["peanut butter"],
  "cream" => ["ice cream", "whipped cream"]
}

Product.search(query).exclude(exclude_queries[query])

You can demote results by boosting by a factor

Issues· 9 open

View all issuesOpen on GitHub
  • #1748

    Support custom ID loading and native multi-tenancy

    feature requestUpdated Jul 28, 2026
  • #1742

    Track Reindex Progress with Rails.cache

    feature requestUpdated Apr 21, 2026
  • #1740

    Support deferring resolving credentials until needed

    feature requestUpdated Mar 17, 2026
  • #1732

    When adding a search_document_id, it is impossible to find a record in the database

    feature requestUpdated Dec 12, 2025
  • #1642

    Support double indexes

    feature requestUpdated Dec 1, 2023

> Tags

Rubyelasticsearchopensearchrails

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 23, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架