Skip to content
Luna
Esc
↑↓navigate↵open⌘Jpreview
On this page

Search vectors

Store embeddings in a vector column and find the rows nearest a vector, exactly or with an index for large tables.

Words find rows that say the same thing, but not rows that mean the same thing in other words. Store an embedding of each row from a model of your choice in a vector column, and Luna finds the rows whose embeddings are nearest a query’s.

Find the nearest rows

t.vector(dims) is a column of dims numbers, from 1 to 8192. The table’s nearest method returns the rows nearest a vector you pass.

import { defineLuna, t, table } from "luna";

export const Board = defineLuna({
  name: "Board",
  key: { id: t.string() },
  schema: {
    cards: table({
      id: t.id(),
      title: t.string(),
      lane: t.string(),
      embedding: t.vector(1536).optional(),
    }),
  },
  handlers: (h) => ({
    similar: h({ args: { to: t.array(t.float()) }, query: true }, (ctx, { to }) =>
      ctx.db.cards
        .nearest("embedding", to, { k: 5 })
        .map((hit) => ({ title: hit.row.title, distance: hit.distance })),
    ),
  }),
});

Each hit has the row and its distance, nearest first. k sets how many come back, 10 by default and at most 1,000. Rows whose vector is null are left out.

Write a vector

Write a Float32Array of the column’s length, and read one back the same way.

add: h(
  { args: { title: t.string(), embedding: t.array(t.float()) } },
  (ctx, { title, embedding }) =>
    ctx.db.cards.insert({
      title,
      lane: "todo",
      embedding: Float32Array.from(embedding),
    }),
),

A vector of another length is refused, and the write fails with an error such as a vector of 1536 numbers, not 3. A vector column cannot be unique or have a default.

Pick a distance metric

metric decides how distance is measured. A smaller distance is nearer, whichever you pick.

metric Measures
"cosine" How far apart the directions of two vectors are. Two vectors pointing the same way are 0 apart. The default
"l2" The straight-line distance
"l1" The sum of the absolute differences along each dimension
embedding: t.vector(1536, { metric: "l2" }).optional(),

Use the metric your embedding model was made for.

Index a large table

Without an index, nearest compares the query with every row, and its answer is exact. index: true keeps an approximate index, so a large table is searched without reading every row.

embedding: t.vector(1536, { index: true }).optional(),

A table with fewer than 10,000 vectors is still searched exactly. Above that, the search is approximate: it may miss some of the true nearest rows, and it reads far fewer. Pass exact: true to compare with every row anyway.

ctx.db.cards.nearest("embedding", to, { k: 5, exact: true });

Adding index: true to a table that already has rows builds the index from them.

Narrow the results with where

where keeps only rows that meet other conditions, with the operators of Read and write rows.

ctx.db.cards.nearest("embedding", to, { k: 5, where: { lane } });

With an index, Luna first takes the nearest k times 10 candidates, at most 10,000, and applies the conditions to them. When fewer than k candidates meet the conditions, it compares with every row instead, so you still get k hits when that many rows match.

Next steps

Was this page helpful?