Litestone guide / Built in
Vectors
@vector(n) stores an embedding on a Bytes column and lets you order rows by how near they are to a query vector.
model Passage {
id Int (autoincrement())
body String
embedding Bytes? (4)
}
await db.passage.create({ data: { body: 'east', embedding: new Float32Array([1, 0, 0, 0]) } })
await db.passage.create({ data: { body: 'north', embedding: new Float32Array([0, 1, 0, 0]) } })
await db.passage.create({ data: { body: 'near', embedding: new Float32Array([0.9, 0.1, 0, 0]) } })
const hits = await db.passage.findMany({
orderBy: { embedding: { near: [1, 0, 0, 0] } },
limit: 10,
})
if (hits.map(h => h.body).join() !== 'east,near,north') throw new Error(hits.map(h => h.body).join())
What else it can do
- Retrieval is an ordering on
findMany, so it composes withwhere,select, the gate and row policies - Each row carries its cosine distance as
_distance, 0 identical, 1 orthogonal, 2 opposite - There is no threshold option; compare
_distanceyourself nearmust be the first sort key, and later keys break ties- Page with
limitandoffset;findManyCursorrefuses a distance ordering - The vector is left out of a default read, and
select: { embedding: true }brings it back - A row with no vector yet is absent from a nearest list
- A
wherethat prunes rows first makes the scan cheaper - With
sqlite-vecinstalled (bun add sqlite-vec) the distance runs inside SQLite, about 2x faster - Without it the same query runs in JavaScript, which is the only path in the browser
- Embedding the text is your app's job, and chunking too
- Not combinable with
@encrypted,@secret,@hashed,@uniqueor@@fts
In depth: Vectors, § Retrieval is an ordering, not a verb, § The two engines