Class: Lunar::Index
- Inherits:
-
Object
- Object
- Lunar::Index
- Defined in:
- lib/lunar/index.rb
Overview
Handles the management of indices for a given namespace. Although you may use this directly, it’s much more convenient to use ‘Lunar::index` and `Lunar::delete`.
Constant Summary collapse
- FUZZY_MAX_LENGTH =
100
- MissingID =
Class.new(StandardError)
- FuzzyFieldTooLong =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#fuzzies ⇒ Object
readonly
Returns the value of attribute fuzzies.
-
#metaphones ⇒ Object
readonly
Returns the value of attribute metaphones.
-
#nest ⇒ Object
readonly
Returns the value of attribute nest.
-
#numbers ⇒ Object
readonly
Returns the value of attribute numbers.
-
#sortables ⇒ Object
readonly
Returns the value of attribute sortables.
Instance Method Summary collapse
-
#delete(existing_id) ⇒ nil
Deletes everything related to an existing document given its ‘id`.
- #fuzzy(att, value) ⇒ Object
-
#id(value = nil) ⇒ String
Get / Set the id of the document The ‘id` is used for all other keys to define the structure of the keys, therefore it’s imperative that you set it first.
-
#initialize(namespace) ⇒ Lunar::Index
constructor
This is actually wrapped by ‘Lunar.index` and is not inteded to be used directly.
-
#number(att, value) ⇒ Boolean
Adds a numeric index for ‘att` with `value`.
-
#sortable(att, value) ⇒ String
Adds a sortable index for ‘att` with `value`.
-
#text(att, value) ⇒ Array<String>
Indexes all the metaphone equivalents of the words in value except for words included in Stopwords.
Constructor Details
#initialize(namespace) ⇒ Lunar::Index
This is actually wrapped by ‘Lunar.index` and is not inteded to be used directly.
25 26 27 28 29 30 31 |
# File 'lib/lunar/index.rb', line 25 def initialize(namespace) @nest = Lunar.nest[namespace] @metaphones = @nest[:Metaphones] @numbers = @nest[:Numbers] @sortables = @nest[:Sortables] @fuzzies = @nest[:Fuzzies] end |
Instance Attribute Details
#fuzzies ⇒ Object (readonly)
Returns the value of attribute fuzzies.
18 19 20 |
# File 'lib/lunar/index.rb', line 18 def fuzzies @fuzzies end |
#metaphones ⇒ Object (readonly)
Returns the value of attribute metaphones.
15 16 17 |
# File 'lib/lunar/index.rb', line 15 def @metaphones end |
#nest ⇒ Object (readonly)
Returns the value of attribute nest.
14 15 16 |
# File 'lib/lunar/index.rb', line 14 def nest @nest end |
#numbers ⇒ Object (readonly)
Returns the value of attribute numbers.
16 17 18 |
# File 'lib/lunar/index.rb', line 16 def numbers @numbers end |
#sortables ⇒ Object (readonly)
Returns the value of attribute sortables.
17 18 19 |
# File 'lib/lunar/index.rb', line 17 def sortables @sortables end |
Instance Method Details
#delete(existing_id) ⇒ nil
Deletes everything related to an existing document given its ‘id`.
165 166 167 168 169 170 171 |
# File 'lib/lunar/index.rb', line 165 def delete(existing_id) id(existing_id) delete_numbers delete_sortables delete_fuzzies end |
#fuzzy(att, value) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/lunar/index.rb', line 173 def fuzzy(att, value) if value.to_s.length > FUZZY_MAX_LENGTH raise FuzzyFieldTooLong, "#{att} has a value #{value} exceeding the max #{FUZZY_MAX_LENGTH}" end words = Words.new(value).uniq fuzzy_words_and_parts(words) do |word, parts| parts.each do |part, encoded| fuzzies[att][encoded].zadd(1, id) end fuzzies[id][att].sadd word end delete_fuzzies_for(att, fuzzies[id][att].smembers - words, words) end |
#id(value = nil) ⇒ String
Get / Set the id of the document The ‘id` is used for all other keys to define the structure of the keys, therefore it’s imperative that you set it first.
54 55 56 57 |
# File 'lib/lunar/index.rb', line 54 def id(value = nil) @id = value.to_s if value @id or raise MissingID, "In order to index a document, you need an `id`" end |
#number(att, value) ⇒ Boolean
Adds a numeric index for ‘att` with `value`.
120 121 122 |
# File 'lib/lunar/index.rb', line 120 def number(att, value) numbers[att].zadd(value, id) end |
#sortable(att, value) ⇒ String
Adds a sortable index for ‘att` with `value`.
157 158 159 |
# File 'lib/lunar/index.rb', line 157 def sortable(att, value) sortables[id][att].set(value) end |
#text(att, value) ⇒ Array<String>
Indexes all the metaphone equivalents of the words in value except for words included in Stopwords.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/lunar/index.rb', line 83 def text(att, value) old = [id][att].smembers new = [] Scoring.new(value).scores.each do |word, score| = Lunar.(word) nest[att][].zadd(score, id) [id][att].sadd() new << end (old - new).each do || nest[att][].zrem(id) [id][att].srem() end return new end |