Search⌘ K
AI Features

Query Top-k and Use Metadata Filters

Explore how to enhance retrieval quality in AI support workflows by mastering top-k ranking and applying metadata filters. Learn to balance relevance and noise, verify results beyond scores, and implement reliable validation to support grounded, policy-based responses.

A retrieval step has one job. Given a support question, it searches the vector index and returns a ranked list of policy chunks that might contain the answer. The workflow then decides whether any returned chunk is good enough to support a grounded reply.

A typical call looks like this as a non-executable reference using a common Chroma pattern:

results = collection.query(
query_texts=[question],
n_results=5, # top-k
where={"policy_type":"refunds"} # optional metadata filter
)
# results contains ids, documents (chunk text), metadatas, distances or scores
A non-executable reference call: top-k plus an optional metadata filter

The observable output is not just text. Each result includes a id that points back to a stored chunk, a similarity score or distance used for ranking, a text snippet, and metadata such as policy_type, doc_name, or section.

Similarity ranking is probabilistic. A higher-ranked chunk is often more relevant, but scores are not stable across models or vendors and do not prove correctness. The only reliable check is whether the returned chunk text actually contains the sentence we need to cite in the draft reply.

Use the example retrieval response to pick the best chunk based on its snippet and metadata, not the score alone:

Query, embed, search, and top-k retrieval, the four stages that turn a support question into a ranked list of candidate chunks
Query, embed, search, and top-k retrieval, the four stages that turn a support question into a ranked list of candidate chunks

That pipeline produced the ranked response below for the question "What is the company policy on remote work?" Read the snippet and metadata for each rank before trusting the score.

Retrieval results for "What is the company policy on remote work?"
Rankid / scoretext (chunk snippet)metadatanotes

1

id: doc_1024, score: 0.862

Employees may work remotely up to 3 days per week with manager approval. Roles must align with remote work eligibility and business needs...

doc_name: Remote Work Policy.pdf, policy_type: Work Arrangement, section: 3.2 Eligibility & Limits, updated_at: 2024-04-12

BEST ANSWER: directly states the remote work allowance and approval requirement.

2

id: doc_0871, score: 0.798

Hybrid work is encouraged to balance flexibility and collaboration. Teams should coordinate in-office days to support alignment...

doc_name: Hybrid Work Guidelines.pdf, policy_type: Work Arrangement, section: 2.1 Overview, updated_at: 2024-02-20

3

id: doc_1199, score: 0.732

Employees must ensure a safe and ergonomic home workspace and comply with all security requirements when working remotely...

doc_name: Information Security Policy.pdf, policy_type: Security, section: 4.3 Remote Work, updated_at: 2024-01-15

4

id: doc_0632, score: 0.681

Department leaders may approve alternative arrangements based on business needs and performance...

doc_name: Manager Handbook.pdf, policy_type: People Management, section: 5.6 Flexible Arrangements, updated_at: 2023-11-30

5

id: doc_0447, score: 0.624

The company provides equipment for remote work where required by the role and approved by IT...

doc_name: IT Workplace Services Policy.pdf, policy_type: IT Services, section: 3.1 Equipment, updated_at: 2023-10-05

Do you find this helpful?

The key habit is simple. Treat scores as a sorting hint, then verify by reading the chunk and confirming it mentions the policy condition the support answer must rely on.

Top-k as a relevance vs. noise trade

Once we can interpret a single retrieval response, the next control knob is how many results we ask for. Top-k is the number of chunks returned, often set with n_results ...