Skip to content

Troubleshoot a search that returns the wrong passages ​

A search returns a passage that looks right and is wrong: the rollback procedure of another product, the revenue of another year, the clause of another contract. The words match, the document does not. This procedure helps you to tell this symptom from the others, and to pick the cheapest fix. It is the entry point of the pages that improve retrieval, and it is devoted to the decision. The fixes are described in the procedures that it links to.

What do you need? ​

  • A knowledge base built with the build procedure, here knowledge.duckdb.
  • The same embeddings model as the one used for indexing, for every verb below. Set it once as embeddings in the manifest, or pass the same --embeddings each time. A different model embeds the question in a different space and returns noise.
  • Five to ten real questions that fail. Take them from a log or from your users, and write down the document that should have answered each one.

Step 1: collect the failing questions ​

Do not start from a theory. Start from questions that you can replay. Put them in a file, one per line, and keep it: the next procedures use the same file to check that a change helps.

shell
$ cat failing-questions.txt
what is the refund window for the enterprise plan?
how do we roll back a Cedar release?

Step 2: read the hits ​

Run each question with a top-K of 20, which is the default, and read more than the first hit. The answer may be at rank 7.

shell
$ duckdb-vectors search "what is the refund window for the enterprise plan?" \
    --database knowledge.duckdb --top-k 20

Each hit carries the source of its page, the title of its section, the text of the chunk, and the metadata of the page, which includes document.title and section.headings. Two other attributes help to read the ranking: vector_rank and text_rank say where the embedding and the keyword search put the chunk. A score is an RRF value, higher is better, and it is not a probability.

For each question, answer these checks, in this order:

  1. Is the expected document in the sources of status --sources? If the answer is 'No', then the content is not indexed. Go to when tags are not the answer.
  2. Does the expected text exist in some chunk? Search for a sentence that you copy from the page. If you do not get that chunk at rank 1, then the index does not hold what you think.
  3. Where is the expected chunk in the 20 hits? Note its rank, or "absent".

Step 3: name the symptom ​

SymptomWhat you see in the hitsLayer that fixes it
Wrong documentHits are the right kind of passage, from another product, year or contract. The expected page is lower, or absent.Project the metadata you already have
Right document, wrong sectionThe expected source is in the hits, but the chunk is another section of the page.Project the headings, then generate contextual tags
Nothing relevantNo hit is close to the question, whatever the document.Not a tag problem. See below
Results change after a rebuildThe same question gives other hits after indexing again.Not a tag problem. See below

The first symptom is the common one. Chunking removes the context of a passage: a chunk that says "the refund window is 30 days" no longer says which product it belongs to. In other terms, two pages that look alike produce chunks that look alike, and the search cannot choose between them. A tag puts back the product, the company or the contract in front of the chunk.

For the second symptom, the document is found, but its sections look alike. The title of a section and the chain of its headings are already in the metadata of every chunk, so they cost nothing to project. Where the headings are too thin, a model can write a sentence that situates each section in its page.

Results that change after a rebuild have an explanation that you can check with status: the embeddings model, the declared tag_fields and the number of chunks. Changing the embeddings model, the tags, or the setting --tag-bm25 changes the vectors or the keyword index, hence the ranking. A search follows whatever index the file holds. Index again with the same settings, and the hits should be the same.

When tags are not the answer ​

A tag moves chunks that exist and are already near the top. It cannot do more than that. Check these causes first, because each of them looks like a tagging problem:

  • The content is missing. The page was not transcoded, it sits outside the folder that you indexed, or its text is in an image. Compare the sources of status --sources with your list of pages. The inspection procedure does this check. A scanned PDF page needs the OCR fallback of the PDF chunker.
  • The top-K is too small. If the expected chunk is at rank 12 and your agent reads 5 hits, then the search works and the client cuts too early. Raise top_k for this client, and accept the cost in prompt tokens.
  • The chunk does not fit the input window. Embedding models cut their input, 512 tokens for every fastembed model that we measured. A very long chunk, or a long tag prefix, pushes the end of the text out of the window. The tags report warns about the prefix, as the projection procedure explains.
  • The question needs a reranker. If the right chunk is in the 20 hits but never at the top, and no tag separates it from its neighbours, then a model that reads the question and each hit together may do better. duckdb-vectors has no reranker. This is a decision for your client, and we have not measured it.

Which layer comes next? ​

Go in the order of the cost of adoption. Stop at the first layer that fixes your questions.

  1. Project the metadata you already have: a title, a product, a company or a contract, put in front of each chunk. No model is called, and it takes minutes.
  2. Measure on your own queries, before and after, so that a tag is kept on evidence. Do this at each layer, and not only at the end.
  3. Generate contextual tags with a model where the metadata is thin. This is written once, offline, in sidematter files.
  4. Keep the tags fresh in continuous integration.

Next steps ​