Class: RedisSearch
- Inherits:
-
Object
- Object
- RedisSearch
- Defined in:
- lib/redis_search.rb
Instance Attribute Summary collapse
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
-
#index(id, doc) ⇒ Object
Public: Index a document.
-
#initialize(opts = {}) ⇒ RedisSearch
constructor
Public: Instantiate a new RedisSearch instance.
-
#search(query) ⇒ Object
Public: Search for a document.
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
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
4 5 6 |
# File 'lib/redis_search.rb', line 4 def namespace @namespace end |
#redis ⇒ Object (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 |