Skip to content
AI360Xpert
Gen AI

Semantic Search

Instead of searching for exact keywords (CTRL+F), semantic search searches for meaning. If you search for 'dog', it will return results for 'puppy' and 'hound', even if the word 'dog' never appears in the text.

Traditional search looks for exact keyword overlaps. Semantic search embeds the query and the documents into the same vector space, then returns the documents that are geometrically closest to the query.
Traditional search looks for exact keyword overlaps. Semantic search embeds the query and the documents into the same vector space, then returns the documents that are geometrically closest to the query.

Why Does This Exist?

For decades, search engines ran on Lexical Search (specifically, BM25 or TF-IDF). Lexical search operates by matching exact words. If you search a database of medical records for "heart attack", it will only return documents that explicitly contain the words "heart" and "attack." It will completely miss a document that says "myocardial infarction."

This is the "vocabulary mismatch" problem. Humans have dozens of ways to say the exact same thing.

Semantic Search solves this by searching for meaning rather than exact strings. It is the underlying retrieval technology that makes Retrieval-Augmented Generation (RAG) and modern enterprise search possible.

Think of It Like This

Think of It Like This

Imagine a massive library where books are sorted purely by alphabetical order of the title. If you want a book about "Making Espresso", you look under 'M'. You will completely miss a book called "The Art of Coffee", because 'C' doesn't match 'M'. That is Lexical Search.

Now imagine a library where books are grouped by their concept. All the books about caffeine, brewing, espresso, and coffee beans are in the same physical corner of the room. When you ask for "Making Espresso", the librarian takes you to that corner and hands you the 10 closest books. That is Semantic Search.

How It Actually Works

Semantic search relies entirely on Dense Vector Embeddings.

1. The Offline Phase (Indexing)

You take every document in your database and pass it through an Embedding Model (like OpenAI's text-embedding-3-small or a local Sentence Transformer). The model converts the document's text into a dense vector—an array of numbers (e.g., 1536 dimensions). You store these vectors in a Vector Database (like Pinecone, Milvus, or pgvector).

2. The Online Phase (Querying)

When a user types a query (e.g., "How do I fix a leaky pipe?"), you do not run a SQL LIKE query. Instead, you pass their query through the exact same Embedding Model used in step 1. The model turns their query into a 1536-dimensional vector.

You now have a Query Vector and a database of millions of Document Vectors. You calculate the distance between the query vector and every document vector using a metric like Cosine Similarity. Because the model was trained to put semantically similar concepts near each other in the multi-dimensional space, the document about "Repairing dripping plumbing" will have a very high cosine similarity to the query "How do I fix a leaky pipe?", even though they share almost zero keywords. You return the Top-KK closest vectors.

Show Me the Code

Here is how you execute a semantic search using Python, sentence-transformers, and scikit-learn.

from sentence_transformers import SentenceTransformerfrom sklearn.metrics.pairwise import cosine_similarityimport numpy as np
# 1. Load a pre-trained embedding modelmodel = SentenceTransformer('all-MiniLM-L6-v2')
# 2. Your database of documentsdocuments = [    "The myocardial infarction patient requires immediate care.",    "Baking soda and vinegar is a great way to clean a kitchen.",    "A dog is a man's best friend.",    "Canines are known for their loyalty to humans."]
# 3. Embed the documents (Offline Indexing)doc_embeddings = model.encode(documents)
# 4. Embed the user's query (Online Search)query = "Tell me about loyal pets."query_embedding = model.encode([query])
# 5. Calculate Cosine Similarity between the query and all documentssimilarities = cosine_similarity(query_embedding, doc_embeddings)[0]
# 6. Get the index of the most similar documentbest_match_idx = np.argmax(similarities)
print(f"Query: '{query}'")print(f"Top Result: '{documents[best_match_idx]}'")print(f"Similarity Score: {similarities[best_match_idx]:.3f}")
# Output:# Query: 'Tell me about loyal pets.'# Top Result: 'Canines are known for their loyalty to humans.'# Similarity Score: 0.651

Watch Out For

Watch Out For

The Lexical Blindspot (Why you still need Hybrid Search). Semantic search is incredible at conceptual matching, but it is surprisingly terrible at exact noun matching. If a user searches for an exact serial number ("TX-9942-B"), an exact error code ("Err-505"), or a highly specific name, the embedding model will often blur the specific details and return documents about general errors or general serial numbers. In production, you rarely use pure Semantic Search. You use Hybrid Search: you run Semantic Search (for concepts) and BM25 Lexical Search (for exact keywords) simultaneously, and merge the results using Reciprocal Rank Fusion.

The Quick Version

  • Semantic Search finds documents based on their meaning, not their exact spelling.
  • It works by passing all documents through an Embedding Model to create dense vectors, which are stored in a Vector Database.
  • At query time, the user's query is also embedded into a vector.
  • The system returns the documents whose vectors have the highest Cosine Similarity to the query vector.
  • It solves the vocabulary mismatch problem but struggles with exact-keyword lookups, which is why it is usually paired with lexical search.
  • query-understanding — How to extract intent and entities from a query before you embed it.
  • embeddings — The deep-dive into how text is mathematically converted into vectors.
  • approximate-nearest-neighbor-search — How to calculate cosine similarity across a billion vectors in milliseconds without actually comparing them all.

Related concepts