Part 5 of 12 · Data Structures

How do you match messy part numbers to a catalog?

Customers write 'brg-6204 2rs', 'BRG62O4RS' and 'sealed bearing 6204'. Exact maps, normalised keys, edit distance and token overlap each catch a different kind of mess, and each has a data structure that keeps it fast at 100,000 parts. Measured.

Rows of small blue and white drawers holding nuts and bolts in a hardware shop

The verdict, which the measurements below will earn: in tiers, cheapest and most certain first.

  1. Exact lookup in a hash map.
  2. The same lookup after normalising: upper-case, strip spaces and punctuation, so brg-6204 2rs finds BRG62042RS.
  3. Edit distance for typos, with an index so you don’t compare against every part.
  4. Word overlap on the description when there’s no usable part number, through an inverted index.

Then cross-check whatever matched against the description and the unit, because the dangerous typo is the one that turns one valid part number into another valid part number. Brute-force scans are fine for a few thousand parts. At 100,000, a typo lookup by scanning took 25 ms per line; the right index took 2 µs.

The case arrives as an order line: BRG62O4RS. Is that a bearing in the catalog, or a different bearing, or nothing at all? Multiply by thousands of lines and a catalog of 100,000 parts, and the obvious approach, comparing against everything, grinds to a halt. This problem looks ordinary, and a surprising amount of computer science hides inside it. A fifty-year-old tree, a trick about deleting letters and a lesson about common words all turn up on the way to reading one order line.

This builds on the matcher in my LLM document-ingestion pipeline (internal/catalog), which uses exactly these tiers with linear scans. The scaling measurements are in systems-data-structures (go run ./cmd/partdemo), on a synthetic catalog of SKUs like BRG6204RS.

Case file

The case BRG62O4RS: which part did the customer mean?
Suspects Comparing every line against the whole catalog
Culprit Brute-force scans at 100,000 parts, and a typo that’s also a valid part
The fix Tiers: exact, normalised, edit distance through an index, word overlap; then cross-check
Evidence systems-data-structures (go run ./cmd/partdemo) on Go 1.27 with synthetic catalogs
Flowchart: Order line, Exact SKU in the map?, Cross-check description and unit, Normalised key matches?, Within one edit, via an index?, Word overlap on the description, Accept, close call, Send to a personFlowchart: Order line, Exact SKU in the map?, Cross-check description and unit, Normalised key matches?, Within one edit, via an index?, Word overlap on the description, Accept, close call, Send to a person
Diagram source
flowchart TD
  L["Order line"] --> E{"Exact SKU in the map?"}
  E -- yes --> X["Cross-check description and unit"]
  E -- no --> N{"Normalised key matches?"}
  N -- yes --> X
  N -- no --> T{"Within one edit, via an index?"}
  T -- yes --> X
  T -- no --> W["Word overlap on the description"]
  W --> X
  X -- agrees --> OK["Accept"]
  X -- "close call" --> P["Send to a person"]

The suspects: four kinds of mess, four structures #

Each kind of mess has its own natural tool, and the trick is matching culprit to method:

What the customer wrote What’s wrong Structure Cost per lookup
BRG6204RS Nothing Hash map O(1)
brg-6204 rs Case, spaces, punctuation Hash map on a normalised key O(1)
BRG62O4RS (letter O for zero) A typo Edit distance: linear scan, BK-tree, or symmetric-delete index O(n) for the scan; much less with an index
BRG62… Only the start Sorted array (or trie) + binary search O(log n)
sealed bearing 6204 please No part number at all Word overlap (Jaccard) through an inverted index Proportional to the items sharing a word

Tiers 1 and 2: exact and normalised #

This is the cheapest tier, and with reasonably clean input most lines stop here. Build one map from SKU to part, and another from a normalised key to part. Normalising means: upper-case; keep letters and digits only; maybe also fold look-alikes (O→0, I→1) if your SKUs never mix them.

Two things to get right:

  • Normalisation can collide. Two real SKUs that differ only by punctuation, such as AB-12-3 and AB-123, normalise to the same key. Detect that when you build the map, and treat a hit on such a key as ambiguous, not a match. The ingestion pipeline’s matcher drops its confidence to 0.4 in that case.
  • Keep the original SKU for display and for the order; the normalised key is only for lookup.

Tier 3: the typo (edit distance) #

A typo is one edit away: a substituted, missing or extra character. The obvious way to find the parts within one edit is to compute the edit distance to every SKU. That’s what my ingestion matcher does, and for its catalog size it’s the right call. But here’s the evidence on how it scales, with 1,000 lookups per row, each a real SKU with one random typo:

Catalog Linear scan BK-tree, ≤ 1 edit BK-tree, ≤ 2 edits Symmetric-delete index (≤ 1 edit)
1,000 249 µs 32 µs 91 µs 0.99 µs
10,000 2.5 ms 135 µs 598 µs 2.5 µs
100,000 25.1 ms 608 µs 3.6 ms 1.94 µs
Distances computed at 100,000 100,000 1,297 7,886 a handful, to verify
Index build at 100,000 - 257 ms 257 ms 442 ms; 949K keys
Chart: Typo lookup, µs per query, catalog of 1,000Chart: Typo lookup, µs per query, catalog of 1,000
Diagram source
xychart-beta
  title "Typo lookup, µs per query, catalog of 1,000"
  x-axis ["Linear scan", "BK-tree ≤ 1 edit", "BK-tree ≤ 2 edits", "Symmetric-delete"]
  y-axis "µs"
  bar [249 , 32 , 91 , 0.99]

Every index returned exactly the same matches as the scan; a test checks this on 300 random typos.

  • A BK-tree (Burkhard and Keller, 1973) files each SKU under its distance from a parent. Because edit distance obeys the triangle inequality, a search only visits branches that could contain a match. At 100,000 SKUs it computed 1,297 distances instead of 100,000, about 40× faster: one inequality lets you rule out whole subtrees without looking at them. It allows any maximum distance, but the saving shrinks quickly as the distance grows (7,886 at two edits). One catch: it needs a true metric. Plain Levenshtein distance is one; the “optimal string alignment” variant that also counts swapped letters as one edit is not, so use full Damerau-Levenshtein or plain Levenshtein in the tree.
  • A symmetric-delete index (the idea behind SymSpell) stores every SKU and every string you get by deleting one character from it. Two strings within one edit always share one of those forms, so a lookup generates the query’s deletions, looks each one up, and verifies the few candidates. This trick turns the search inside out, and it’s my favourite reveal in the post: instead of looking for near neighbours, you make them collide on purpose. That made it about 13,000× faster than the scan at 100,000 SKUs, and flat as the catalog grows. The cost is memory: about 9.5 index keys per SKU for one edit, and far more for two.

The most dangerous suspect: a typo that’s also a valid part. BRG6204RS with one typo might be BRG6205RS, a real, different bearing. Edit distance can’t tell you which one the customer meant. So a fuzzy match should never be accepted on the part number alone. Check the description (“6204” in the text?), the unit and the price band. If the near-misses are close to each other, send the line to a person (the confidence-routing post covers how to set that threshold). In my ingestion pipeline, a fuzzy match on the SKU alone stays below the auto-accept threshold until the description agrees.

Tier 3b: prefixes #

For search-as-you-type, or for a customer who writes only the start of the code:

Catalog Linear scan Sorted array + binary search
1,000 5.3 µs 0.21 µs
100,000 563 µs 5.6 µs

A sorted array keeps every SKU with a given prefix in one contiguous run: binary-search to the start, read until the prefix stops matching. A trie does the same with more memory and an easier “what are the possible next characters?” (the autocomplete post compares the two). Databases do this with a B-tree index and LIKE 'BRG62%' (in PostgreSQL, with a text_pattern_ops index unless the database uses the C collation).

Tier 4: the description #

Now for the hardest case, with no code to lean on at all. When there’s no usable part number (“sealed bearing 6204, 2 off please”), compare words: split into tokens, expand trade abbreviations (ss → stainless steel, brg → bearing), and score each catalog item by word overlap (Jaccard: shared words ÷ distinct words in either).

Catalog Score every item Inverted index Inverted index, rare words only
1,000 162 µs 257 µs 81 µs
100,000 14.5 ms 25.6 ms (scored 94% of items) 6.5 ms (scored 26%)

Here’s the false lead, and the surprise: the plain inverted index (word → items containing it) was slower than scanning. Queries contained words like “hydraulic”, “bearing” and “series” that appear in a large part of the catalog, so the index produced nearly every item as a candidate, plus its own overhead. An inverted index only helps through selective words. Gathering candidates only through words that appear in at most 1% of items cut the work to a quarter. In these queries, it still found the same best score as the full scan every time, though nothing guarantees that in general.

Real search engines solve this properly: they rank words by how rare they are (IDF, BM25), and stop early once the top results can’t change. If you need description search over a big catalog, use one: PostgreSQL full-text search (tsvector + GIN), or OpenSearch, Elasticsearch, Meilisearch. For fuzzy SKUs in PostgreSQL, the pg_trgm extension with a GIN index gives you similarity() and the % operator without building anything yourself.

Putting it together: what I’d build for a B2B order-entry pipeline #

  • Up to a few thousand parts: the tiers above with linear scans. Everything fits in memory, every lookup is under a millisecond, and the code is easy to reason about. That’s what the ingestion pipeline does.
  • Tens of thousands to millions of parts: exact and normalised maps, a symmetric-delete index (or pg_trgm) for typos, and a real full-text index for descriptions. Rebuild the in-memory indexes when the catalog changes, or keep them in the database.
  • Always: cross-check every non-exact match against the description and unit; never auto-accept a fuzzy match on the part number alone; send close calls to a person with the candidates shown side by side.

Remember this

  • Normalise, then look up in a map: most of the mess is case and punctuation.
  • Index typos: the symmetric-delete index took 2 µs where the scan took 25 ms.
  • Never auto-accept a fuzzy match on the part number alone.

Case closed: what I’d do by default #

  1. Normalise, then look up in a map. Most of the mess is case and punctuation.
  2. Edit distance ≤ 1-2 for typos, through an index once the catalog passes about 10,000 parts.
  3. Descriptions through a search index that weights rare words, not a hand-rolled inverted index.
  4. Cross-check and route doubts to a person. A wrong part shipped costs far more than a line someone glanced at.

None of these structures is new: Burkhard and Keller, the SymSpell idea and decades of search-engine work already did the hard thinking. The fun, and the quiet satisfaction of the whole case, is noticing which one each kind of mess is asking for.

Measured Nov 2026 with Go 1.27 on a laptop (Intel i7-8550U), synthetic catalogs of 1,000 to 100,000 SKUs, 1,000 queries per row. Code and full results: systems-data-structures; the production-style matcher: llm-document-ingestion, internal/catalog.

Like (sign in first) 0 likes 💬 0

Written by

Vishal Kumar

Associate Director at CapGrid, running product and engineering in a CPTO capacity. 10+ years building distributed, multi-tenant SaaS and AI platforms. AWS Certified Solutions Architect - Professional. Based in Gurugram.

Comments

No comments yet.

Sign in to comment. We'll email you a link; no password needed.