Skip to content

vectordb

stable

In-memory vector database with cosine and Euclidean similarity search. Provides a VectorDB class for storing and querying fixed-dimension float vectors with optional string metadata.

use plugin vectordb::{VectorDB, insert, search, …}
15 functions Database
/ filter jk navigate Esc clear
Functions (15)
  1. VectorDB Creates a new in-memory vector database for vectors of a fixed dimensionality.
  2. insert Insert or replace a vector by ID
  3. search Find top-k nearest vectors by cosine similarity
  4. search_euclidean Find top-k nearest vectors by Euclidean distance
  5. remove Remove a vector by ID
  6. count Get the number of stored vectors
  7. get Retrieve a vector entry by ID
  8. list_ids List all stored IDs
  9. clear Remove all vectors from the database
  10. dimension Get the vector dimension
  11. contains Check if an ID exists in the database
  12. batch_insert Insert multiple vectors at once
  13. update_metadata Update metadata for an existing entry
  14. cosine_similarity Compute cosine similarity between two vectors
  15. euclidean_distance Compute Euclidean distance between two vectors

Creates a new in-memory vector database for vectors of a fixed dimensionality.

Creates a new in-memory vector database for vectors of a fixed dimensionality. All vectors inserted must match this dimension.

use plugin vectordb::{VectorDB}

let db = VectorDB(3)
db.insert("vec1", [0.1, 0.2, 0.9], #{"label": "cat"})
db.insert("vec2", [0.8, 0.1, 0.1], #{"label": "dog"})

Insert or replace a vector by ID

Inserts a vector with the given string id. If an entry with that ID already exists it is replaced. metadata is an optional table of string key-value pairs.

use plugin vectordb::{VectorDB}

let db = VectorDB(4)
db.insert("item-1", [0.1, 0.5, 0.3, 0.8], #{"category": "food"})
db.insert("item-2", [0.9, 0.2, 0.4, 0.1])

Find top-k nearest vectors by Euclidean distance

Returns the top-k entries closest to query_vector using Euclidean distance (lower is better). Each result includes id, distance, and metadata.

use plugin vectordb::{VectorDB}

let db = VectorDB(2)
db.insert("p1", [0.0, 0.0])
db.insert("p2", [3.0, 4.0])

let results = db.search_euclidean([1.0, 1.0], 2)
print(results[1]["id"])
print(results[1]["distance"])

Remove a vector by ID

Removes the entry with the given id. Returns true if the entry was found and removed, false otherwise.

use plugin vectordb::{VectorDB}

let db = VectorDB(2)
db.insert("tmp", [1.0, 2.0])
let removed = db.remove("tmp")
print(removed)

Get the number of stored vectors

Returns the number of vectors currently stored in the database.

use plugin vectordb::{VectorDB}

let db = VectorDB(2)
db.insert("a", [1.0, 0.0])
db.insert("b", [0.0, 1.0])
print(db.count())

Retrieve a vector entry by ID

Returns the entry for id as a table with id, vector, and metadata fields, or nil if not found.

use plugin vectordb::{VectorDB}

let db = VectorDB(2)
db.insert("x", [0.5, 0.5], #{"tag": "center"})
let entry = db.get("x")
print(entry["metadata"]["tag"])

List all stored IDs

Returns a list of all stored entry IDs.

use plugin vectordb::{VectorDB}

let db = VectorDB(2)
db.insert("a", [1.0, 0.0])
db.insert("b", [0.0, 1.0])
let ids = db.list_ids()
print(ids[1])

Remove all vectors from the database

Removes all entries from the database, resetting it to empty while preserving the dimension setting.

use plugin vectordb::{VectorDB}

let db = VectorDB(3)
db.insert("x", [1.0, 2.0, 3.0])
db.clear()
print(db.count())

Get the vector dimension

Returns the fixed vector dimension this database was created with.

use plugin vectordb::{VectorDB}

let db = VectorDB(128)
print(db.dimension())

Check if an ID exists in the database

Returns true if an entry with the given id exists in the database.

use plugin vectordb::{VectorDB}

let db = VectorDB(2)
db.insert("a", [1.0, 0.0])
print(db.contains("a"))
print(db.contains("z"))

Insert multiple vectors at once

Inserts multiple vectors at once. items is a list of tables, each with id (string), vector (table of numbers), and optional metadata. Returns the number of items inserted. Existing IDs are replaced.

use plugin vectordb::{VectorDB}

let db = VectorDB(2)
let n = db.batch_insert([
  #{"id": "a", "vector": [1.0, 0.0]},
  #{"id": "b", "vector": [0.0, 1.0], "metadata": #{"tag": "unit"}}
])
print("Inserted: {n}")

Update metadata for an existing entry

Updates the metadata of an existing entry without changing its vector. Returns true if the entry was found, false otherwise.

use plugin vectordb::{VectorDB}

let db = VectorDB(2)
db.insert("item", [0.5, 0.5], #{"status": "draft"})
db.update_metadata("item", #{"status": "published"})

Compute cosine similarity between two vectors

Computes the cosine similarity between two vectors (independent of any database). Returns a value from -1.0 to 1.0.

use plugin vectordb::{cosine_similarity}

let score = cosine_similarity([1.0, 0.0, 0.0], [0.9, 0.1, 0.0])
print(score)

Compute Euclidean distance between two vectors

Computes the Euclidean (L2) distance between two vectors (independent of any database).

use plugin vectordb::{euclidean_distance}

let dist = euclidean_distance([0.0, 0.0], [3.0, 4.0])
print(dist)
enespt-br