Class: Lunar::Search
- Inherits:
-
Object
- Object
- Lunar::Search
- Defined in:
- lib/lunar/search.rb
Instance Attribute Summary collapse
-
#fuzzy_sets ⇒ Object
readonly
Returns the value of attribute fuzzy_sets.
-
#key_value_sets ⇒ Object
readonly
Returns the value of attribute key_value_sets.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#search_identifier ⇒ Object
readonly
Returns the value of attribute search_identifier.
-
#sets ⇒ Object
readonly
Returns the value of attribute sets.
Instance Method Summary collapse
-
#initialize(prefix, keywords) ⇒ Search
constructor
A new instance of Search.
- #results(&block) ⇒ Object
Constructor Details
#initialize(prefix, keywords) ⇒ Search
Returns a new instance of Search.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/lunar/search.rb', line 5 def initialize(prefix, keywords) if keywords.is_a?(Hash) if fuzzy_hash = keywords.delete(:fuzzy) @fuzzy_sets = fuzzy_hash.inject([]) { |a, (field, query)| a | FuzzySets.new(prefix, query, field) } @search_identifier = fuzzy_hash.hash else @key_value_sets = keywords.map { |field, val| Sets.new(prefix, val, field) }.flatten @search_identifier = keywords.hash end else @sets = Sets.new(prefix, keywords) @search_identifier = keywords.hash end @prefix = prefix # Default finder, uses Ohm style finding @finder = lambda { |id| prefix[id] } end |
Instance Attribute Details
#fuzzy_sets ⇒ Object (readonly)
Returns the value of attribute fuzzy_sets.
3 4 5 |
# File 'lib/lunar/search.rb', line 3 def fuzzy_sets @fuzzy_sets end |
#key_value_sets ⇒ Object (readonly)
Returns the value of attribute key_value_sets.
3 4 5 |
# File 'lib/lunar/search.rb', line 3 def key_value_sets @key_value_sets end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
3 4 5 |
# File 'lib/lunar/search.rb', line 3 def prefix @prefix end |
#search_identifier ⇒ Object (readonly)
Returns the value of attribute search_identifier.
3 4 5 |
# File 'lib/lunar/search.rb', line 3 def search_identifier @search_identifier end |
#sets ⇒ Object (readonly)
Returns the value of attribute sets.
3 4 5 |
# File 'lib/lunar/search.rb', line 3 def sets @sets end |
Instance Method Details
#results(&block) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/lunar/search.rb', line 29 def results(&block) block ||= @finder if sets if sets.empty? return [] else if not Lunar.redis.exists(dist_key) Lunar.redis.zunion dist_key, sets.size, *sets Lunar.redis.expire dist_key, Lunar.ttl end SortedResultSet.new(dist_key, &block) end elsif key_value_sets if key_value_sets.empty? return [] else Lunar.redis.zinter dist_key, key_value_sets.size, *key_value_sets SortedResultSet.new(dist_key, &block) end elsif fuzzy_sets if fuzzy_sets.empty? return [] else if not Lunar.redis.exists(dist_key) Lunar.redis.sunionstore dist_key, *fuzzy_sets Lunar.redis.expire dist_key, Lunar.ttl end UnsortedResultSet.new(dist_key, &block) end end end |