Class: Leann::SearchResult
- Inherits:
-
Object
- Object
- Leann::SearchResult
- Defined in:
- lib/leann/search_result.rb
Overview
Represents a single search result
Instance Attribute Summary collapse
-
#id ⇒ String
readonly
Document ID.
-
#metadata ⇒ Hash
readonly
Document metadata.
-
#score ⇒ Float
readonly
Similarity score (higher is better).
-
#text ⇒ String
readonly
Document text.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compare by score (for sorting).
-
#==(other) ⇒ Boolean
(also: #eql?)
Check equality.
-
#hash ⇒ Integer
Hash code for use as hash key.
-
#initialize(id:, text:, score:, metadata: {}) ⇒ SearchResult
constructor
A new instance of SearchResult.
-
#inspect ⇒ String
Detailed inspection.
-
#to_h ⇒ Hash
Convert to hash.
-
#to_s ⇒ String
Human-readable string representation.
-
#truncated_text(max_length = 100, omission: "...") ⇒ String
Truncate text to a maximum length.
Constructor Details
#initialize(id:, text:, score:, metadata: {}) ⇒ SearchResult
Returns a new instance of SearchResult.
31 32 33 34 35 36 |
# File 'lib/leann/search_result.rb', line 31 def initialize(id:, text:, score:, metadata: {}) @id = id @text = text @score = score.to_f @metadata = .transform_keys(&:to_sym) end |
Instance Attribute Details
#id ⇒ String (readonly)
Returns Document ID.
16 17 18 |
# File 'lib/leann/search_result.rb', line 16 def id @id end |
#metadata ⇒ Hash (readonly)
Returns Document metadata.
25 26 27 |
# File 'lib/leann/search_result.rb', line 25 def @metadata end |
#score ⇒ Float (readonly)
Returns Similarity score (higher is better).
22 23 24 |
# File 'lib/leann/search_result.rb', line 22 def score @score end |
#text ⇒ String (readonly)
Returns Document text.
19 20 21 |
# File 'lib/leann/search_result.rb', line 19 def text @text end |
Instance Method Details
#<=>(other) ⇒ Integer
Compare by score (for sorting)
74 75 76 |
# File 'lib/leann/search_result.rb', line 74 def <=>(other) other.score <=> score # Descending order end |
#==(other) ⇒ Boolean Also known as: eql?
Check equality
81 82 83 84 85 |
# File 'lib/leann/search_result.rb', line 81 def ==(other) return false unless other.is_a?(SearchResult) id == other.id && text == other.text && score == other.score end |
#hash ⇒ Integer
Hash code for use as hash key
90 91 92 |
# File 'lib/leann/search_result.rb', line 90 def hash [id, text, score].hash end |
#inspect ⇒ String
Detailed inspection
56 57 58 |
# File 'lib/leann/search_result.rb', line 56 def inspect "#<Leann::SearchResult id=#{id.inspect} score=#{format("%.4f", score)} text=#{truncated_text(50).inspect}>" end |
#to_h ⇒ Hash
Convert to hash
62 63 64 65 66 67 68 69 |
# File 'lib/leann/search_result.rb', line 62 def to_h { id: id, text: text, score: score, metadata: } end |
#to_s ⇒ String
Human-readable string representation
50 51 52 |
# File 'lib/leann/search_result.rb', line 50 def to_s "[#{format("%.3f", score)}] #{truncated_text(80)}" end |
#truncated_text(max_length = 100, omission: "...") ⇒ String
Truncate text to a maximum length
42 43 44 45 46 |
# File 'lib/leann/search_result.rb', line 42 def truncated_text(max_length = 100, omission: "...") return text if text.length <= max_length text[0, max_length - omission.length] + omission end |