Project tags in embeddings
Your search returns the right kind of passage from the wrong document, and your pages already say which document is which: a title, a product, a company, a contract. This procedure puts that metadata in front of each chunk before it is embedded, with no model call. It takes minutes, and it is the first layer to try. The troubleshooting procedure tells you when this is the layer that you need.
What do you need?
- Markdown pages or PDF files, and the
duckdb-vectorspackage. - The file of failing questions of the troubleshooting procedure. You need it for the check at the end, and for the
tagsreport. - The definitions of tag, prefix and sidematter are in the tagged retrieval workbook. This page does not repeat them.
Step 1: declare the tags
A tag is a label: $.path pair, where the path is a JSONPath expression evaluated over the metadata of the page. Declare as many pairs as you need, and index:
$ duckdb-vectors index ./pages --recursive --database knowledge.duckdb \
--tag-field "product: $.product.name" \
--tag-field "audience: $.audience[*]" \
--tag-bm25The same list, from the environment and from Python:
$ export DUCKDB_VECTORS_TAG_FIELDS="product: $.product.name; audience: $.audience[*]"
$ export DUCKDB_VECTORS_TAG_BM25=truefrom duckdb_vectors import VectorStore, index_folder
from duckdb_vectors.config import Settings
settings = Settings(
db_path="knowledge.duckdb",
tag_fields=["product: $.product.name", "audience: $.audience[*]"],
tag_bm25=True,
)
store = VectorStore(settings=settings)
index_folder(store, "pages", recursive=True)
store.close()The order of the list is the order of the lines in the prefix. A tag whose path finds nothing in a page is left out of the prefix of that page. When you index again after a change of tags or of a value, only the chunks whose prefix changed are embedded again.
Without --tag-bm25, the tags change the embedding only. With it, a question that names a product also finds chunks whose text never says it. We recommend --tag-bm25: in our runs the keyword index carried a large share of the gain, as the performance workbook shows. Note that a search follows whatever index the file holds. If you index again with the other setting, then the keyword index is rebuilt, even when no page changed.
The workbook lists the JSONPath expressions that work. The most useful ones are $.title, $.product.name, and $.audience[*] for a list.
Where do the values come from?
There are four sources, from the most to the least effort. Try them in this order.
Front matter that people write. The values are right and they use the words of your searchers. Use them first. Where they are sparse, ask the authors to fill them in for the few pages that matter.
Metadata that every page has. The chunkers add three reserved keys to every chunk, with no work on your side: document.title, document.path and section.headings. The last one is the chain of headings of the section, joined with >. These keys are available from release v261129. They are the answer when your pages have no front matter at all, and they help with the second symptom of the troubleshooting procedure, the right document and the wrong section:
$ duckdb-vectors index ./pages --recursive --database knowledge.duckdb \
--tag-field "title: $.document.title" \
--tag-field "where: $.section.headings" \
--tag-bm25Sidematter files written by a script. If the value lives outside the page, then write it in a sidematter file next to the page. The folder of the page, the system that exported it, the name of the conversion that produced it are examples. The sidematter file is a YAML file named after the page with .sidematter.yaml added, and the indexer merges it in the metadata of the page:
# cedar/deploy-runbook.md.sidematter.yaml
product: Cedar
exported_from: confluenceA few lines of shell or Python are enough to generate one file per page. The merge rules and the collisions are described once, in the workbook.
The document information of a PDF file. A PDF file has no front matter, but it exposes its title, author, subject and keywords when the file declares them. The paths $.title and $.author work on PDF and Markdown files alike. Check them before you rely on them: many PDF files carry the name of a template, or nothing.
Which values deserve a tag?
Ask one question for each candidate: do people name this value in their questions?
If the answer is 'Yes', as for a company, a product, a year or a contract, then the tag restores what chunking removed, in the words of the searcher. If the answer is 'No', as for a repository, a directory or a file extension, then the tag adds noise. In our runs, the repository, the directory and the extension of the code set gained nothing, and cost one embedder 0.095 of Pass@20 in vector search. The company, the document type and the period of SEC filings gained 0.105 and 0.127 for two embedders. Both results are in the performance workbook.
Three more rules, from the same runs:
- A tag that every page shares separates nothing. Drop it.
- A tag with a different value on every page does no harm by itself. A title can match words of a question, while an opaque code such as
REF-001cannot. - Write the same thing in the same way.
Enterprise,enterprise planandEnt.are three values, and they split a group that should be one. The inspection procedure lists the values of each tag.
How to read the tags report?
The tags verb computes the figures of every declared tag over the whole index, and gives a verdict. Pass your questions, because the verdict relies on them:
$ duckdb-vectors tags --database knowledge.duckdb \
--tag-field "product: $.product.name" \
--tag-field "kind: $.kind" \
--queries failing-questions.txt| Field | What it tells you |
|---|---|
coverage | Share of documents that carry the tag. Low coverage means most chunks get no help from it. |
cardinality | Number of distinct values. |
entropy | How evenly the values are spread, from 0 (one value everywhere) to 1 (every value equally common). |
documents_per_value | How many documents share a value. About one adds a note: a title helps if queries use titles, an opaque code does not. |
query_mention | With --queries: share of the queries that contain at least one value of the tag as whole words. |
verdict, reasons | effective or decorative, and why. |
margin_*, d_*, value_* | How much the tag raises the similarity of chunks of one document or of chunks that share its value, with and without the tag. Reported, not used for the verdict. |
A tag is decorative when its coverage is below 50%, when it has a single value, or, with queries, when fewer than 20% of the queries contain one of its values. Every other tag is effective. Without --queries, only sparse and constant tags are screened out.
The query-mention threshold is provisional. It was fitted on 17 tags from validation sets that later proved unable to show what tags restore, and the later runs did not re-test it. Use the verdict as a hint, and not as a decision. The report also judges page tags only: a section context differs on every section, so it counts one value per page and is always called decorative. Ignore the verdict for contexts.
Its warnings list says when prefix and chunk probably exceed the input window of the model, estimated at four characters per token. Every fastembed model that we measured cuts at 512 tokens, so a long prefix pushes the end of the chunk out of the window. Keep the prefix to the few tags that matter.
Step 2: check the prefix
The tags column of each chunk stores the prefix as it was embedded. Read a few, and find the longest:
SELECT chunk_id, tags, length(tags) AS prefix_characters
FROM chunks
ORDER BY prefix_characters DESC
LIMIT 5;Then replay your failing questions with duckdb-vectors search, and compare the rank of the expected chunk with the ranks that you noted before. Five questions show a problem, but they do not measure a gain. For a measure, go to the evaluation procedure. Keep a tag on evidence, and drop it otherwise.
What gain can you expect?
Two measurements, both with no model call at indexing time. Pass@20 is the share of questions whose right passage is in the top 20. The figures below are for the embedder MiniLM-384, hybrid search, on the test split of each corpus. A star marks a gain that survives the multiple-test correction of the evaluation. Every number links to its source: the performance workbook for the table, and the reproduction procedure for the commands.
| Corpus (test questions) | Baseline Pass@20 | Title | Headings |
|---|---|---|---|
| Engineering pages, private (180) | 0.655 | +0.061 * | +0.071 * |
| Contracts and policies, LegalBench-RAG (374) | 0.616 | +0.100 * | +0.116 * |
| SEC filings, FinanceBench (110) | 0.262 | +0.041 | +0.045 |
| Code, Anthropic cookbook (162) | 0.768 | +0.043 | +0.032 |
Native page tags written by people did better when they named what the questions ask for. On contracts, the collection and the document name gained +0.055 to +0.141. On SEC filings, the company, the type, the period and the sector gained +0.105 and +0.127 for two of the three embedders, and nothing for the default static model.
Title and headings are positive for every embedder on every corpus, and none of the 96 intervals lies below zero. On the two small sets, the intervals are too wide for most gains to count. At the end of the day, no tag set is good enough to become a default, and tag_fields stays empty until you set it. We recommend title and headings as a cheap first try when a corpus has no other metadata, and the metadata that people name in their questions as the better one.
Embedding the texts takes at most 12% longer with the default static model for these tag sets. A sentence written by a model takes 17% to 21% longer, as the next layer shows.
Next steps
- Evaluate retrieval on your own queries: the next layer, to keep a tag on evidence.
- Generate contextual tags: where the metadata is thin.
- Tagged retrieval workbook: tags, prefix, JSONPath, sidematter.
- Free tags experiment: the record of the measure of title, headings and path.
- Performance workbook: the measured results.