← Blog
A purple catalog drawer holds ivory cards with one orange tab raised above the row.
Published on
2026-11-27

Build a persistent vector search table with LanceDB on CPU

Suppose a support agent asks for the two closest documents in the support collection. The two nearest vectors in the whole catalog both belong to engineering. If the application asks for two results and then discards the engineering documents, the agent sees an empty list. Three support documents were available all along.

This example uses the embedded LanceDB vector database on CPU. It creates one table on disk, ranks documents within a chosen collection, and repeats the queries after the writer exits. Five hand-picked vectors make it possible to check every result with a pencil.

Give each vector a document ID and a collection

We tested with Python 3.12.14, LanceDB 0.41.0 and PyArrow 26.0.1. In a fresh Python environment, install the two packages below. LanceDB opens a directory on the local filesystem, so you can run both scripts without a database server or a GPU. The open-source LanceDB project maintains the embedded library.

python -m pip install lancedb==0.41.0 pyarrow==26.0.1

Save this as build_catalog.py. The table has a text ID, a collection label and a fixed-size vector of three 32-bit floats. The collection column groups rows inside the same table. In an application, the ID would point back to the document you want to display.

import sys
from pathlib import Path

import lancedb
import pyarrow as pa

ROWS = [
    ("release-notes", "engineering", [1, 1, 0]),
    ("api-limits",    "engineering", [2, 1, 0]),
    ("reset-access",  "support",     [1, 1, 2]),
    ("check-status",  "support",     [1, 4, 0]),
    ("replace-token", "support",     [1, 1, 4]),
]

def create_catalog(path):
    schema = pa.schema([
        ("doc_id", pa.string()),
        ("collection", pa.string()),
        ("vector", pa.list_(pa.float32(), 3)),
    ])
    records = [dict(zip(schema.names, row)) for row in ROWS]
    db = lancedb.connect(str(Path(path).resolve()))
    return db.create_table("docs", data=records, schema=schema)

if __name__ == "__main__":
    print(create_catalog(sys.argv[1]).count_rows())

These coordinates are deliberately simple. They let you test storage and ranking independently of an embedding model. When you switch to real text, use the same embedding model and preprocessing for documents and queries, and set the vector width to the model’s output dimension. If you are still deciding where the vectors should live, the vector and relational database comparison covers that choice.

Filter the eligible documents before selecting the nearest two

Save the reader below as find_docs.py. It searches for the vector [1, 1, 0] and requests two results. With prefilter=True, the collection condition determines which rows can compete for those two places. The example uses fixed predicates for the two named collections.

import sys
from pathlib import Path

import lancedb

QUERY = [1.0, 1.0, 0.0]

def nearest(table, predicate=None):
    search = table.search(QUERY).distance_type("l2").bypass_vector_index()
    if predicate is not None:
        search = search.where(predicate, prefilter=True)
    rows = search.select(["doc_id", "_distance"]).limit(2).to_list()
    return [(row["doc_id"], row["_distance"]) for row in rows]

if __name__ == "__main__":
    db = lancedb.connect(str(Path(sys.argv[1]).resolve()))
    docs = db.open_table("docs")
    print(nearest(docs))
    print(nearest(docs, "collection = 'support'"))
    print(nearest(docs, "collection = 'legal'"))

The reader specifies the L2 metric and calls bypass_vector_index() for an exact search. We built no index on this small table. Bypassing one explicitly lets you keep the same reference query if you add an approximate index later. The LanceDB Python API reference documents the query methods.

Reopen the table from another process

Run these commands from the same working directory. Choose a new catalog path for the first run. The builder creates docs and prints 5. Once it exits, the reader connects to the same directory and opens the existing table. For subsequent reads, run the second command by itself.

python build_catalog.py ./vector-catalog
python find_docs.py ./vector-catalog
  • Whole catalog. The reader returns release-notes at distance 0 and api-limits at 1. Both belong to engineering.
  • Support collection. It returns reset-access at 4 and check-status at 9. The third support document, replace-token, is farther away at 16.
  • Legal collection. It returns []. No row carries that label. The application can show an empty result without substituting a document from another collection.

In this run, _distance held the squared Euclidean distance. For reset-access, only the third coordinate differs from the query, by 2. Squaring gives 4. For check-status, the second coordinate differs by 3, giving 9. Lower values rank first. If you compare these scores against a threshold expressed in ordinary Euclidean units, account for the square.

We calculated the rankings separately in Python by selecting the eligible rows, summing the squared coordinate differences, and sorting. The writer and a fresh reader both matched those IDs and distances. The reader also saw the original schema and all five rows.

Use the small catalog to check a deployment

Give the writer and reader the same absolute data path. Two services can resolve ./vector-catalog to different directories if they start in different locations. On a CPU instance from Compute with Hivenet, put the catalog on storage you intend to retain, restart the reader, and repeat one known search. A process restart tests reopening. Recovery after losing the disk requires a separate backup and restore test.

For a larger catalog, keep exact results for a representative set of queries. If you introduce an approximate index, compare its returned IDs with those reference IDs and measure latency under your normal collection filters. The five-row example gives you a correctness check. Sizing a CPU instance calls for measurements with your own vectors and concurrent readers.

Your next workload belongs on Hivenet.

Pick one AI, compute, or storage workload and see the difference for yourself. Spin it up in minutes, or let our team map your fastest path to production.