Topic 5 of 5 · 5 min read
Extracting filters from natural-language questions
Goal: Write a structured-output schema that turns a question into metadata filters and catch an extraction that over-narrows the search.
Many questions carry filters in plain words, so extract them into structured metadata and apply them to the search instead of hoping the embedding captures "2024", "EU" or "release notes". Vector similarity is poor at hard constraints.
Check yourself · 1 of 3
Why apply "year = 2023" as a metadata filter instead of leaving "2023" in the vector query?
Why it matters
"Show me the 2023 security incidents for the EU region" has a topic (security incidents) and three constraints (year, region, maybe document type). Embeddings treat "2023" as a faint hint. A metadata filter treats it as a rule: only chunks tagged year=2023 are candidates. That is both more accurate and faster, because the candidate set shrinks before ranking.
This only works if your chunks were indexed with the metadata in the first place. Filters on fields that do not exist return nothing.
How to do it
- Define the filterable fields you actually store:
year,product,region,doc_type. - Write a schema with an enum or format per field, and make every field optional and nullable. The model should return null when the question does not say.
- Call the model with structured output (see the references) and parse the result.
- Validate in code. Check values against the real set of values, normalise ("EU", "Europe" -> "eu"), and reject impossible ones.
- Build the filter and search. If the filtered search returns too few results, retry without the weakest filter.
{
"type": "object",
"properties": {
"year": { "type": ["integer", "null"] },
"region": { "enum": ["eu", "us", "apac", null] },
"doc_type": { "enum": ["incident", "policy", "release_notes", null] }
},
"required": ["year", "region", "doc_type"],
"additionalProperties": false
}
Before and after
Weak: the filter is guessed from vague wording.
Q: "any recent outages?"
Extracted: {"year": 2025, "region": "us", "doc_type": "incident"}
Result: 0 chunks
"Recent" turned into a year and a region the user never gave. Fixed: extract only what is stated.
Extracted: {"year": null, "region": null, "doc_type": "incident"}
Sort by date descending instead of filtering by year.
What goes wrong
- Over-narrowing. The model fills in fields it should leave null, and the search returns nothing or only a slice. Instruct it to extract only what is explicit, and monitor the rate of zero-result searches.
- Wrong value formats. "Q3" or "last quarter" has no date until your code resolves it from today's date; pass today's date in the prompt.
- Filter on an unindexed field, which silently matches nothing.
- A hard filter on an uncertain extraction. Treat low-confidence filters as soft: boost, not require.
Examples
A good extraction
Question: "What changed for EU customers in the March 2025 release notes?"
Extracted: {"year": 2025, "region": "eu", "doc_type": "release_notes"}. The topic text, "what changed", still goes to the vector search.
An over-narrowed one
Question: "How do I reset my password?" The extractor returns {"product": "mobile-app"} because the user's earlier message mentioned the phone. The password guide is tagged web, so the filtered search returns 0 results. Fix: only carry a filter the user states for this question, or retry without it.
Key points
- Hard constraints in a question (year, region, product, document type) belong in metadata filters, not in the embedding.
- Filters only work on fields you indexed; a filter on a missing field returns nothing.
- Make every schema field nullable and extract only what the question states, so the model leaves the rest null.
- Validate and normalise extracted values in code, and resolve relative dates against the current date.
- Watch the zero-result rate and fall back by dropping the weakest filter when a filtered search returns too little.
Common mistakes
- Letting the model fill fields the question never mentioned, which over-narrows the search.
- Filtering on fields that were never stored as chunk metadata.
- Passing relative dates such as "last quarter" to the filter without resolving them.
- Never checking the rate of zero-result searches after adding filters.
Check yourself · 2 of 3
A question says nothing about region. What should the extraction schema return for region?
Check yourself · 3 of 3
Search with the filter {"product": "mobile-app", "year": 2025} returns 0 chunks, though the topic exists in the index. What is the best next step?
Practice items, not counted towards your mastery.
Start here
- Precise Zero-Shot Dense Retrieval without Relevance Labels (HyDE) (opens in a new tab)PaperFree
Introduces searching with a generated hypothetical answer instead of the raw question.
- Query Rewriting for Retrieval-Augmented Large Language Models (opens in a new tab)PaperFree
Shows rewriting the query before retrieval as its own trainable step in a RAG pipeline.
- Measuring and Narrowing the Compositionality Gap in Language Models (Self-Ask) (opens in a new tab)PaperFree
Explains why multi-part questions fail and how asking sub-questions closes the gap.
More for this topic
- Structured model outputs (OpenAI API docs) (opens in a new tab)DocsFree
How to force model output to match a JSON schema, which routers and filter extractors depend on.