DEV Community

RAMO
RAMO

Posted on

Half inch, 1/2", or 15mm? Teaching a store search box that they're the same thing

Plumbing parts have a naming problem that has nothing to do with spelling. One fitting can be written as 1/2", 1/2 inch, ½, half inch, חצי צול in Hebrew, or by people who think in metric, 15mm or 20mm depending on what they're measuring. All of those can mean the same product.

On our store, RAMO, the search box used to treat each of those as a different word. Someone typing ברז חצי צול (half-inch tap) and someone typing ברז 1/2 were effectively searching two different catalogs. Fuzzy matching didn't help, because these aren't typos. They're different spellings of the same measurement.

Here's what we ended up doing. Nothing clever, but it took a few rounds to get right.

Step 1: normalize sizes before anything else touches the query

We pull sizes out of the query and turn them into one canonical token before the text goes to full-text or trigram search. The same function runs on product titles when we index them, so both sides end up speaking the same language.

const SIZE_ALIASES: Array<[RegExp, string]> = [
  [/(?:1\/2|½|half\s*inch|חצי\s*צול)(?:\s*(?:"|''|inch|אינץ'?|צול))?/gi, 'size_1_2in'],
  [/(?:3\/4|¾|שלושה\s*רבעי\s*צול)(?:\s*(?:"|''|inch|אינץ'?|צול))?/gi, 'size_3_4in'],
  [/(?:\b1\b)\s*(?:"|''|inch|אינץ'?|צול)/gi, 'size_1in'],
];

export function normalizeSizes(input: string): string {
  return SIZE_ALIASES.reduce((s, [re, token]) => s.replace(re, ` ${token} `), input)
    .replace(/\s+/g, ' ')
    .trim();
}
Enter fullscreen mode Exit fullscreen mode

Two things we learned the hard way:

  • Order matters. 1 1/2" has to be matched before 1/2", or you turn a one-and-a-half-inch pipe into "1 + half inch".
  • A bare 1 is not a size. We only treat a number as a size when a unit follows it. Otherwise "1 tap" or a model number like A1 gets rewritten into something silly.

Step 2: keep the alias list in the database, not in the code

The regex above is a simplified version. In practice the aliases live in a table, because the people who hear how customers actually talk are on the phone and in the shop, not in the repo. When someone notices customers writing 3/4 אינץ with a typo'd geresh, adding it shouldn't need a deploy.

create table search_size_alias (
  id serial primary key,
  pattern text not null,       -- what people type
  canonical text not null,     -- e.g. size_1_2in
  priority int not null default 0  -- longer / more specific patterns first
);
Enter fullscreen mode Exit fullscreen mode

We load it once, build the regexes, and cache them. Priority handles the 1 1/2 vs 1/2 ordering without anyone having to think about regex.

Step 3: be honest about metric

This is where it gets messy. Pipe "inch" sizes are nominal; they don't map neatly to one millimetre number, and the right metric equivalent depends on whether you're talking about the thread, the outer diameter, or a PEX/multilayer pipe. So we deliberately did not add 15mm → size_1_2in as a hard alias.

Instead, metric sizes get their own tokens, and we added a "related size" hint that shows up under the results: "Looking for 1/2" fittings? Here are those too." It suggests, it doesn't silently merge. A wrong merge sends someone home with a part that doesn't fit, and a returned plumbing part is a worse outcome than one extra click.

For people who aren't sure which size they actually have, we point them to a small pipe diameter selector instead of trying to guess in the search box.

Step 4: test with real queries, not invented ones

The most useful test file we have is just a list of query pairs that must return the same top results:

const sameResults: Array<[string, string]> = [
  ['ברז חצי צול', 'ברז 1/2"'],
  ['מחבר ½', 'מחבר 1/2 אינץ'],
  ['3/4 inch elbow', 'ברך 3/4'],
];

test.each(sameResults)('%s ≈ %s', async (a, b) => {
  const [ra, rb] = await Promise.all([search(a), search(b)]);
  expect(ra.slice(0, 5).map(p => p.id)).toEqual(rb.slice(0, 5).map(p => p.id));
});
Enter fullscreen mode Exit fullscreen mode

Whenever a customer tells us "I searched X and didn't find it", that query goes into the list. It's grown into the best documentation we have of how people actually describe parts.

What I'd tell anyone with a parts catalog

  • Sizes are vocabulary, not typos. Fuzzy search won't save you here.
  • Normalize the query and the indexed titles with the same function.
  • Put aliases where non-developers can edit them.
  • Don't auto-convert between unit systems unless the conversion is actually exact for that product type. Suggest instead.

If your catalog looks anything like our pipe fittings category, where half the titles are mostly numbers and quote marks, this is probably worth a day of work. I'm curious how others handle the metric/imperial overlap, especially for threads.

Top comments (0)