Many questions carry their own filter: a year, a product version, a department, or a tenant. Without it, the retriever can return a correct-looking chunk from the wrong year or the wrong customer. Self-query extracts those filters from the question itself and applies them as metadata filters on the search, so the ranking only considers documents in scope. Pinecone describes the same idea as filtering first: a metadata filter narrows the candidate pool before ranking, so an out-of-scope document cannot compete for a result slot.
What you need
- Chunks stored with metadata such as date, document type, version or tenant
- A model or parser that can extract filters from a question
- Test questions that mention a date, version or tenant
Step 1: Extract the filter from the question
Read the question and pull out the structured parts: the year, the product version, the document type, or the tenant name. Write the filter down separately from the search text, so a vague phrase does not become a wrong filter.
Step 2: Apply the filter before ranking
Run the vector search with the metadata filter attached, so only matching documents are ranked. A filter that matches nothing should return no results, not fall back to unfiltered search.
Step 3: Check the results stay in scope
Read the top results and confirm every one matches the filter. If a result from the wrong year appears, the filter was not applied or the metadata on that chunk is wrong.
Worked example: synthetic self-query on 4 questions
The table below is a synthetic example. It shows the expected result of extracting filters; no search was run.
| Question | Filter extracted | Results in scope? |
|---|---|---|
| 2024 refund policy | year = 2024 | Yes |
| Pro plan pricing | plan = Pro | Yes |
| Acme tenant setup | tenant = Acme | Yes |
| Refund policy, no year | No filter | All years shown |
Expected result (UNTESTED). the three questions with a filter return only matching documents, and the unfiltered question shows all years. This check was not run live; the table shows the expected result for the synthetic example.
Limits
- A wrongly extracted filter is worse than no filter: it hides the right answer instead of finding it.
- Self-query only works when the metadata exists on the chunks; missing metadata cannot be filtered on.
- A filter that matches nothing should be reported as no results, not silently ignored.
- Keep the filter separate from the search text so the model can still match meaning within the filtered set.
