class RedisSearch
Attributes
namespace[R]
redis[R]
Public Class Methods
new(opts = {})
click to toggle source
Public: Instantiate a new RedisSearch
instance.
opts - A Hash of options.
:redis - A Redis instance. :namespace - The String namespace for Redis keys.
Returns nothing.
# File lib/redis_search.rb, line 13 def initialize(opts = {}) @redis = opts[:redis] @namespace = opts[:namespace] end
Public Instance Methods
index(id, doc)
click to toggle source
Public: Index a document.
id - The ID of the document. doc - The String document to index.
Returns nothing.
# File lib/redis_search.rb, line 24 def index(id, doc) redis.pipelined do tokens(doc).uniq.each do |token| redis.lpush key(token), id end end end
search(query)
click to toggle source
Public: Search for a document.
query - The String query.
Returns an Array of matching document IDs.
# File lib/redis_search.rb, line 37 def search(query) redis.pipelined do tokens(query).uniq.each do |token| redis.lrange(key(token), -100, -1) end end.flatten.uniq.map do |res| res.force_encoding('ascii-8bit') end end
Private Instance Methods
key(token)
click to toggle source
Private: Namespace a Redis key.
token - The token to namespace.
Returns a String.
# File lib/redis_search.rb, line 53 def key(token) [namespace, token].compact.join(':') end
tokens(string)
click to toggle source
Private: Tokenize a document or query.
string - The String to tokenize.
Returns an Array of String tokens.
# File lib/redis_search.rb, line 62 def tokens(string) string.scan(/\w+/) end