mirror of
https://github.com/lancedb/lancedb.git
synced 2026-10-13 15:22:30 +00:00
Move the public website pages into `docs/web` beside the SDK, with
shared navigation and executable example sources in `docs/web-tests`.
The combined website is assembled by
[lancedb/docs#355](https://github.com/lancedb/docs/pull/355), with six
Enterprise replacements from
[lancedb/sophon#7681](https://github.com/lancedb/sophon/pull/7681).
This includes Function guidance, runnable Quickstarts, regenerated
snippets, production contact/demo fixes, the legacy SQL route, and the
LeRobot redirect. LeRobot remains attributed to Geneva; all 34 Geneva
pages remain in the docs repository until Function officially launches.
Materialized-view guidance follows the full rebuild refresh introduced
by #4373: local refresh writes the complete result, has no `full`
argument, supports `source_version`, and no longer requires stable row
IDs. Deployment SQL refresh options and stable-ID setup are qualified by
version/configuration. Function input guidance distinguishes ordinary
stored columns from computed inputs supported by newer deployments.
**Validation:** independently accepted local checks include 95 Python
tests (real Enterprise excluded), eight TypeScript tests, Rust
Quickstart, snippet parity, typos, deterministic assembly, Mint
validation and links/anchors/redirects. The combined baseline retains
all 181 production routes and 17 legacy redirects. Examples ran locally
against source-built Python SDK 0.40.0b13; existing GitHub workflows do
not run `docs/web-tests`. New main at `069b2f7d` is 0.41.0b0 and adds
computed Function inputs; that SDK was not exercised by the accepted
tests. Latest prose corrections receive focused site validation.
**Merge/rollout:** merge this and #7681 before #355's combined CI, which
reads producer `main`. This is migration infrastructure: section
overlays, source-owned skills and automatic versioned paired releases
remain follow-ups. The current manual publisher is transitional. Hosted
validation, a supported SDK/Enterprise release pair and a real
Enterprise walkthrough remain pending.
Production Mintlify watches `lancedb/docs:deploy-freeze`, directory
`docs`; preserve that source through review. The separate SDK GitHub
Pages reference workflow rebuilds on main merges.
**Updated-head CI (9a50f8db):**
[Typos](https://github.com/lancedb/lancedb/actions/runs/37654185443/job/112904990558),
[PR
title](https://github.com/lancedb/lancedb/actions/runs/37654179507/job/112904974595)
and
[Aikido](https://app.aikido.dev/featurebranch/scan/215386145?groupId=74229)
pass. Critical findings in the example lockfiles were patched: all Sharp
copies use 0.35.5, protobufjs 7.6.6, tar 7.5.22, quinn-proto 0.11.19,
zerovec 0.11.8 and zerovec-derive 0.11.6. The SDK's product dependencies
are unchanged. The revised lockfiles pass eight TypeScript examples,
HuggingFace decode/resize/crop/rescale/normalize plus AVIF roundtrip,
and Rust Quickstart. Aikido passing does not mean every advisory is
cleared; npm still reports 29 high and five moderate advisories in the
example dependency graph. No scanner bypass or suppression was added.
The Function prose correction passes combined/standalone Mint,
deterministic assembly, routes and assets. The revised 325-file site
checksum is
`175bec06864abb400dd292e667ba93448c0479b8359fd7bf6146afabf9040647`. This
PR remains open for independent review; further merges are on hold.
---------
Co-authored-by: Brendan Clement <brendan.clement@lancedb.com>
312 lines
9.5 KiB
Rust
312 lines
9.5 KiB
Rust
// SPDX-License-Identifier: Apache-2.0
|
|
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
|
|
|
//! This example demonstrates vector search query options.
|
|
//!
|
|
//! Snippets from this example are used in the documentation on vector search.
|
|
|
|
use std::sync::Arc;
|
|
|
|
use arrow_array::types::Float32Type;
|
|
use arrow_array::{
|
|
Array, FixedSizeListArray, Int32Array, Int64Array, RecordBatch, RecordBatchIterator,
|
|
RecordBatchReader, UInt8Array,
|
|
};
|
|
use arrow_schema::{DataType, Field, Schema};
|
|
|
|
use futures::TryStreamExt;
|
|
use lancedb::connection::Connection;
|
|
use lancedb::index::vector::IvfFlatIndexBuilder;
|
|
use lancedb::index::Index;
|
|
use lancedb::query::{ExecutableQuery, QueryBase, Select};
|
|
use lancedb::{connect, DistanceType, Result, Table};
|
|
|
|
const DIM: usize = 128;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<()> {
|
|
if std::path::Path::new("data").exists() {
|
|
std::fs::remove_dir_all("data").unwrap();
|
|
}
|
|
let uri = "data/sample-lancedb";
|
|
let db = connect(uri).execute().await?;
|
|
let tbl = create_table(&db).await?;
|
|
tbl.create_index(&["vector"], Index::Auto).execute().await?;
|
|
|
|
configure_distance_metric(&tbl).await?;
|
|
exact_vs_approximate(&tbl).await?;
|
|
search_distance_range(&tbl).await?;
|
|
vector_search_prefilter(&tbl).await?;
|
|
vector_search_postfilter(&tbl).await?;
|
|
fast_search(&tbl).await?;
|
|
brute_force_search(&tbl).await?;
|
|
bypass_vector_index(&tbl).await?;
|
|
batch_search(&tbl).await?;
|
|
binary_search(&db).await?;
|
|
Ok(())
|
|
}
|
|
|
|
fn create_some_records() -> Result<Box<dyn RecordBatchReader + Send>> {
|
|
const TOTAL: usize = 1000;
|
|
|
|
let schema = Arc::new(Schema::new(vec![
|
|
Field::new("id", DataType::Int32, false),
|
|
Field::new(
|
|
"vector",
|
|
DataType::FixedSizeList(
|
|
Arc::new(Field::new("item", DataType::Float32, true)),
|
|
DIM as i32,
|
|
),
|
|
true,
|
|
),
|
|
]));
|
|
|
|
let batches = RecordBatchIterator::new(
|
|
vec![RecordBatch::try_new(
|
|
schema.clone(),
|
|
vec![
|
|
Arc::new(Int32Array::from_iter_values(0..TOTAL as i32)),
|
|
Arc::new(
|
|
FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
|
|
(0..TOTAL).map(|_| Some(vec![Some(1.0); DIM])),
|
|
DIM as i32,
|
|
),
|
|
),
|
|
],
|
|
)
|
|
.unwrap()]
|
|
.into_iter()
|
|
.map(Ok),
|
|
schema.clone(),
|
|
);
|
|
Ok(Box::new(batches))
|
|
}
|
|
|
|
async fn create_table(db: &Connection) -> Result<Table> {
|
|
let initial_data: Box<dyn RecordBatchReader + Send> = create_some_records()?;
|
|
let tbl = db
|
|
.create_table("my_vectors", initial_data)
|
|
.execute()
|
|
.await
|
|
.unwrap();
|
|
Ok(tbl)
|
|
}
|
|
|
|
async fn configure_distance_metric(table: &Table) -> Result<()> {
|
|
let query_vector = [1.0; DIM];
|
|
// --8<-- [start:configure_distance_metric]
|
|
// Use the same distance metric the index was trained with.
|
|
let mut results = table
|
|
.vector_search(&query_vector)?
|
|
.distance_type(DistanceType::Cosine)
|
|
.limit(10)
|
|
.execute()
|
|
.await?;
|
|
while let Some(batch) = results.try_next().await? {
|
|
println!("{:?}", batch);
|
|
}
|
|
// --8<-- [end:configure_distance_metric]
|
|
Ok(())
|
|
}
|
|
|
|
async fn exact_vs_approximate(table: &Table) -> Result<()> {
|
|
let query_vector = [1.0; DIM];
|
|
// --8<-- [start:exact_vs_approximate]
|
|
// Approximate ANN search (fast, distances may come from the index representation)
|
|
let mut fast_results = table.vector_search(&query_vector)?.limit(10).execute().await?;
|
|
while let Some(batch) = fast_results.try_next().await? {
|
|
println!("{:?}", batch);
|
|
}
|
|
|
|
// Rerank a larger candidate set on full vectors for better recall
|
|
let mut refined_results = table
|
|
.vector_search(&query_vector)?
|
|
.limit(10)
|
|
.refine_factor(20)
|
|
.execute()
|
|
.await?;
|
|
while let Some(batch) = refined_results.try_next().await? {
|
|
println!("{:?}", batch);
|
|
}
|
|
// --8<-- [end:exact_vs_approximate]
|
|
Ok(())
|
|
}
|
|
|
|
async fn search_distance_range(table: &Table) -> Result<()> {
|
|
let query_vector = [1.0; DIM];
|
|
// --8<-- [start:search_distance_range]
|
|
// Only return rows whose distance falls within [0.1, 0.5).
|
|
let mut results = table
|
|
.vector_search(&query_vector)?
|
|
.distance_range(Some(0.1), Some(0.5))
|
|
.execute()
|
|
.await?;
|
|
while let Some(batch) = results.try_next().await? {
|
|
println!("{:?}", batch);
|
|
}
|
|
// --8<-- [end:search_distance_range]
|
|
Ok(())
|
|
}
|
|
|
|
async fn vector_search_prefilter(table: &Table) -> Result<()> {
|
|
let query_vector = [1.0; DIM];
|
|
// --8<-- [start:vector_search_prefilter]
|
|
// Prefiltering is the default: the filter is applied before vector search.
|
|
let mut results = table
|
|
.vector_search(&query_vector)?
|
|
.only_if("id > 100")
|
|
.select(Select::columns(&["id"]))
|
|
.limit(5)
|
|
.execute()
|
|
.await?;
|
|
while let Some(batch) = results.try_next().await? {
|
|
println!("{:?}", batch);
|
|
}
|
|
// --8<-- [end:vector_search_prefilter]
|
|
Ok(())
|
|
}
|
|
|
|
async fn vector_search_postfilter(table: &Table) -> Result<()> {
|
|
let query_vector = [1.0; DIM];
|
|
// --8<-- [start:vector_search_postfilter]
|
|
// Apply the filter after vector search by calling postfilter().
|
|
let mut results = table
|
|
.vector_search(&query_vector)?
|
|
.only_if("id > 100")
|
|
.postfilter()
|
|
.select(Select::columns(&["id"]))
|
|
.limit(5)
|
|
.execute()
|
|
.await?;
|
|
while let Some(batch) = results.try_next().await? {
|
|
println!("{:?}", batch);
|
|
}
|
|
// --8<-- [end:vector_search_postfilter]
|
|
Ok(())
|
|
}
|
|
|
|
async fn fast_search(table: &Table) -> Result<()> {
|
|
let query_vector = [1.0; DIM];
|
|
// --8<-- [start:fast_search]
|
|
// Skip unindexed data for lower latency.
|
|
let mut results = table
|
|
.vector_search(&query_vector)?
|
|
.fast_search()
|
|
.limit(5)
|
|
.execute()
|
|
.await?;
|
|
while let Some(batch) = results.try_next().await? {
|
|
println!("{:?}", batch);
|
|
}
|
|
// --8<-- [end:fast_search]
|
|
Ok(())
|
|
}
|
|
|
|
async fn brute_force_search(table: &Table) -> Result<()> {
|
|
let query_vector = [1.0; DIM];
|
|
// --8<-- [start:brute_force_search]
|
|
// A plain vector search returns the top-k closest rows.
|
|
let mut results = table.vector_search(&query_vector)?.limit(3).execute().await?;
|
|
while let Some(batch) = results.try_next().await? {
|
|
println!("{:?}", batch);
|
|
}
|
|
// --8<-- [end:brute_force_search]
|
|
Ok(())
|
|
}
|
|
|
|
async fn bypass_vector_index(table: &Table) -> Result<()> {
|
|
let query_vector = [1.0; DIM];
|
|
// --8<-- [start:bypass_vector_index]
|
|
// Force an exhaustive (flat) scan for exact, ground-truth results.
|
|
let mut results = table
|
|
.vector_search(&query_vector)?
|
|
.bypass_vector_index()
|
|
.limit(5)
|
|
.execute()
|
|
.await?;
|
|
while let Some(batch) = results.try_next().await? {
|
|
println!("{:?}", batch);
|
|
}
|
|
// --8<-- [end:bypass_vector_index]
|
|
Ok(())
|
|
}
|
|
|
|
async fn batch_search(table: &Table) -> Result<()> {
|
|
let query_1 = [1.0; DIM];
|
|
let query_2 = [0.5; DIM];
|
|
// --8<-- [start:batch_search]
|
|
// Search multiple query vectors in one call. Each result row carries a
|
|
// `query_index` mapping it back to the query it matched.
|
|
let mut results = table
|
|
.vector_search(&query_1)?
|
|
.add_query_vector(&query_2)?
|
|
.limit(5)
|
|
.execute()
|
|
.await?;
|
|
while let Some(batch) = results.try_next().await? {
|
|
println!("{:?}", batch);
|
|
}
|
|
// --8<-- [end:batch_search]
|
|
Ok(())
|
|
}
|
|
|
|
async fn binary_search(db: &Connection) -> Result<()> {
|
|
// A 256-bit binary vector is stored as 256 / 8 = 32 packed uint8 bytes.
|
|
const NUM_BYTES: i32 = 32;
|
|
const TOTAL: usize = 1024;
|
|
|
|
let schema = Arc::new(Schema::new(vec![
|
|
Field::new("id", DataType::Int64, false),
|
|
Field::new(
|
|
"vector",
|
|
DataType::FixedSizeList(
|
|
Arc::new(Field::new("item", DataType::UInt8, true)),
|
|
NUM_BYTES,
|
|
),
|
|
true,
|
|
),
|
|
]));
|
|
|
|
let values =
|
|
UInt8Array::from_iter_values((0..TOTAL * NUM_BYTES as usize).map(|i| (i % 256) as u8));
|
|
let vectors = FixedSizeListArray::try_new(
|
|
Arc::new(Field::new("item", DataType::UInt8, true)),
|
|
NUM_BYTES,
|
|
Arc::new(values),
|
|
None,
|
|
)
|
|
.unwrap();
|
|
let batch = RecordBatch::try_new(
|
|
schema.clone(),
|
|
vec![
|
|
Arc::new(Int64Array::from_iter_values(0..TOTAL as i64)),
|
|
Arc::new(vectors),
|
|
],
|
|
)
|
|
.unwrap();
|
|
let reader: Box<dyn RecordBatchReader + Send> =
|
|
Box::new(RecordBatchIterator::new(vec![Ok(batch)].into_iter(), schema.clone()));
|
|
let tbl = db.create_table("binary_vectors", reader).execute().await?;
|
|
tbl.create_index(
|
|
&["vector"],
|
|
Index::IvfFlat(IvfFlatIndexBuilder::default().distance_type(DistanceType::Hamming)),
|
|
)
|
|
.execute()
|
|
.await?;
|
|
|
|
// --8<-- [start:binary_search]
|
|
// Binary vectors use `hamming` distance over the packed uint8 bytes.
|
|
let query: Arc<dyn Array> = Arc::new(UInt8Array::from(vec![1u8; NUM_BYTES as usize]));
|
|
let mut results = tbl
|
|
.vector_search(query)?
|
|
.distance_type(DistanceType::Hamming)
|
|
.limit(10)
|
|
.execute()
|
|
.await?;
|
|
while let Some(batch) = results.try_next().await? {
|
|
println!("{:?}", batch);
|
|
}
|
|
// --8<-- [end:binary_search]
|
|
Ok(())
|
|
}
|