Class: Whoosh::VectorStore::MemoryStore
- Inherits:
-
Object
- Object
- Whoosh::VectorStore::MemoryStore
- Defined in:
- lib/whoosh/vector_store/memory_store.rb
Instance Method Summary collapse
- #close ⇒ Object
-
#count(collection) ⇒ Object
Count vectors in a collection.
-
#delete(collection, id:) ⇒ Object
Delete a vector.
-
#drop(collection) ⇒ Object
Drop a collection.
-
#initialize ⇒ MemoryStore
constructor
A new instance of MemoryStore.
-
#insert(collection, id:, vector:, metadata: {}) ⇒ Object
Store a vector with metadata.
-
#search(collection, vector:, limit: 10) ⇒ Object
Search by cosine similarity, return top-k results.
Constructor Details
#initialize ⇒ MemoryStore
Returns a new instance of MemoryStore.
6 7 8 9 |
# File 'lib/whoosh/vector_store/memory_store.rb', line 6 def initialize @collections = {} @mutex = Mutex.new end |
Instance Method Details
#close ⇒ Object
55 56 57 |
# File 'lib/whoosh/vector_store/memory_store.rb', line 55 def close # No-op end |
#count(collection) ⇒ Object
Count vectors in a collection
42 43 44 45 46 |
# File 'lib/whoosh/vector_store/memory_store.rb', line 42 def count(collection) @mutex.synchronize do @collections[collection]&.size || 0 end end |
#delete(collection, id:) ⇒ Object
Delete a vector
35 36 37 38 39 |
# File 'lib/whoosh/vector_store/memory_store.rb', line 35 def delete(collection, id:) @mutex.synchronize do @collections[collection]&.delete(id) end end |
#drop(collection) ⇒ Object
Drop a collection
49 50 51 52 53 |
# File 'lib/whoosh/vector_store/memory_store.rb', line 49 def drop(collection) @mutex.synchronize do @collections.delete(collection) end end |
#insert(collection, id:, vector:, metadata: {}) ⇒ Object
Store a vector with metadata
12 13 14 15 16 17 |
# File 'lib/whoosh/vector_store/memory_store.rb', line 12 def insert(collection, id:, vector:, metadata: {}) @mutex.synchronize do @collections[collection] ||= {} @collections[collection][id] = { vector: vector, metadata: } end end |
#search(collection, vector:, limit: 10) ⇒ Object
Search by cosine similarity, return top-k results
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/whoosh/vector_store/memory_store.rb', line 20 def search(collection, vector:, limit: 10) @mutex.synchronize do items = @collections[collection] return [] unless items && !items.empty? scored = items.map do |id, data| score = cosine_similarity(vector, data[:vector]) { id: id, score: score, metadata: data[:metadata] } end scored.sort_by { |r| -r[:score] }.first(limit) end end |