Measured with a local embedding model: two different ISBNs score 0.975, and ISBN queries found the right note 1 time in 8. Route identifiers to an exact lookup first.
The wrong case, retrieved with confidence
Dinky's help-desk assistant for Belgavi's Book Store answers questions about open cases by retrieving case notes from a vector index. A colleague asks "What is the status of CASE-1042?" and the assistant summarises CASE-1043, a different customer's wrong-edition complaint. Both notes are written from the same template, and the two case numbers differ by one digit. To the embedding model, they are nearly the same text.
Identifiers are the weak spot of dense retrieval. Case numbers, order ids, ISBNs, SKUs, error codes and version strings carry their meaning in exact characters, and an embedding model trained to map similar meaning to nearby vectors has no reason to keep 1042 far from 1043. This guide measures that with a small local model, then shows a deterministic router that sends identifier queries to an exact lookup before, or alongside, vector search.
Measuring how close identifiers are
The companion example uses all-minilm (the 23-million-parameter, 384-dimension sentence-transformers model all-MiniLM-L6-v2) through a local Ollama server's /api/embed endpoint, and computes cosine similarity between pairs of short strings. Two runs gave identical output.
| String A | String B | Cosine similarity |
|---|---|---|
| 978-0-13-468599-1 | 978-0-13-468598-4 | 0.975 |
| 978-0-13-468599-1 | 978-1-49-207800-5 | 0.905 |
| CASE-1042 | CASE-1043 | 0.955 |
| CASE-1042 | CASE-2401 | 0.787 |
| order 55012 | order 55021 | 0.915 |
| refund request | return request | 0.533 |
| refund request | shipping delay | 0.230 |
Two different ISBNs, both valid, are more similar to each other (0.975) than "refund request" is to "return request" (0.533). Even two unrelated ISBNs score 0.905. The model is doing what it was trained for: both strings are "an ISBN", and that is most of what they mean to it.
What that does to retrieval
Similarity between bare strings is not the whole story, because real queries and documents have other words. So the example also builds a tiny index: eight synthetic case notes, all from the same template ("CASE-1042: customer Jenya reports damaged cover on book ISBN 978-0-13-468599-1. Return approved, refund to original payment."), with different case numbers, names, issues and ISBNs. It then asks sixteen questions, eight by case number and eight by ISBN, and checks whether the right note ranks first by cosine similarity.
| Query type | Right note ranked first | Worst rank of the right note |
|---|---|---|
| "What is the status of CASE-…?" | 5 of 8 | 5th of 8 |
| "Which case is about ISBN …?" | 1 of 8 | 8th of 8 |
For case numbers, three of eight queries returned a neighbour: CASE-1042 retrieved CASE-1043 first, CASE-1044 ranked its own note fifth. For ISBNs, seven of eight queries ranked the same unrelated note first, a note whose wording happened to sit closest to the question template, and one query ranked its own note last. This is one small model on a deliberately uniform corpus, and larger embedding models do better on some identifiers. But the shape of the problem is general: nothing in a dense embedding guarantees that one changed digit produces a different nearest neighbour.
Route identifiers to an exact lookup
The fix is to stop asking the vector index a question it is not built to answer. Before retrieval, a deterministic router extracts identifiers from the query using the formats your systems actually issue, validates them, and looks them up exactly.
import re
CASE = re.compile(r"\bCASE-\d{4}\b")
ISBN13 = re.compile(r"\b97[89][- ]?(?:\d[- ]?){9}\d\b")
def isbn13_ok(raw: str) -> bool:
d = [int(c) for c in raw if c.isdigit()]
return len(d) == 13 and sum(x * (1 if i % 2 == 0 else 3) for i, x in enumerate(d)) % 10 == 0
def route(query: str) -> dict:
cases = CASE.findall(query)
isbns = ["".join(c for c in m if c.isdigit()) for m in ISBN13.findall(query) if isbn13_ok(m)]
if cases or isbns:
return {"mode": "exact", "case_ids": cases, "isbns": isbns}
return {"mode": "vector"}The exact path queries the system of record, or a keyword index over normalised identifiers, and returns the matching documents with a score that says "exact match". If the identifier is valid but nothing matches, the answer is "no such case", which is far better than the nearest neighbour. The check digit matters: ISBN-13 has one, so a typo is usually caught before lookup, and the router can say "that ISBN is not valid" instead of guessing.
Normalise before comparing. ISBNs are written with and without hyphens; case numbers may appear in lower case or with a space. Store one canonical form in the index and convert queries to it.
Combining both paths
Most real questions mix an identifier with a description: "Did CASE-1042 get its refund for the damaged cover?" A good design uses both:
- Extract identifiers. If any are found, filter candidates to the documents carrying exactly those identifiers.
- Rank within that filtered set by vector similarity, or by recency, to choose the most relevant note for the case.
- If no identifier is found, run vector search as before.
Many vector databases support a metadata filter applied together with the similarity search; store the identifiers as structured fields when you index, rather than relying on them being somewhere in the chunk text. Hybrid search, a lexical ranking such as BM25 fused with vector similarity, also helps, because exact tokens dominate a lexical score. But fusion is still a ranking: a near-identical identifier with many shared words can still rank above the exact one. A filter on the identifier field is a hard constraint, and identifiers deserve one.
Failure modes to test
- Identifier formats you did not list. The router only knows the patterns you give it. Inventory the identifiers people actually type, including legacy formats, before relying on it, and log queries that look like identifiers but matched no pattern.
- Identifiers inside retrieved text. When an answer is generated from several notes, the model can still attribute a fact from one case to another. Ask it to cite the case id of each note it used, and check those ids against the filter.
- Partial identifiers. "the 1042 case" has no prefix. Decide whether a bare four-digit number may be a case id in your context, and if so, ask a clarifying question when it matches several kinds of identifier.
- Over-eager routing. A year ("2026") or a page number can look like an id. Keep patterns specific and validate them, as with the ISBN check digit.
Indexing identifiers so the filter can work
Routing only helps if the exact path has something exact to query. When documents are chunked and embedded, extract their identifiers at indexing time and store them as structured fields next to the vector: the case id of the note, every ISBN it mentions, the order ids. Store them normalised, digits only for ISBNs and upper case for case ids, and index those fields with an ordinary equality index.
Two details save trouble later. First, keep the identifier on every chunk of a long document, not only the chunk where it appears in the text; otherwise a filter on the case id finds the first chunk of the note and misses the rest. Second, record which identifiers a chunk merely mentions and which one it is about. A note on CASE-1042 that says "see also CASE-1043" should match a filter for CASE-1043 only as a reference, so rank documents whose primary identifier matches above documents that only mention it.
Verifying the change
Build an evaluation set of identifier queries from your own logs, with the correct document for each, and measure two numbers before and after routing: how often the correct document is first, and how often a wrong document with a near-identical identifier is returned. The second number is the one that matters for trust. In the companion example, routing makes both case-number and ISBN queries return the right note for all sixteen questions, which is unsurprising for exact lookup and is exactly the point: the vector index is no longer being asked.
Limits
The measurements come from one small embedding model served locally, a corpus of eight synthetic notes and sixteen questions. They demonstrate a mechanism, not a benchmark, and other models will give other numbers. The example's offline tests use recorded embeddings and need no server; the live probe needs Ollama with all-minilm pulled.