Skip to content

Incremental engine for long horizon agents 🌟 Star if you like it!

7.9 KB
Raw Blame

Semantic Search with LanceDB and CocoIndex β€” chunk a folder of Markdown, embed each chunk locally, and store the vectors in an embedded LanceDB table on disk, with no server to run

Semantic search over Markdown, stored in LanceDB.

The Semantic Search 101 pipeline pointed at LanceDB β€” an embedded, file-based vector store with no server to stand up, no POSTGRES_URL, just a directory on disk you can copy to move.
Walk, chunk, embed locally, store β€” incrementally β€” in plain async Python.

Star us β€οΈ β†’ Star CocoIndex on GitHub  Β·  CocoIndex documentation  Β·  Join the CocoIndex Discord

stars pypi discord license


This is Semantic Search 101 with one thing changed: the vectors land in LanceDB instead of Postgres. LanceDB is an embedded, file-based vector store β€” no server to stand up, just a ./lancedb_data/ directory created on first run. You declare the transformation in native Python and your own types β€” target_state = transformation(source_state) β€” and the heavy lifting (incremental processing, change tracking, managed targets) runs in a Rust engine underneath, so editing one file re-embeds one file, not the whole folder.

How it works

The chunk-and-embed half is byte-for-byte the base example β€” RecursiveSplitter cuts each file into overlapping Markdown chunks, and a local SentenceTransformerEmbedder (all-MiniLM-L6-v2, no API key) turns each into a vector. What changes is the resource and the target: a LanceAsyncConnection instead of an asyncpg pool, and lancedb.mount_table_target instead of the Postgres one β€” same call shape, same table.declare_row(...). Read it in main.py:

@coco.lifespan
async def coco_lifespan(builder: coco.EnvironmentBuilder) -> AsyncIterator[None]:
    conn = await lancedb.connect_async(LANCEDB_URI)   # the "connection" is just a path on disk
    builder.provide(LANCE_DB, conn)
    builder.provide(EMBEDDER, SentenceTransformerEmbedder(EMBED_MODEL))
    yield

@coco.fn
async def app_main(sourcedir: pathlib.Path) -> None:
    target_table = await lancedb.mount_table_target(
        LANCE_DB, table_name=TABLE_NAME,
        table_schema=await lancedb.TableSchema.from_class(DocEmbedding, primary_key=["id"]),
    )
    files = localfs.walk_dir(sourcedir, recursive=True,
        path_matcher=PatternFilePathMatcher(included_patterns=["**/*.md"]), live=True)
    await coco.mount_each(process_file, files.items(), target_table)

lancedb.mount_table_target is the LanceDB counterpart to the Postgres mount_table_target: it creates and manages the table, handles idempotent upserts keyed on the primary key, and cleans up orphan rows when a file disappears. Only the import changed.

πŸ“˜ Full Tutorial β†’
Step-by-step walkthrough with the LanceDB connection, the managed table target, the row schema, and the async search query.

Why it's worth a star ⭐

  • Zero infrastructure. No database to install, no POSTGRES_URL β€” LanceDB writes to ./lancedb_data/, created on first run. To start fresh, delete the directory and re-run.
  • Portable by design. Data lives in one directory on disk; copy it to move the whole index.
  • Managed table target. lancedb.mount_table_target owns the schema, idempotent upserts, and orphan cleanup β€” the same guarantees the Postgres target gives, against a local store.
  • Incremental by default. @coco.fn(memo=True) skips files whose content and code are unchanged; each row's id is derived from its chunk text, so only changed rows are upserted and vanished ones are deleted.
  • Same flow, different store. The chunk-and-embed code is identical to the Postgres version β€” proof the target is a swappable detail. The same local embedder is reused at query time so indexing and search stay consistent.

Run it

No database to install β€” LanceDB is embedded and writes to ./lancedb_data/, created on first run.

1. Configure & install:

cp .env.example .env     # no required secrets; optional LANCEDB_URI override
pip install -e .

2. Build the index β€” the example ships a markdown_files/ folder of sample docs:

cocoindex update main          # catch-up: scan, sync, exit
cocoindex update -L main       # live: keep watching for file changes

3. Search β€” embeds your query with the same model and returns the nearest vectors via LanceDB's async search:

python main.py "what is self-attention?"

The most semantically similar chunks come back ranked β€” even when they share none of the words in your query.


If this gave you a portable, server-free vector index, give CocoIndex a star ⭐ β€” it helps a lot.
Docs Β· Walkthrough Β· Discord Β· See all examples β†’