Semantic Search
Semantic search goes beyond traditional keyword matching by understanding the meaning behind search queries. It uses vector embeddings to represent both queries and documents as points in high-dimensional space, finding results based on conceptual similarity.
For example, a semantic search for "how to deploy containers" would also find documents about "Kubernetes deployment," "Docker orchestration," and "container management" — even if they don't contain the exact search terms.
The typical semantic search pipeline involves: embedding documents during indexing, embedding the query at search time, performing approximate nearest neighbor search in a vector database, and optionally re-ranking results for relevance.
Hybrid search combines semantic search with traditional keyword search (BM25) for the best of both worlds. This is particularly effective when users mix conceptual queries with specific terms like error codes or product names.
How it works
At indexing time every document, or every chunk of a long document, is passed through an embedding model and the resulting vector is stored. At query time the query is embedded with the same model and the system returns the documents whose vectors are closest, most often by cosine similarity.
Comparing the query against every stored vector is fine for modest corpora; the sentence-transformers semantic_search utility is documented for corpora up to about a million entries. Larger systems use an approximate nearest-neighbour index inside a vector database, trading a little recall for much faster lookups.
The right model depends on the shape of the task. The sentence-transformers documentation distinguishes symmetric search, where query and documents are similar in length (finding duplicate questions), from asymmetric search, where a short query must match a longer passage, and stresses choosing a model trained for the right one.
Semantic search is often combined with lexical scoring. BM25, Elasticsearch's default, scores documents by term frequency, inverse document frequency and document-length normalisation (default k1 = 1.2, b = 0.75). Lexical matching catches exact identifiers such as error codes or product names that embeddings can blur, and the merged results can then be reordered by a reranking model.
Example
Embedding a three-sentence corpus and a query with sentence-transformers, following the library's own semantic search example.
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
corpus = [
"Rolling back a Kubernetes deployment",
"Baking sourdough bread at home",
"Restoring the previous version of a release",
]
corpus_emb = model.encode_document(corpus, convert_to_tensor=True)
query_emb = model.encode_query("how do I undo a bad deploy", convert_to_tensor=True)
scores = model.similarity(query_emb, corpus_emb)[0]
for text, score in sorted(zip(corpus, scores.tolist()), key=lambda p: -p[1]):
print(f"{score:.3f} {text}") The query shares no words with "Restoring the previous version of a release", yet that sentence and the Kubernetes rollback are expected to score well above the bread recipe, because the model places them near each other in meaning. A pure keyword search for "undo" would match none of the three. Run it to see the actual scores for this model.
Common confusion
What is the difference between semantic search and keyword search?
Keyword search (for example BM25) ranks documents by how often the query's exact terms appear, weighted by how rare those terms are. Semantic search ranks by closeness of meaning using embeddings, so it finds paraphrases and synonyms but can miss exact strings like version numbers. Hybrid search runs both and merges the results.
Is semantic search the same as RAG?
No. Semantic search returns documents. Retrieval-augmented generation takes the retrieved documents and puts them into an LLM prompt so the model can write an answer grounded in them. Semantic search is frequently the retrieval step inside a RAG system, but it is useful on its own as a search box.
Related tools & demos
- Embedding Space Explorer See words placed in 2D and search by nearest neighbours.
- Vector Embeddings The vectors semantic search compares.
- Vector Database Where the vectors are stored and indexed for fast lookup.
- Retrieval-Augmented Generation (RAG) What happens when search results are fed to an LLM.
Further reading
- Semantic Search Sentence Transformers documentation
- Practical BM25, Part 2: The BM25 Algorithm and its Variables Elastic
Want to learn more?
Explore more developer terms or read in-depth articles on the blog.
Browse all terms