Class: Leann::Searcher
- Inherits:
-
Object
- Object
- Leann::Searcher
- Defined in:
- lib/leann/searcher.rb
Overview
Handles search operations on an index
Instance Attribute Summary collapse
- #index ⇒ Index readonly
Instance Method Summary collapse
-
#initialize(index) ⇒ Searcher
constructor
A new instance of Searcher.
-
#search(query, limit: 5, threshold: nil, filters: nil) ⇒ SearchResults
Search the index.
Constructor Details
#initialize(index) ⇒ Searcher
Returns a new instance of Searcher.
12 13 14 15 16 17 18 |
# File 'lib/leann/searcher.rb', line 12 def initialize(index) @index = index @backend = nil @embedding_provider = nil @offsets = nil @passages_cache = nil end |
Instance Attribute Details
Instance Method Details
#search(query, limit: 5, threshold: nil, filters: nil) ⇒ SearchResults
Search the index
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/leann/searcher.rb', line 36 def search(query, limit: 5, threshold: nil, filters: nil) start_time = Time.now # Compute query embedding = .compute([query]).first # Load all passages for on-the-fly embedding computation passages = load_all_passages # Search with on-the-fly embedding recomputation raw_results = backend.search( , embedding_provider: , passages: passages, limit: limit * 2 ) # Load passages and build results results = raw_results.map do |id, score| passage = load_passage(id) next unless passage SearchResult.new( id: id, text: passage[:text], score: score, metadata: passage[:metadata] || {} ) end.compact # Apply threshold filter results = results.select { |r| r.score >= threshold } if threshold # Apply metadata filters results = apply_filters(results, filters) if filters # Limit and sort results results = results.sort.first(limit) duration = Time.now - start_time SearchResults.new(results, query: query, duration: duration) end |