Files
lancedb/docs/web/search/index.mdx
T
Xuanwo b5f35d5c56 fix: honor Enterprise query batch size limits (#5350)
Enterprise queries could return Arrow batches larger than the size
explicitly requested by the caller. The SDK now slices oversized batches
in the final query stream and keeps server-selected batch sizes when the
caller omits the limit or passes zero.

The search guide shows how to stream a full table or filtered scan in
one query using Python's `search().to_batches()`. It covers older
deployments that return buffered Arrow IPC files or enforce a query row
cap.

A local Enterprise 1.16.0 probe streamed all 131,073 rows from a pinned
snapshot in one query, with batches of at most 37 rows and the first
batch available before the Arrow end-of-stream marker. The probe ran
with Plan Executor enabled and disabled.

Related to OSS-3133.
2026-12-02 09:42:17 +00:00

146 lines
7.0 KiB
Plaintext

---
title: Search
sidebarTitle: Overview
description: "Choose vector, full-text or hybrid search and return the records your application needs."
icon: "list"
availability:
embedded: available
enterprise: available
---
import { PyScanBatches, PyVectorSearch, TsVectorSearchBody, TsVectorSearchTail } from '/snippets/search_examples.mdx';
import { RsScanBatches } from '/snippets/search_filter_search.mdx';
import { RsExampleBody as RsVectorSearchBody, RsExampleTail as RsVectorSearchTail } from '/snippets/search_vector_search.mdx';
LanceDB supports vector, full-text and hybrid search. Use vector search to find
similar content, full-text search to match words, or hybrid search to combine
both. Filters narrow the results to records that match your criteria.
## Choose a search method
| You want to find | Use | Prepare |
|:----------------|:----|:--------|
| Items similar to a query, such as related passages or images | [Vector search](/search/vector-search) | Stored vectors and a compatible query vector |
| Documents represented by several embeddings | [Multivector search](/search/vector-search#multivector) | A multivector column and compatible query vectors |
| Records containing particular words or phrases | [Full-text search](/search/full-text-search) | An FTS index on the text column |
| Relevant meaning alongside keyword matches | [Hybrid search](/search/hybrid-search) | Vectors and an FTS index |
| Rows matching a condition, without similarity ranking | [Filtering](/search/filtering) | A predicate over table columns |
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 {#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](/indexing/fts-index).
<span id="run-a-first-query"></span>
<Accordion title="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.
- **Python:** Install with `pip install lancedb`, save as `search.py`, then run `python search.py`.
- **TypeScript:** Install with `npm install @lancedb/lancedb`, save as `search.mts`, then run `node search.mts` with Node.js 22.18 or later.
- **Rust:** Download and extract the [complete Rust examples](https://raw.githubusercontent.com/lancedb/lancedb/2af85bea8999c8cd23ec01ff62ba110c54652c10/docs/web/static/code/search-rust.zip), then run `cargo run --locked --bin vector_search` from the extracted directory.
<CodeGroup>
<CodeBlock filename="Python" language="python" icon="python">
{PyVectorSearch}
</CodeBlock>
<CodeBlock filename="TypeScript" language="typescript" icon="square-js">
{TsVectorSearchBody + "\n" + TsVectorSearchTail}
</CodeBlock>
<CodeBlock filename="Rust" language="rust" icon="rust">
{RsVectorSearchBody + "\n" + RsVectorSearchTail}
</CodeBlock>
</CodeGroup>
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](/basics/catalog).
</Accordion>
## Read the results
Use `select()` to choose the columns your application needs and `limit()` to
set the maximum number of results. The search also returns a ranking value:
| Search method | Ranking column | Order |
|:--------------|:---------------|:------|
| Vector | `_distance` | Smaller distance first |
| Full-text | `_score` | Higher text relevance first |
| Hybrid | `_relevance_score` | Higher combined relevance first |
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 {#performance}
[Filters](/search/filtering) 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](/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 {#avoid-materializing-the-whole-table}
For an export or a full table scan, build a query without a search term or
vector and read the results one batch at a time. LanceDB Enterprise runs one
query, so the scan needs no key column, index or pagination. The examples
read the `text` and `category` columns from an open table.
<CodeGroup>
<CodeBlock filename="Python" language="python" icon="python">
{PyScanBatches}
</CodeBlock>
<CodeBlock filename="Rust" language="rust" icon="rust">
{RsScanBatches}
</CodeBlock>
</CodeGroup>
The row ID option includes `_rowid` in each batch. To restrict the scan, add a
predicate with `where()` in Python or `only_if()` in Rust. Omit `limit()` to read
every matching row. Use `limit(n)` to cap the total number of rows.
Python's `batch_size` and Rust's `max_batch_length` cap the rows per batch
delivered to the application. The SDK decodes each server batch at its original
size before slicing it. In TypeScript, `for await (const batch of table.query())`
uses the default batch sizing. TypeScript's `maxBatchLength` option is available
on `toArrow()` and `toArray()`, which collect the full result.
Use a current SDK with LanceDB Enterprise 1.16.0 or later for this workflow. SDK
0.42.0 introduced streaming Arrow IPC query responses. Python and Rust SDKs
through 0.42.1 may return Enterprise batches larger than the requested cap.
Older deployments can return buffered Arrow IPC files or reject scans without
`limit()` if they enforce a query row cap.
To pin a retained version, call `table.checkout(version)` in Python or
`table.checkout(version).await?` in Rust before building the query. If the
stream is interrupted, the batches already read are a partial result. Python
raises `ResponseInterruptedError` and Rust returns `Error::ResponseInterrupted`.
For training, see [Data loading](/training/data-loading). Python's
`table.to_lance()` gives direct Lance dataset access in LanceDB Embedded.
## Investigate a slow query {#diagnostics}
Use [query plans](/basics/sql#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.