Anvia
Retrieval

Metadata Filters

Filter vector search results by document metadata.

Metadata filters constrain retrieval before results are returned. Use them for tenant boundaries, product scopes, document status, and other application rules.

1. Store Metadata During Embedding

const embedded = await embedDocuments(embeddings, documents, {
  id: (doc) => doc.id,
  content: (doc) => doc.body,
  metadata: (doc) => ({
    tenantId: doc.tenantId,
    product: doc.product,
    rank: doc.rank,
  }),
});

Metadata values can be strings, numbers, booleans, or null.

import { vectorFilter } from "@anvia/core";

const results = await index.search({
  query: "billing limits",
  topK: 5,
  filter: vectorFilter.eq("tenantId", "acme"),
});

3. Combine Filters

const filter = vectorFilter.and(
  vectorFilter.eq("tenantId", "acme"),
  vectorFilter.eq("product", "billing"),
);

const results = await index.search({
  query: "invoice settings",
  topK: 5,
  filter,
});

Use and(...) and or(...) to build larger filters.

4. Use Range Filters

const results = await index.search({
  query: "priority support",
  topK: 5,
  filter: vectorFilter.gt("rank", 2),
});

gt(...) and lt(...) compare numbers, strings, and booleans.

5. Apply Filters to Dynamic Context

const agent = new AgentBuilder("support", model)
  .dynamicContext(index, {
    topK: 3,
    filter: vectorFilter.eq("tenantId", tenantId),
  })
  .build();

Keep security-sensitive filters in application code. Do not ask the model to enforce tenant or permission boundaries.