Objective
Keyword search fails the moment a user phrases a question in their own words.
"How long do I have to send something back" does not contain the word refund,
so LIKE '%refund%' finds nothing, and the help article that answers it sits
one row away.
The usual fix is a second system: a vector database, an ingestion pipeline to keep it in sync, and two sources of truth. This recipe does it in one table with two SQL functions.
Step 1: A table with an embedding column
VECTOR(384) must match the embedding model's output. The default model,
all-minilm, produces 384 dimensions. The engine checks the size on every
write, and a mismatch fails right there with an error that names both sizes.
-- Drop first so the recipe is re-runnable.
DROP TABLE IF EXISTS recipe_sem_articles;
CREATE TABLE recipe_sem_articles (
article_id INT PRIMARY KEY,
title TEXT,
body TEXT,
embedding VECTOR(384)
);
Step 2: Write the rows and their meaning in one statement
EMBED() turns text into a vector at insert time. Embed the title and body
together — the title alone is usually too short to carry the meaning.
INSERT INTO recipe_sem_articles (article_id, title, body, embedding) VALUES
(1, 'Refund policy',
'Unopened items can be returned within 30 days for a full refund.',
EMBED('Refund policy. Unopened items can be returned within 30 days for a full refund.')),
(2, 'Shipping times',
'Standard delivery takes 3 to 5 business days within the country.',
EMBED('Shipping times. Standard delivery takes 3 to 5 business days within the country.')),
(3, 'Password reset',
'Use the forgot-password link to receive a reset email.',
EMBED('Password reset. Use the forgot-password link to receive a reset email.')),
(4, 'Warranty',
'Hardware is covered for 24 months against manufacturing defects.',
EMBED('Warranty. Hardware is covered for 24 months against manufacturing defects.'));
Step 3: Ground truth — show that keyword search fails
Before claiming semantic search helps, prove the alternative does not:
SELECT COUNT(*) AS keyword_hits
FROM recipe_sem_articles
WHERE body LIKE '%send something back%';
Expected: 0. The refund article is the right answer and contains none of those words. This is the gap, in one query.
Step 4: Search by meaning
SELECT
title,
ROUND(COSINE_SIMILARITY(embedding, EMBED('how long do I have to send something back')), 4) AS similarity
FROM recipe_sem_articles
ORDER BY similarity DESC
LIMIT 3;
Expected, in this order:
| title | similarity |
|---|---|
| Refund policy | 0.6509 |
| Shipping times | 0.4303 |
| Password reset | 0.2601 |
The right article wins with no shared keywords. "Shipping times" placing second is correct behaviour rather than a bug — both are about getting goods moved — and the 0.22 gap between first and second is what you threshold on.
Alias the similarity so it shows next to each row. Sorting by the alias, as above, is the readable form.
ORDER BY COSINE_SIMILARITY(...)written out directly is also accepted on 2.5.1 and returns the same order.
Step 5: Combine meaning with ordinary SQL
This is the part a separate vector database makes hard. Filters, joins and similarity in one statement, against one copy of the data:
SELECT
article_id,
title,
ROUND(COSINE_SIMILARITY(embedding, EMBED('my device stopped working after a year')), 4) AS similarity
FROM recipe_sem_articles
WHERE article_id <> 3
ORDER BY similarity DESC
LIMIT 2;
Expected: Warranty first — a year-old device failing is a warranty
question, and the model knows that without the word warranty appearing in the
query.
The WHERE is the point. In a two-system setup you would search the vector
store, get ids back, then query the database to filter them, and handle the case
where filtering empties your top-k. Here the filter is just a predicate.