flo is a redis powered node.js autocompleter inspired by soulmate. You can use this anywhere you want since this is just a module. If you look into the examples folder, I have provided an example on how to get it work with express.
If you want see a real world example of this, you should try out the search box at SeatGeek or Quora.
First, connect to the redis instance:
Sets up a new Redis Connection.
var flo = require('flo').connect();
options - Optional Hash of options.
redis
- An existing redis connection to use.host
- String Redis host. (Default: Redis' default)port
- Integer Redis port. (Default: Redis' default)password
- String Redis password.namespace
- String namespace prefix for Redis keys. (Default: flo).mincomplete
- Minimum completion of keys required for auto completion. (Default: 1)database
- Integer of the Redis database to select.
Returns a Connection instance.
These are the public functions:
add_term(type, id, term, score, data, callback)
:
type
- the type of data of this term (String)id
- unique identifier(within the specific type)term
- the phrase you wish to provide completions forscore
- user specified ranking metric (redis will order things lexicographically for items with the same score)data
- container for metadata that you would like to return when this item is matched (optional)callback
- callback to be run (optional)
Returns nothing.
search_term(types, phrase, limit, callback)
:
types
- types of term that you are looking for (Array of String)phrase
- the phrase or phrases you want to be autocompletedlimit
- the count of the number you want to return (optional, default: 5)callback(err, result)
- err is the error and results is the results
This call:
search_term(["chinese", "indian"], "rice", 1, cb);
will return a result in json format like:
{
term: "rice"
chinese: [
{
id: 3,
term: "mongolian fried rice",
score: 10,
data: {
name: "Gonghu Chinese Restaurant",
address: "304, University Avenue, Palo Alto"
}
}
],
indian: [
{
id: 1,
term: "Briyani Chicken Rice",
score: 5,
data: {
name: "Bombay Grill",
address: "100 Green St, Urbana"
}
}
]
}
remove_term(type, id, callback)
:
type
- the type of data of this term (String)id
- unique identifier(within the specific type)callback
- callback to be run (optional)
Returns nothing.
get_ids (type, term, callback)
:
type
- the type of data for this termterm
- the term to find the unique identifiers forcallback(err, result)
- result is an array of IDs for the term. Empty array if none were found
get_data(type, id, callback)
:
type
- the type of data for this termid
- unique identifier (within the specific type)callback(err, result)
- result is the data
For more information, you can read it here.
To run tests, first make sure your local redis is running, then:
./node_modules/expresso/bin/expresso test/*.test.js