Semantic Search
Semantic search is an information-retrieval approach that matches queries to documents based on meaning rather than exact keyword overlap. A query and every candidate document are each converted into dense vector embeddings — numerical representations that capture semantic content — and retrieval is performed by finding the vectors whose direction is closest to the query vector, typically via cosine or dot-product similarity. This allows the system to surface a document about "myocardial infarction" in response to a query for "heart attack," even though the terms share no words.
Think of it like searching a library by describing the concept you need to a knowledgeable librarian, rather than handing them a list of exact keywords and hoping the catalogue uses the same vocabulary.
import numpy as np
def semantic_search(query_vec, doc_vecs, top_k=5):
# Normalise for cosine similarity
q = query_vec / np.linalg.norm(query_vec)
D = doc_vecs / np.linalg.norm(doc_vecs, axis=1, keepdims=True)
scores = D @ q
return np.argsort(scores)[::-1][:top_k]
Semantic search is the retrieval layer inside RAG pipelines and relies on Vector Database infrastructure to scale efficiently; improving the underlying Embedding model directly improves retrieval quality. It is closely related to Zero-Shot generalisation, since a good embedding space handles novel query phrasings without retraining, and to Semantic Chunking which determines how documents are indexed in the first place.
You will encounter semantic search in enterprise document search, customer support automation, code retrieval tools, and any application where keyword search produces too many misses or irrelevant matches.