All Projects → ankane → Autosuggest

ankane / Autosuggest

Licence: mit
Autocomplete suggestions based on what your users search

Programming Languages

ruby
36898 projects - #4 most used programming language

Autosuggest

Generate autocomplete suggestions based on what your users search

🍊 Battle-tested at Instacart

Build Status

Installation

Add this line to your application’s Gemfile:

gem 'autosuggest'

Getting Started

Prepare your data

Start with a hash of queries and their popularity, like the number of users who have searched it.

top_queries = {
  "bananas" => 353,
  "apples"  => 213,
  "oranges" => 140
}

With Searchjoy, you can do:

top_queries = Searchjoy::Search.group(:normalized_query)
  .having("COUNT(DISTINCT user_id) >= 5").distinct.count(:user_id)

Then pass them to Autosuggest.

autosuggest = Autosuggest.new(top_queries)

Filter duplicates

Stemming is used to detect duplicates like apple and apples.

The most popular query is preferred by default. To override this, use:

autosuggest.prefer ["apples"]

To fix false positives, use:

autosuggest.not_duplicates [["straws", "straus"]]

Filter misspellings

We tried open-source libraries like Aspell and Hunspell but quickly realized we needed to build a corpus specific to our application.

There are two ways to build the corpus, which can be used together.

  1. Add words
autosuggest.parse_words Product.pluck(:name)

Use the min option to only add words that appear multiple times.

  1. Add concepts
autosuggest.add_concept "brand", Brand.pluck(:name)

Filter words

Profanity is blocked by default. Add custom words with:

autosuggest.block_words ["boom"]

Profit

Get suggestions with:

autosuggest.suggestions

Filter queries without results and you’re set. We also prefer to have someone manually approve them by hand.

Full Example

top_queries = Searchjoy::Search.group(:normalized_query)
  .having("COUNT(DISTINCT user_id) >= 5").distinct.count(:user_id)
product_names = Product.pluck(:name)
brand_names = Brand.pluck(:name)

autosuggest = Autosuggest.new(top_queries)
autosuggest.parse_words product_names
autosuggest.add_concept "brand", brand_names
autosuggest.prefer brand_names
autosuggest.not_duplicates [["straws", "straus"]]
autosuggest.block_words ["boom"]

puts autosuggest.pretty_suggestions
# or
suggestions = autosuggest.suggestions

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/ankane/autosuggest.git
cd autosuggest
bundle install
bundle exec rake test
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].