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

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