Vector queries and reranking
Closest first, exact or through an index, with rerank, nprobe and filters.
Nearest neighbors
A query returns up to top_k hits, closest first, each with its id, distance and attributes.
hits = ns.query([0.9, 0.1, 0, 0], top_k=5, filter=attr("year") >= 2022)
for hit in hits:
print(hit.id, round(hit.distance, 4), hit.attrs) let hits = ns.query(&[2.0, 0.1], 2).await?;
assert_eq!(hits[0].id, "east");
assert_eq!(hits[0].attrs, Some(serde_json::json!({ "label": "east" })));
assert!(hits[0].distance < hits[1].distance);Options
Python takes them as keywords on query; Rust passes QueryOptions to query_with.
| Option | Default | What it does |
|---|---|---|
rerank | 4 | An index proposes rerank * top_k candidates, which are then rescored against their exact vectors. None proposes every row. |
nprobe | None (index picks) | How many SQ8 partitions to read, or under HNSW a multiplier on how wide the graph search is. |
exact | false | Skip indexes and scan every live document. |
filter | None | A predicate the hits must match. See Filtering. |
let exact = QueryOptions::default().exact(true);
assert_eq!(ns.query(&[0.0], 1).await?, ns.query_with(&[0.0], 1, &exact).await?);How does a query run?
Each segment with an index proposes candidates through it; segments without one and the WAL tail are scanned. A candidate that a newer segment or the tail overwrote or deleted is dropped, the survivors are reranked on exact vectors, and the closest top_k across all sources win. The answer never carries an approximate distance.
A query that starts after a write returned sees it, from any handle or process.
Distances
L2 is squared Euclidean, cosine is 1 - cos, dot product is negated, so lower is closer for all three. Distances are comparable only within one query.
