Filtering
Typed attributes, declared when the namespace is created, and predicates on them, alone or narrowing a vector query.
Attribute types
Attribute fields are declared when the namespace is created, each with a type. Writes are checked against the schema: an undeclared field is UnknownAttr, a wrong type AttrTypeMismatch. null is allowed for any field. Fields cannot be added later.
| Rust | Python |
|---|---|
Bool | AttrType.BOOL |
Number | AttrType.NUMBER |
Text | AttrType.STRING |
TextSet | AttrType.SET_STRING |
NumberSet | AttrType.SET_NUMBER |
schema = itzma.Schema(
dim=2,
metric=itzma.Metric.L2,
attrs={
"tag": itzma.AttrType.STRING,
"year": itzma.AttrType.NUMBER,
"draft": itzma.AttrType.BOOL,
"labels": itzma.AttrType.SET_STRING,
},
)Predicates
A predicate is a typed tree: and, or, not, null and boolean tests, number comparisons, in lists and ranges, text comparisons, and set tests (contains, contains any, contains all, is empty). Python builds them from attr(name) with comparison operators or the matching methods: attr returns an Attr, each condition is an Expr, and &, | and ~ combine Exprs. An Attr can’t be a dict key or set member, since == on it builds a condition. Rust builds a Predicate.
(attr("tag") == "news") & (attr("year") >= 2022)
attr("tag").isin(["news", "blog"]) | attr("tag").starts_with("dev")
~(attr("year") < 2000)
attr("year").between(2020, 2024) # a chained 2020 <= attr("year") <= 2024 raises
attr("tag") == None # is_null()
attr("labels").contains_all(["eu", "tech"])
attr("labels").is_empty() let cheap = Predicate::number("price", Compare::Lt, 20.0);In Python, wrap each comparison in parentheses: & and | bind tighter than == and >=. Leaving them out raises a TypeError that says so.
Missing and null fields
A comparison against a null or missing field is false, and not flips it. So ~(attr("tag") == "x") also matches documents with no tag at all.
Filter without a vector
filter returns matching rows in id order, a page at a time (limit 1 to 10,000, default 100); pass the returned next as after for the next page. It reads no vectors. Paging is not a snapshot.
# filter() pages in id order; pass `next` back as `after`.
page = ns.filter(attr("year") > 0, limit=2)
while True:
print("page:", [row.id for row in page])
if page.next is None:
break
page = ns.filter(attr("year") > 0, limit=2, after=page.next) let page = ns.filter(&cheap, &FilterOptions::default()).await?;
let ids: Vec<&str> = page.rows.iter().map(|r| r.id.as_str()).collect();
assert_eq!(ids, ["a", "c"]);Filtering without a vector scans ids and attributes until a page is full.
Filtered top k
A filter on a vector query is applied after shadowing, so an old copy of a document can’t match in place of its newer one. A segment or tail that runs short of matches is asked again for more, so the answer is the true filtered top top_k, short only when fewer documents match.
# The same conditions narrow a vector query.
hits = ns.query([0, 0], top_k=2, filter=attr("year") >= 2023)
print("nearest from 2023 on:", hits.ids)