Choose a search method
Vector search can run without an index. An approximate nearest-neighbor index
reduces the work needed for larger tables, with a tradeoff between recall and
latency. Full-text search needs an FTS index before you query it.
Before you search
For vector search, generate stored and query vectors with the same embedding model and compatible settings. Their dimensions must match. Keep the fields you want to return, such as document text or an item ID, in the same table. For full-text search, choose the text column and build its index. Enterprise builds indexes in the background, so wait for the index to be ready before querying it. Phrase queries also need token positions in the index. See FTS indexes.Run a complete search example in Python, TypeScript or Rust
Run a complete search example in Python, TypeScript or Rust
These examples use three sample records and hand-written vectors, so no
embedding service is needed. Each program creates a temporary local table
and removes it when finished.This query returns the puppy and dog records, ordered by vector distance.
It searches the three-row table directly, without an ANN index or an embedding
service. For connection options, see Connect.
- Python: Install with
pip install lancedb, save assearch.py, then runpython search.py. - TypeScript: Install with
npm install @lancedb/lancedb, save assearch.mts, then runnode search.mtswith Node.js 22.18 or later. - Rust: Download and extract the complete Rust examples, then run
cargo run --locked --bin vector_searchfrom the extracted directory.
Read the results
Useselect() to choose the columns your application needs and limit() to
set the maximum number of results. The search also returns a ranking value:
These values have different meanings. Do not compare a vector distance directly
with a full-text score or treat a reranker score as a probability.
Add filters and reranking
Filters restrict which rows qualify, such as documents from a particular collection. Prefiltering searches within those rows. Postfiltering removes nonmatching rows from the candidates already retrieved, so it can leave fewer results than requested. Reranking reorders the candidates returned by retrieval. Start with representative queries and expected results, then compare changes to relevance and latency. A reranker cannot recover a relevant row that retrieval never returned.Read large result sets in batches
Materializing all rows at once can exhaust application memory. For exports and training reads, use bounded queries or the batch-reading interfaces described in Data loading. The SDK buffers Enterprise query responses before exposing batches. Set a query limit rather than relying on batch iteration alone to bound memory. Direct Lance dataset access through Python’stable.to_lance() is a LanceDB
Embedded interface. Use the SDK query builders for reads that need to work with
both LanceDB Embedded and LanceDB Enterprise.
Investigate a slow query
Use query plans to see the scans, filters and indexes selected for a query.explain_plan() inspects the plan; analyze_plan() executes the
query and reports measurements. Compare one change at a time, such as adding
an index, returning fewer columns, or adjusting search parameters.