r/SQLv2 • u/Alternative_Pin9598 • 5h ago
SQLv2 Lesson 12: teach the database your supplier's shorthand. 'blk mech kbd us' matches nothing at 0.35; run it through an 11-row abbreviation table and the same line hits 'Mechanical keyboard, US layout' at 0.81, then one word stays too vague to book (real output)
Lesson 11 ended on a promise. When 'blk' and 'ANSI' stopped matching once a second colour and layout existed, I said the fix was "a lookup table of your supplier's abbreviations, joined before Step 2." This is that table. (This sub is run by the SynapCores team; run on 2.5.1 this morning, 2.3.2 gave the same numbers.)
The problem
A new batch from the supplier, in their in-house shorthand. Six lines:
blk mech kbd us
usb-c chgr 65w
alu stnd
4k mon 27in
nc hdset mic
monYou read every one at a glance. The catalog does not (the 12 items from Lesson 11; CREATE and INSERTs in the first comment). Match the raw shorthand by meaning, Lesson 8's COSINE_SIMILARITY, and most of it falls under Lesson 8's 0.43 floor: the keyboard line scores 0.35, the stand 0.27, the headset 0.41. Booked by similarity alone, they'd all go to NO MATCH.
Step 1: keep your shorthand in a table
One row per abbreviation, a table you grow when a new one shows up: abbrev(seq, short, long), 11 rows, in the first comment.
Step 2: expand each line before you embed it
REPLACE swaps one word at a time, so fold the whole dictionary over the line with a recursive CTE, one abbreviation per step (full query in the first comment). Pad with spaces so 'us' expands the word and not the 'us' inside 'usb-c':
1 black mechanical keyboard US layout
2 usb-c charger 65w
3 aluminium stand
4 4k monitor 27in
5 noise-cancelling headset microphone
6 monitorStep 3, the payoff: expand, then match by meaning, in one statement
Wrap that expansion in the query that embeds the expanded phrase and scores it against the catalog (COSINE_SIMILARITY over EMBED, Lesson 8). Each line's best catalog match, scored as raw shorthand vs expanded:
blk mech kbd us -> Mechanical keyboard, US layout 0.35 -> 0.81
usb-c chgr 65w -> USB-C laptop charger, 65 W 0.69 -> 0.91
alu stnd -> Aluminium laptop stand 0.27 -> 0.84
4k mon 27in -> 27-inch 4K monitor 0.61 -> 0.94
nc hdset mic -> Noise-cancelling headset with microphone 0.41 -> 0.98
mon -> 27-inch 4K monitor 0.16 -> 0.62
6 rows in 0.09 sThe three lines under the floor as shorthand (0.35, 0.27, 0.41) clear 0.80 once expanded. The dictionary is plain SQL: REPLACE and a recursive CTE over a table you grow one row at a time. The meaning match is the SynapCores part, EMBED turns the expanded phrase into a vector inside the engine and COSINE_SIMILARITY scores it, no API and no key.
When do you trust it? Look at line 6.
'mon' expands to 'monitor', and that is all it says. Two monitors in the catalog, so the same query that books the rest sends this one to review:
line expanded pick top 2nd action
1 ...keyboard US layout Mechanical keyboard 0.81 0.69 BOOK
4 4k monitor 27in 27-inch 4K monitor 0.94 0.72 BOOK
6 monitor 27-inch 4K monitor 0.62 0.60 REVIEW (too close to call)Line 4, "4k mon 27in", books the 4K monitor at 0.94: the '4k' and '27in' it kept are enough to tell them apart. Line 6 is the bare word 'mon', 0.62 for the 4K and 0.60 for the 1080p, a 0.02 gap. The score is the tell. A clear leader books; a near-tie goes to a person. One word was never going to pick a resolution, and the query says so instead of guessing.
Lesson 11: https://www.reddit.com/r/SQLv2/comments/1yb1cm6/
Free Community Edition: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=sqlv2-lesson-12
Homework. Your shorthand grows. The supplier starts writing 'wl mse' for a wireless mouse.
- Add (12,'wl','wireless') and (13,'mse','mouse') to abbrev. What does 'wl mse blk' expand to, and which catalog item does it match, above or below 0.43?
- A line that just says 'kbd' expands to 'keyboard'. With a US and a UK keyboard in the catalog, what does the decide query do with it, and is that the answer you want?
What shorthand do your suppliers (or your own team) use that a new hire just has to learn?