Redis::Autosuggest

Provides autocompletions through Redis, with the ability to rank results and integrate with Rails

Installation

$ gem install redis-autosuggest

Usage

By default Autosuggest creates a new Redis client on db 0 at localhost:6379.

To change the server/port:

r = Redis.new(:host => "my_host", :port => my_port)
Redis::Autosuggest.redis = r

To add items to be use for autocompletions:

Redis::Autosuggest.add("North By Northwest", "Northern Exposure")

To check for autocompletions for an item:

Redis::Autosuggest.suggest("nor")
# => ["north by northwest", "northern exposure"]

Autocompletions will be ordered their score value (descending).

Some other usage examples:

# Add items with initial scores 
Redis::Autosuggest.add_with_score("North By Northwest", 9, Northern Exposure, 3)
# Increment an item's score
Redis::Autosuggest.increment("North By Northwest", 1)

Fuzzy matching:

Redis::Autosuggest.fuzzy_match = true
Redis::Autosuggest.add("North By Northwest")
Redis::Autosuggest.suggest("nort byenorthwest")
# => ["north by northwest"]

Rails support

Autosuggest can also be integrated with Rails. Include it in a model:

class Movie < ActiveRecord::Base
  include Redis::Autosuggest

  attr_accessible :movie_title
  autosuggest     :movie_title
end

For first time usage, seed the Redis db with the autosuggest sources:

rake autosuggest:init

You can optionally specify a numeric field to be used as the initial score for an item when it is added and a limit of how many items maximum to keep:

  autosuggest :movie_title, :rank_by => imdb_rating, limit => 10000

Front-end portion

Jquery plugin for dropdown autocompletions for a from can be found here