Class: Leann::SearchResults
- Inherits:
-
Object
- Object
- Leann::SearchResults
- Includes:
- Enumerable
- Defined in:
- lib/leann/search_result.rb
Overview
Collection of search results with utility methods
Instance Attribute Summary collapse
-
#duration ⇒ Float
readonly
Search duration in seconds.
-
#query ⇒ String
readonly
Original query.
- #results ⇒ Array<SearchResult> readonly
Instance Method Summary collapse
-
#[](index) ⇒ SearchResult?
Get result by index.
-
#above(min_score) ⇒ SearchResults
Filter results by minimum score.
-
#combined_text(separator: "\n\n") ⇒ String
Join all texts.
-
#each(&block) ⇒ Object
Iterate over results.
-
#empty? ⇒ Boolean
Check if empty.
-
#first ⇒ SearchResult?
Get first result.
-
#initialize(results, query: nil, duration: nil) ⇒ SearchResults
constructor
A new instance of SearchResults.
-
#size ⇒ Integer
(also: #length, #count)
Number of results.
-
#texts ⇒ Array<String>
Get all texts.
-
#to_a ⇒ Array<Hash>
Convert to array of hashes.
-
#to_s ⇒ String
Pretty print results.
-
#top(n) ⇒ Array<SearchResult>
Get top n results.
Constructor Details
#initialize(results, query: nil, duration: nil) ⇒ SearchResults
Returns a new instance of SearchResults.
111 112 113 114 115 |
# File 'lib/leann/search_result.rb', line 111 def initialize(results, query: nil, duration: nil) @results = results @query = query @duration = duration end |
Instance Attribute Details
#duration ⇒ Float (readonly)
Returns Search duration in seconds.
106 107 108 |
# File 'lib/leann/search_result.rb', line 106 def duration @duration end |
#query ⇒ String (readonly)
Returns Original query.
103 104 105 |
# File 'lib/leann/search_result.rb', line 103 def query @query end |
#results ⇒ Array<SearchResult> (readonly)
100 101 102 |
# File 'lib/leann/search_result.rb', line 100 def results @results end |
Instance Method Details
#[](index) ⇒ SearchResult?
Get result by index
152 153 154 |
# File 'lib/leann/search_result.rb', line 152 def [](index) results[index] end |
#above(min_score) ⇒ SearchResults
Filter results by minimum score
159 160 161 162 |
# File 'lib/leann/search_result.rb', line 159 def above(min_score) filtered = results.select { |r| r.score >= min_score } SearchResults.new(filtered, query: query, duration: duration) end |
#combined_text(separator: "\n\n") ⇒ String
Join all texts
173 174 175 |
# File 'lib/leann/search_result.rb', line 173 def combined_text(separator: "\n\n") texts.join(separator) end |
#each(&block) ⇒ Object
Iterate over results
118 119 120 |
# File 'lib/leann/search_result.rb', line 118 def each(&block) results.each(&block) end |
#empty? ⇒ Boolean
Check if empty
132 133 134 |
# File 'lib/leann/search_result.rb', line 132 def empty? results.empty? end |
#first ⇒ SearchResult?
Get first result
138 139 140 |
# File 'lib/leann/search_result.rb', line 138 def first results.first end |
#size ⇒ Integer Also known as: length, count
Number of results
124 125 126 |
# File 'lib/leann/search_result.rb', line 124 def size results.size end |
#texts ⇒ Array<String>
Get all texts
166 167 168 |
# File 'lib/leann/search_result.rb', line 166 def texts results.map(&:text) end |
#to_a ⇒ Array<Hash>
Convert to array of hashes
191 192 193 |
# File 'lib/leann/search_result.rb', line 191 def to_a results.map(&:to_h) end |
#to_s ⇒ String
Pretty print results
179 180 181 182 183 184 185 186 187 |
# File 'lib/leann/search_result.rb', line 179 def to_s lines = ["Search results for: #{query.inspect}"] lines << "Found #{size} results in #{format("%.3f", duration || 0)}s" lines << "-" * 60 results.each_with_index do |r, i| lines << "#{i + 1}. #{r}" end lines.join("\n") end |
#top(n) ⇒ Array<SearchResult>
Get top n results
145 146 147 |
# File 'lib/leann/search_result.rb', line 145 def top(n) results.first(n) end |