Guides Markdown

Hybrid search with metadata filters

Vector search alone, narrowed by text and metadata filters, or fused with BM25: a five-document example with fixed embeddings and the exact order each query returns.

Give documents a vector (an embedding) and SkimmerDB finds the nearest ones to a query vector. Make the embeddings yourself, as on this page, or let SkimmerDB make them from the text with your own OpenAI, Voyage, Gemini or Cohere key, or hosted: see Embeddings. A text match and metadata filters can narrow the candidates first (hybrid search): the answer is the nearest among the documents that pass. One copy of the data serves text, vectors and SQL, and vectors are as durable and as quick to become searchable as text.

Create a vector namespace

A namespace's vector settings are fixed for its life: dims (1 to 2,048, your embeddings' length) and metric, cosine (the default; vectors are stored normalised) or euclidean.

B=https://api.skimmerdb.com/v1
curl -s $B/namespaces -H "Authorization: Bearer $KEY" \
  -d '{"name": "notes", "vector": {"dims": 768, "metric": "cosine"}}'
notes = db.create_namespace("notes", dims=768, exist_ok=True)
const notes = await db.createNamespace("notes", { dims: 768, existOk: true });

You can also skip this step: the first write of documents with vectors to a new name makes a namespace with vectors of the first vector's length and the cosine metric.

Note One embedding model per namespace. Switching models means a new namespace, written alongside the old one until it has caught up.

Write vectors

Put "vector": [...] in each document, or "vector_b64": base64 of little-endian float32s, smaller than JSON numbers. text may then be left out; a vector is optional per document.

curl -s $B/namespaces/notes/docs -H "Authorization: Bearer $KEY" -d '{"docs": [
  {"id": "n-17", "text": "Webhook timeouts on the api team", "meta": {"team": "api"}, "vector": [0.12, -0.03, ...]}
]}'
notes.write([{"id": "n-17", "text": "Webhook timeouts on the api team", "meta": {"team": "api"}, "vector": embedding}])
await notes.write([{ id: "n-17", text: "Webhook timeouts on the api team", meta: { team: "api" }, vector: embedding }]);
  • A vector of the wrong length, or a vector written to a namespace without vectors, is a 422.
  • GET .../docs/{id} returns the vector. A PATCH keeps it unless it gives a new one.

Search by vector

POST /v1/namespaces/{name}/search with "vector" (or "vector_b64"):

curl -s $B/namespaces/notes/search -H "Authorization: Bearer $KEY" \
  -d '{"vector": [0.12, -0.03, ...], "limit": 10}'
res = notes.search(vector=query_embedding, limit=10)
for hit in res.hits:
    print(hit.id, hit.score)
const res = await notes.search("", { vector: queryEmbedding, limit: 10 });
for (const hit of res.hits) console.log(hit.id, hit.score);

The answer:

Answer
{"namespace": "notes", "hits": [{"id": "n-17", "score": 0.912, "meta": {"team": "api"}, "written_at": "..."}],
 "docs": 1344643, "took_ms": 0.73, "scanned": 15232, "reranked": 64, "partial": false,
 "temperature": "warm", "store_gets": 0, "store_bytes": 0}
  • Hits come nearest first. score is the cosine similarity (cosine namespaces) or the euclidean distance (euclidean ones). Ties are broken by id.
  • limit is 10 by default, at most 1,000.
  • scanned and reranked say how many candidates were looked at and reranked.
  • Answers carry cpu_ms and timed_out as text searches do, and the same time limits.

Hybrid: vectors with text and filters

Add q (with regex if you like) and filter to a vector query. Both narrow the candidates before ranking: the answer is the nearest among the documents that contain the text and pass the filter.

curl -s $B/namespaces/notes/search -H "Authorization: Bearer $KEY" \
  -d '{"vector": [0.12, -0.03, ...], "q": "timeout", "filter": {"team": "api"}, "limit": 10}'
res = notes.search("timeout", vector=query_embedding, filter={"team": "api"}, limit=10)
const res = await notes.search("timeout", { vector: queryEmbedding, filter: { team: "api" }, limit: 10 });

This is the combination agents usually want: meaning from the vector, exactness from the text (a name, an error code, an id), and scope from the filter (a user, a team, a time window).

  • A hybrid query takes the text's candidates from the index, without reading documents, and ranks them; only the nearest are checked for the text.
  • A text that most documents hold is checked on the nearest vectors instead.
  • A top-limit query returns limit hits whenever that many documents qualify; otherwise the answer says "partial": true.

Ranked hybrid: BM25 and vectors

With "rank": "bm25", q stops being a filter and becomes a second ranking: the documents nearest the vector and the documents best for q's words (BM25) are fused by reciprocal rank, each document scoring 1 / (60 + its place) in each list it's in. A document high in both comes first; one that only one ranking finds can still make the top.

curl -s $B/namespaces/notes/search -H "Authorization: Bearer $KEY" \
  -d '{"vector": [0.12, -0.03, ...], "q": "timeout retry", "rank": "bm25", "filter": {"team": "api"}, "limit": 10}'
res = notes.search("timeout retry", vector=query_embedding, rank="bm25", filter={"team": "api"}, limit=10)
const res = await notes.search("timeout retry", { vector: queryEmbedding, rank: "bm25", filter: { team: "api" }, limit: 10 });
  • Each hit has the fused score, and vector_rank and text_rank (its place in each list, 1 first; 0 when it isn't in one).
  • Each list is max(4 x limit, 40) deep, under the same filter. Fused answers have no next.
  • Works with semantic as well as vector. It's one query for metering.

Accuracy

The defaults give recall@10 of about 0.97 on 1.3M 768-dimension vectors, in under a millisecond warm. Three knobs trade speed for accuracy:

OptionDefaultWhat it does
nprobe48Clusters probed in the biggest index (smaller indexes are scaled to it). More: better recall, slower.
rerank64Candidates reranked with fuller-precision vectors.
fullfalsetrue reranks with the vectors as written (float32) rather than their int8 copies.
res = notes.search(vector=query_embedding, limit=10, nprobe=96, rerank=128)
const res = await notes.search("", { vector: queryEmbedding, limit: 10, nprobe: 96, rerank: 128 });

How it works

  • Recent writes are searched exactly, by brute force.
  • Compaction builds an IVF index per segment: k-means centroids over RaBitQ 1-bit codes, with int8 rows for reranking. The float32 vectors as written are kept in object storage for full reranks.
  • Cold queries read the namespace's manifest and a small map of its indexes side by side, then the clusters a query probes (with their documents' ids and metadata) and the newest writes side by side: two rounds to object storage, without downloading the rest of the namespace. A cold query with a filter or text opens the namespace instead.

Worked example: five tickets, four queries

Hand-made 4-number vectors, so the order of every answer can be checked with a calculator. The query vector [1, 0, 0, 0] stands for "webhook problems". Run it with an admin key (KEY) on an empty account; the comments are what it printed.

B=https://api.skimmerdb.com/v1
H=(-H "Authorization: Bearer $KEY" -H "Content-Type: application/json")

curl -fsS "$B/namespaces" "${H[@]}" -d '{"name":"tickets","vector":{"dims":4,"metric":"cosine"}}' >/dev/null
curl -fsS "$B/namespaces/tickets/docs" "${H[@]}" -d '{"wait":true,"docs":[
  {"id":"t1","text":"Webhook timeout after 30 seconds","meta":{"team":"api"},"vector":[0.9,0.1,0,0]},
  {"id":"t2","text":"Webhook retries exhausted with E504","meta":{"team":"api"},"vector":[0.8,0.3,0.1,0]},
  {"id":"t3","text":"Payment webhook timeout on renewal","meta":{"team":"billing"},"vector":[0.95,0,0,0.2]},
  {"id":"t4","text":"Login page slow to load","meta":{"team":"web"},"vector":[0.1,0.9,0.2,0]},
  {"id":"t5","text":"Avatar upload timeout","meta":{"team":"web"},"vector":[0.3,0.3,0.9,0]}]}' | jq -c '{written}'
# {"written":5}

Q='"vector":[1,0,0,0],"limit":3'
# 1. nearest only
curl -fsS "$B/namespaces/tickets/search" "${H[@]}" -d "{$Q}" | jq -c '[.hits[] | [.id, (.score*1000|round/1000)]]'
# [["t1",0.994],["t3",0.979],["t2",0.93]]
# 2. nearest within one team
curl -fsS "$B/namespaces/tickets/search" "${H[@]}" -d "{$Q,\"filter\":{\"team\":\"api\"}}" | jq -c '[.hits[].id]'
# ["t1","t2"]
# 3. nearest among documents containing "timeout"
curl -fsS "$B/namespaces/tickets/search" "${H[@]}" -d "{$Q,\"q\":\"timeout\"}" | jq -c '[.hits[].id]'
# ["t1","t3","t5"]
# 4. vector and BM25 rankings fused
curl -fsS "$B/namespaces/tickets/search" "${H[@]}" \
  -d '{"vector":[1,0,0,0],"q":"retries","rank":"bm25","limit":3}' | jq -c '[.hits[] | [.id, .vector_rank, .text_rank]]'
# [["t2",3,1],["t1",1,0],["t3",2,0]]
from skimmerdb import SkimmerDB

db = SkimmerDB(KEY)
tickets = db.create_namespace("tickets", dims=4, exist_ok=True)
tickets.write([
    {"id": "t1", "text": "Webhook timeout after 30 seconds", "meta": {"team": "api"}, "vector": [0.9, 0.1, 0, 0]},
    {"id": "t2", "text": "Webhook retries exhausted with E504", "meta": {"team": "api"}, "vector": [0.8, 0.3, 0.1, 0]},
    {"id": "t3", "text": "Payment webhook timeout on renewal", "meta": {"team": "billing"}, "vector": [0.95, 0, 0, 0.2]},
    {"id": "t4", "text": "Login page slow to load", "meta": {"team": "web"}, "vector": [0.1, 0.9, 0.2, 0]},
    {"id": "t5", "text": "Avatar upload timeout", "meta": {"team": "web"}, "vector": [0.3, 0.3, 0.9, 0]},
], wait=True)

q = [1, 0, 0, 0]
print([h.id for h in tickets.search(vector=q, limit=3).hits])                         # ['t1', 't3', 't2']
print([h.id for h in tickets.search(vector=q, filter={"team": "api"}, limit=3).hits])  # ['t1', 't2']
print([h.id for h in tickets.search("timeout", vector=q, limit=3).hits])               # ['t1', 't3', 't5']
print([h.id for h in tickets.search("retries", vector=q, rank="bm25", limit=3).hits])  # ['t2', 't1', 't3']
import { SkimmerDB } from "skimmerdb";

const db = new SkimmerDB(KEY);
const tickets = await db.createNamespace("tickets", { dims: 4, existOk: true });
await tickets.write([
  { id: "t1", text: "Webhook timeout after 30 seconds", meta: { team: "api" }, vector: [0.9, 0.1, 0, 0] },
  { id: "t2", text: "Webhook retries exhausted with E504", meta: { team: "api" }, vector: [0.8, 0.3, 0.1, 0] },
  { id: "t3", text: "Payment webhook timeout on renewal", meta: { team: "billing" }, vector: [0.95, 0, 0, 0.2] },
  { id: "t4", text: "Login page slow to load", meta: { team: "web" }, vector: [0.1, 0.9, 0.2, 0] },
  { id: "t5", text: "Avatar upload timeout", meta: { team: "web" }, vector: [0.3, 0.3, 0.9, 0] },
], { wait: true });

const q = [1, 0, 0, 0];
const ids = (r) => r.hits.map((h) => h.id);
console.log(ids(await tickets.search("", { vector: q, limit: 3 })));                          // ['t1', 't3', 't2']
console.log(ids(await tickets.search("", { vector: q, filter: { team: "api" }, limit: 3 }))); // ['t1', 't2']
console.log(ids(await tickets.search("timeout", { vector: q, limit: 3 })));                   // ['t1', 't3', 't5']
console.log(ids(await tickets.search("retries", { vector: q, rank: "bm25", limit: 3 })));    // ['t2', 't1', 't3']

What each answer shows:

QueryCandidatesAnswerWhy
Vector onlyall 5t1, t3, t2Cosine with [1,0,0,0]: 0.994, 0.979, 0.930 (t5 0.302, t4 0.108)
+ filter: {team: "api"}t1, t2t1, t2Only two documents pass; limit 3 returns two, and that answer is complete
+ q: "timeout"t1, t3, t5t1, t3, t5The text is a filter here: t2 is closer than t5 but doesn't contain "timeout"
+ q: "retries", rank: "bm25"vector list ∪ BM25 listt2, t1, t3t2 is 3rd by vector and 1st by text: 1/63 + 1/61 beats t1's 1/61 alone

So: a plain q with a vector narrows (only documents containing it), and rank: "bm25" blends (a document can win on either ranking). Use the filter for scope (user, tenant, team, dates), the plain q for something that must appear (an error code, an id), and BM25 fusion when the words are a hint rather than a requirement.

Next