Class: RedisSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ RedisSearch

Public: Instantiate a new RedisSearch instance.

opts - A Hash of options.

:redis     - A Redis instance.
:namespace - The String namespace for Redis keys.

Returns nothing.



13
14
15
16
# File 'lib/redis_search.rb', line 13

def initialize(opts = {})
  @redis = opts[:redis]
  @namespace = opts[:namespace]
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



4
5
6
# File 'lib/redis_search.rb', line 4

def namespace
  @namespace
end

#redisObject (readonly)

Returns the value of attribute redis.



4
5
6
# File 'lib/redis_search.rb', line 4

def redis
  @redis
end

Instance Method Details

#index(id, doc) ⇒ Object

Public: Index a document.

id - The ID of the document. doc - The String document to index.

Returns nothing.



24
25
26
27
28
29
30
# 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) ⇒ Object

Public: Search for a document.

query - The String query.

Returns an Array of matching document IDs.



37
38
39
40
41
42
43
44
45
# 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