Expand description
Rust bindings to zvec, Alibaba’s lightweight in-process vector database.
§At a glance
- Raw FFI in
sys— every zvec C API symbol, generated bybindgenfrom a pinnedc_api.h. - Safe, idiomatic wrappers at the crate root covering the full
public C API:
CollectionSchema/FieldSchema,IndexParams(and builder sugar viaFieldSchemaBuilder),VectorQuery/GroupByVectorQuery,Collection(create/open/flush/optimize + DDL + DML + DQL),Doc/DocRef, andCollectionStats. - Batteries-included retrieval helpers:
HybridSearchfor fused multi-query search, andrerank::RrfReRanker/rerank::WeightedReRankerfor standalone result fusion. - Feature-gated niceties: async, JSON ingest, half-precision
vectors, a derive macro, and a zero-setup
bundledinstall path.
§Optional cargo features
| Feature | Adds |
|---|---|
bundled | Fetches upstream’s PyPI wheel at build time — no external zvec setup needed. |
derive | #[derive(IntoDoc)] — struct-to-Doc conversion (see IntoDoc). |
tokio | AsyncCollection — every op wrapped in tokio::task::spawn_blocking. |
serde-json | Doc::from_json — build a Doc from a serde_json::Value + schema. |
half | Doc::add_vector_fp16, DocRef::get_vector_fp16, VectorQuery::set_query_vector_fp16 taking &[half::f16]. |
pkg-config | Locate libzvec_c_api via pkg-config (in addition to env-var discovery). |
§Install paths
build.rs locates a prebuilt libzvec_c_api, in order:
ZVEC_LIB_DIR— explicit directory.ZVEC_ROOT— install prefix; uses$ZVEC_ROOT/lib(+lib64).--features bundled— download + extract the PyPI wheel.pkg-config(if thepkg-configfeature is on).- System linker defaults.
Set ZVEC_STATIC=1 for static linking. ZVEC_INCLUDE_DIR /
ZVEC_ROOT redirect bindgen at an installed header instead of the
one vendored here.
§Quickstart
use zvec::{Collection, CollectionSchema, Doc, FieldSchema, MetricType, VectorQuery};
// Builder sugar for schemas:
let schema = CollectionSchema::builder("docs")
.field(FieldSchema::string("id").invert_index(true, false))
.field(
FieldSchema::vector_fp32("embedding", 3)
.hnsw(16, 200)
.metric(MetricType::Cosine),
)
.build()?;
let collection = Collection::create_and_open("./my_coll", &schema, None)?;
let mut doc = Doc::new()?;
doc.set_pk("doc1")?;
doc.add_string("id", "doc1")?;
doc.add_vector_fp32("embedding", &[0.1, 0.2, 0.3])?;
collection.insert(&[&doc])?;
collection.flush()?;
let q = VectorQuery::builder()
.field("embedding")
.vector_fp32(&[0.1, 0.2, 0.3])
.topk(10)
.build()?;
for row in collection.query(&q)?.iter() {
println!("{:?} score={}", row.pk_copy(), row.score());
}More end-to-end recipes live under examples/:
semantic_search, hybrid_search, json_ingest, and the Rust port of
basic_example.c.
§Thread safety
CollectionisSend + Sync. Sharing one across threads is justArc<Collection>; no dedicatedSharedCollectiontype is needed.- Pure builders / config types (
CollectionSchema,FieldSchema,IndexParams,HnswQueryParams,CollectionOptions, …) areSend + Sync. - Types with mutable C-side state and no documented thread-safe reads
(
Doc,VectorQuery,GroupByVectorQuery,DocSet) areSendonly.
use std::sync::Arc;
use std::thread;
let collection = Arc::new(Collection::create_and_open("./coll", &schema, None)?);
let handles: Vec<_> = (0..4)
.map(|_| {
let c = Arc::clone(&collection);
thread::spawn(move || c.flush())
})
.collect();
for h in handles { let _ = h.join(); }§Where to go next
Collection— lifecycle, DDL, DML, DQL.HybridSearch— fused multi-query search.rerank— reciprocal-rank and weighted fusion over arbitraryVec<Hit>inputs.IntoDoc(featurederive) —#[derive(IntoDoc)]to skip manualadd_*calls.AsyncCollection(featuretokio) — async wrapper for tokio users.Doc::from_json(featureserde-json) — JSON →Docbridge.
Modules§
Structs§
- Async
Collection tokio - Tokio-friendly handle to a
Collection. Cheap to clone. - Collection
- Collection
Options - Collection
Schema - Collection
Schema Builder - Builder for
CollectionSchema. Accumulates pending fields and options; surfaces any validation errors atSelf::build. - Collection
Stats - Config
- Global zvec runtime configuration, passed to
initialize. - Doc
- Owning document.
- DocRef
- Non-owning document reference (e.g. rows returned from a query).
- DocSet
- Result set returned by
Collection::queryorCollection::fetch. - Field
Schema - Field
Schema Builder - Builder for
FieldSchema. Constructed via the type-specific associated functions (FieldSchema::string,::vector_fp32, …). - Field
Schema Ref - Non-owning borrow of a field schema returned by schema accessors.
- Flat
Query Params - Group
ByVector Query - Hnsw
Query Params - Hybrid
Search - Builder that runs a set of
VectorQuerys and fuses their results. - Index
Params - IvfQuery
Params - LogConfig
- Logging sink + level configuration.
- Vector
Query - Vector
Query Builder - Builder for
VectorQuery. Covers every knob on the query exceptset_query_params(the untyped escape hatch); HNSW / IVF / Flat parameters still go through their respective builders and.params(...). - Write
Result - Per-document status returned by the
*_with_resultsbatch write APIs. - Write
Summary - Result summary returned by batch write APIs.
- Zvec
Error - Error returned by fallible safe wrappers over the zvec C API.
Enums§
- Data
Type - Scalar, vector, and array element types recognised by zvec.
- DocOperator
- Document operator semantics carried on a
crate::doc::Doc. - Error
Code - Strongly-typed mirror of
sys::zvec_error_code_t. - Index
Type - Index algorithm families.
- LogLevel
- Log level used by
crate::config::LogConfig. - LogType
- Log sink type.
- Metric
Type - Distance metric used by vector indexes.
- Quantize
Type - Optional post-quantization applied to vectors before indexing.
Traits§
Functions§
- check_
version - Returns true if the linked zvec library is at least
major.minor.patch. - clear_
last_ error - Clear the current thread-local last-error slot.
- initialize
- Initialize the zvec library.
- is_
initialized - Returns whether
initializehas been called successfully. - shutdown
- Tear down any process-global state created by
initialize. - version
- Returns the full version string reported by the linked zvec library.
- version_
major - version_
minor - version_
patch
Type Aliases§
- Result
- Convenience alias for
Result<T, ZvecError>used throughout the crate.