Docs / User guide
Get started with ITZMA in Rust and Python
Create a namespace, write documents and query them, in Rust or Python.
Add the dependency
In Python, install itzma from PyPI:
pip install itzmaIn Rust, add itzma-core from crates.io. This example uses the in-memory store.
[dependencies]
itzma-core = "0.2.1"
object_store = "0.14"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
serde_json = "1"Create, write, query
Dimension, metric and the type of every attribute are fixed for the life of the namespace. One write call is one atomic batch, and it returns its sequence number. Hits come back closest first: lower distance means closer, for every metric.
import itzma
from itzma import attr
store = itzma.Store("memory://") # or "file:///var/lib/itzma", "s3://bucket/prefix", "gs://bucket/prefix", "az://container/prefix"
schema = itzma.Schema(dim=3, metric=itzma.Metric.COSINE, attrs={"year": itzma.AttrType.NUMBER})
ns = itzma.Namespace.create(store, "articles", schema)
ns.upsert([
{"id": "intro", "vector": [0.9, 0.1, 0.0], "attrs": {"year": 2024}},
{"id": "faq", "vector": [0.0, 1.0, 0.2], "attrs": {"year": 2021}},
])
hits = ns.query([1.0, 0.0, 0.0], top_k=2, filter=attr("year") >= 2022)
print(hits.ids, hits.distances) use std::sync::Arc;
use itzma_core::{AttrSchema, AttrType, Document, Metric, Namespace, Schema};
use object_store::memory::InMemory;
#[tokio::main]
async fn main() -> itzma_core::Result<()> {
let store = Arc::new(InMemory::new());
// Dimension, metric and the type of every attribute are fixed for the life
// of the namespace.
let schema = Schema::new(3, Metric::Cosine)
.with_attrs(AttrSchema::new().field("title", AttrType::Text));
let ns = Namespace::create(store.clone(), "articles", schema).await?;
// One call is one atomic write. It returns the sequence number.
let seq = ns
.upsert(vec![
Document {
id: "intro".into(),
vector: vec![0.9, 0.1, 0.0],
attrs: Some(serde_json::json!({ "title": "Introduction" })),
sparse: None,
},
Document {
id: "faq".into(),
vector: vec![0.0, 1.0, 0.2],
attrs: None,
sparse: None,
},
])
.await?;
assert_eq!(seq, 1);
// Closest first. Lower distance means closer, for every metric.
let hits = ns.query(&[1.0, 0.0, 0.0], 2).await?;
assert_eq!(hits[0].id, "intro");
ns.delete(vec!["faq".into()]).await?;
// Another handle, possibly in another process, sees the same data.
let other = Namespace::open(store, "articles").await?;
assert_eq!(other.query(&[0.0, 1.0, 0.0], 5).await?.len(), 1);
Ok(())
}Open from another process
A handle holds no lock. Any number of handles, in one process or many, can work on one namespace, and a query that starts after a write returned sees it.
Writes also take NumPy arrays (ids=, vectors=, attrs=) and Arrow, Polars or DuckDB tables. The Python API page covers them.
Next steps
- Concepts: what a namespace, segment and manifest are.
- Filtering: narrowing a query by attributes.
- Maintenance: keeping a namespace compacted and indexed.
