Sub topic 1
Why keyword search falls short: matching words versus matching meaning
Prerequisites
This chapter assumes you have:
- Basic Python, including sets and regular expressions.
- A general idea of how keyword search works: a query is split into words, and documents that contain those words are returned.
- A rough sense of what a neural network does, which matters from the next chapters onward.
You don't need any machine learning libraries for this chapter.
How keyword search works
Keyword search treats text as a bag of tokens. A token is a unit of text, usually a lowercase word. The engine looks for documents that share tokens with the query. Production systems use scoring schemes such as TF-IDF or BM25, which weight rare words more heavily than common ones. The core idea stays the same. A document matches only if it contains the words the user typed, or close variants of them.
This approach is fast, predictable, and easy to explain. It works very well when users know the exact terms, such as error codes, product names, or legal citations.
Where it breaks
People describe the same idea in many ways. Keyword search can't see that, because it compares spellings and not meaning. Two failure modes appear again and again:
- Vocabulary mismatch. The query and the answer use different words for the same idea. "Reset my password" and "recover access to your account" share no words.
- Misleading overlap. The query and a document share words but mean different things. "Python bites" can match a page about snakes when the user wants a programming bug.
Stemming (cutting words to a root) and synonym lists reduce the first problem. You have to maintain those lists by hand, and they never cover every phrasing.
A demonstration
The script below scores each document by how many query tokens it contains. It runs as written.
import re
docs = [
"Steps to recover access to your account",
"Our office is open from 9 to 5 on weekdays",
"Change your login credentials in the security settings",
]
def tokens(text):
return set(re.findall(r"[a-z0-9]+", text.lower()))
def keyword_score(query, doc):
return len(tokens(query) & tokens(doc))
query = "How do I reset my password?"
for doc in docs:
print(keyword_score(query, doc), doc)Every document scores 0. The first and third documents are exactly what the user needs. A person sees that at once, but the scorer can't tell them apart from the office-hours page. A keyword engine would return nothing useful here, or an arbitrary ranking.
Matching meaning instead
Semantic search compares what texts mean instead of which words they contain. The approach has two parts:
- Convert each text into a list of numbers, called a vector, so that texts with similar meaning get similar vectors.
- Convert the query the same way, then return the documents whose vectors are closest to the query vector.
The conversion is done by a trained model, and the vectors it produces are called embeddings. The next chapters cover what an embedding is, how closeness is measured, and how to build the full pipeline.
Keyword search still has a place
Semantic search is not a strict upgrade. Embeddings can blur exact identifiers. A search for error code E4021 should return documents containing E4021, and a vector model may rank similar-looking codes close by. Keyword search is also cheaper and needs no model. Later chapters show how hybrid search combines both methods, so each covers the other's weaknesses.
Exercises
Exercise
In the demonstration, the query "How do I reset my password?" scores 0 against "Change your login credentials in the security settings". Explain why, and say which kind of failure this is.
Show answer
The query and the document share no tokens, so the overlap count is 0. This is vocabulary mismatch. The two texts express related ideas with different words, and keyword matching compares spellings, not meaning.
Exercise
Give one kind of query where keyword search is the better choice than semantic search, and say why.
Show answer
A query for an exact identifier, such as the error code E4021 or a product SKU. Keyword search returns documents containing that exact string. Embeddings can rank similar-looking identifiers as close, which gives wrong results.