Combining traditional search with AI embeddings gets you semantic search — finding results based on genuine meaning, not just keyword overlap, which is exactly what “search for a word that doesn’t literally appear in the matching document” requires.
from sentence_transformers import SentenceTransformer
import numpy as np
model = SentenceTransformer('all-MiniLM-L6-v2')
documents = [
"How to reset your password",
"Troubleshooting a slow internet connection",
"Cancelling your subscription",
]
doc_embeddings = model.encode(documents)
def search(query, top_k=2):
query_embedding = model.encode([query])[0]
# Cosine similarity between the query and every document
scores = np.dot(doc_embeddings, query_embedding) / (
np.linalg.norm(doc_embeddings, axis=1) * np.linalg.norm(query_embedding)
)
top_indices = np.argsort(scores)[::-1][:top_k]
return [(documents[i], scores[i]) for i in top_indices]
# "I forgot my login" finds the password reset doc,
# even though it shares almost no exact keywords with it
search("I forgot my login")
Why This Beats Pure Keyword Search
Traditional keyword search (even with stemming and synonyms) requires the searched terms to substantially overlap with document terms. Embedding-based semantic search instead compares meaning — “I forgot my login” and “how to reset your password” end up with similar vector representations despite sharing almost no exact words, because the underlying model learned that these phrases are semantically related during its own training.
The Practical Middle Ground: Hybrid Search
Production search systems increasingly combine both approaches — keyword search for precise term matching (exact product names, error codes) plus semantic search for conceptual matching, merged together, since neither approach alone reliably covers both kinds of query intent well.