Search and memory
An embedding is a numeric vector produced by an embedding model. Generate stored embeddings and query embeddings with the same model and dimensions.
The small vectors below illustrate query behavior. They are not outputs from an embedding model.
From your language
Section titled “From your language”Store records with their vectors and return the nearest ones. Each client sends the distance query through its query call.
db.execute("CREATE TABLE memories (id BIGINT PRIMARY KEY, note TEXT, embedding VECTOR(3))")
db.execute("INSERT INTO memories VALUES (1, 'refund policy', '[1,0,0]'), "
"(2, 'shipping times', '[0,1,0]'), (3, 'return window', '[0.9,0.1,0]')")
print(db.query("SELECT id, note FROM memories ORDER BY embedding <-> $1 LIMIT 2", ["[1,0,0]"]))[{'id': 1, 'note': 'refund policy'}, {'id': 3, 'note': 'return window'}]await db.execute('CREATE TABLE memories (id BIGINT PRIMARY KEY, note TEXT, embedding VECTOR(3))');
await db.execute(`INSERT INTO memories VALUES (1, 'refund policy', '[1,0,0]'),
(2, 'shipping times', '[0,1,0]'), (3, 'return window', '[0.9,0.1,0]')`);
const { rows } = await db.query('SELECT id, note FROM memories ORDER BY embedding <-> $1 LIMIT 2', ['[1,0,0]']);
console.log(rows);[ { id: 1, note: 'refund policy' }, { id: 3, note: 'return window' } ]db.execute("CREATE TABLE memories (id BIGINT PRIMARY KEY, note TEXT, embedding VECTOR(3))");
db.execute("INSERT INTO memories VALUES (1, 'refund policy', '[1,0,0]'), "
+ "(2, 'shipping times', '[0,1,0]'), (3, 'return window', '[0.9,0.1,0]')");
System.out.println(db.query("SELECT id, note FROM memories ORDER BY embedding <-> ? LIMIT 2",
"[1,0,0]").getRows());[Row{id=1, note=refund policy}, Row{id=3, note=return window}]db.Exec(ctx, "CREATE TABLE memories (id BIGINT PRIMARY KEY, note TEXT, embedding VECTOR(3))")
db.Exec(ctx, `INSERT INTO memories VALUES (1, 'refund policy', '[1,0,0]'),
(2, 'shipping times', '[0,1,0]'), (3, 'return window', '[0.9,0.1,0]')`)
rows, _ := db.Query(ctx, "SELECT id, note FROM memories ORDER BY embedding <-> $1 LIMIT 2", "[1,0,0]")
nearest, _ := particledb.CollectRows(rows, particledb.RowToMap)
fmt.Println(nearest)[map[id:1 note:refund policy] map[id:3 note:return window]]db.execute("CREATE TABLE memories (id BIGINT PRIMARY KEY, note TEXT, embedding VECTOR(3))", &[])?;
db.execute("INSERT INTO memories VALUES (1, 'refund policy', '[1,0,0]'), \
(2, 'shipping times', '[0,1,0]'), (3, 'return window', '[0.9,0.1,0]')", &[])?;
let query = Value::Vector(vec![1.0, 0.0, 0.0]);
for row in db.query("SELECT id, note FROM memories ORDER BY embedding <-> $1 LIMIT 2", &[query])?.rows {
println!("{:?}", row.values());
}[Int64(1), String("refund policy")]
[Int64(3), String("return window")]CREATE TABLE memories (id BIGINT PRIMARY KEY, note TEXT, embedding VECTOR(3));
INSERT INTO memories VALUES (1, 'refund policy', '[1,0,0]'),
(2, 'shipping times', '[0,1,0]'), (3, 'return window', '[0.9,0.1,0]');
SELECT id, note FROM memories ORDER BY embedding <-> '[1,0,0]' LIMIT 2;1|refund policy
3|return windowStore records with vectors
Section titled “Store records with vectors”CREATE TABLE knowledge ( id BIGINT PRIMARY KEY, account_id BIGINT, content TEXT, embedding VECTOR(3));INSERT INTO knowledge VALUES (1, 7, 'Deployment guide', '[1,0,0]'), (2, 7, 'API reference', '[0,1,0]'), (3, 9, 'Private account note', '[1,0,0]');Retrieve context for one account
Section titled “Retrieve context for one account”SELECT id, contentFROM knowledgeWHERE account_id = 7ORDER BY embedding <-> '[1,0,0]'LIMIT 2;The deployment guide ranks first. Record 3 is excluded by the account filter.
A filter is part of your query, not a complete authorization system. Derive the account scope from authenticated application identity and enforce it on every access path. Security.
Add an index for larger data
Section titled “Add an index for larger data”Choose a supported index and distance metric using the vector reference. Measure retrieval quality and latency on representative embeddings before choosing index parameters.
Next steps
Section titled “Next steps”This site uses Google Analytics cookies to count visits. Privacy · License