New Tool Radar

AI Concepts

How to Filter RAG Search Results by Document Metadata

Filter RAG search results by document metadata, such as year or department, before ranking them by similarity.

Schematic illustration of filter rag search results by document metadata: step one tag the documents, step two add the filter, step three compare the counts.

Similarity alone can return a good match from the wrong year or the wrong department. Metadata filtering fixes that: store facts about each document, such as its year, author or department, alongside its vector, and filter on those facts before or with the similarity search. The model then answers from the right set of documents, not just the most similar words.

Prerequisites

  • Documents with known metadata: year, department, document type or similar.
  • A vector store that supports metadata filters.
  • A question that only documents from one metadata value can answer.

Steps

Step 1: Tag the documents

When you embed each document, store its metadata with the vector: the year, the department, the document type. Keep the metadata values consistent, using the same spelling everywhere.

Step 2: Add the filter

In the search call, add a filter for the metadata value the question needs, such as year 2025, alongside the similarity query. The filter runs before ranking, so irrelevant years never reach the top results.

Step 3: Compare the counts

Run the same question with and without the filter and count the results. The filtered search should return fewer, more relevant chunks; if it returns none, the metadata values are wrong, not the question.

Worked example: Northgate Clinic policy search

The fictional Northgate Clinic has 40 policy documents. A question about the 2025 appointment policy should only search the 12 documents from 2025.

Search Documents searched Top chunk year Relevant
No filter 40 2023 No: old policy
Filter year 2025 12 2025 Yes: current policy

Without a filter the top chunk is a 2023 policy; with the year filter the search covers 12 documents and the top chunk is the 2025 policy.

Expected result. The filtered search returns chunks only from the metadata value the question needs, and the top chunk is relevant. UNTESTED: this guide was not run against a live model or API. The worked example below is fictional practice material, and its result is the expected result, not a measured one.

Limits

  • Metadata is only as good as your tagging; a wrong year on a document breaks the filter silently.
  • A filter that is too narrow returns nothing; check the counts before trusting an empty result.
  • This guide does not cover building the metadata itself.

Sources

See all