Class: FuzzyTools::Index
- Inherits:
-
Object
- Object
- FuzzyTools::Index
- Defined in:
- lib/fuzzy_tools/index.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#indexed_attribute ⇒ Object
readonly
Returns the value of attribute indexed_attribute.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #all(query) ⇒ Object
- #all_with_scores(query) ⇒ Object
- #find(query) ⇒ Object
-
#initialize(options = {}) ⇒ Index
constructor
A new instance of Index.
Constructor Details
#initialize(options = {}) ⇒ Index
Returns a new instance of Index.
8 9 10 11 12 |
# File 'lib/fuzzy_tools/index.rb', line 8 def initialize( = {}) @source = [:source] @indexed_attribute = [:attribute] || :to_s build_index end |
Instance Attribute Details
#indexed_attribute ⇒ Object (readonly)
Returns the value of attribute indexed_attribute.
6 7 8 |
# File 'lib/fuzzy_tools/index.rb', line 6 def indexed_attribute @indexed_attribute end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
6 7 8 |
# File 'lib/fuzzy_tools/index.rb', line 6 def source @source end |
Instance Method Details
#all(query) ⇒ Object
19 20 21 |
# File 'lib/fuzzy_tools/index.rb', line 19 def all(query) all_with_scores(query).map(&:first) end |
#all_with_scores(query) ⇒ Object
23 24 25 |
# File 'lib/fuzzy_tools/index.rb', line 23 def all_with_scores(query) unsorted_scored_results(query).sort_by { |doc, score| [-score, document_attribute(doc)] } end |
#find(query) ⇒ Object
14 15 16 17 |
# File 'lib/fuzzy_tools/index.rb', line 14 def find(query) result, score = unsorted_scored_results(query).max_by { |doc, score| [score, document_attribute(doc)] } result end |