Semantic and Hybrid Search
Complete the full lesson to earn 25 points
Work through each section, then tap “Mark as Complete” on the last one.
✦ Skip the page breaks and see fewer ads — read each lesson on a single page with Pro
Lesson: Semantic and Hybrid Search in Information Extraction
Introduction: The Evolution of Search
In the context of modern information extraction and retrieval-augmented generation (RAG), the ability to find the "right" piece of information within a massive corpus is the difference between a functional system and a hallucinating one. Historically, search systems relied almost exclusively on keyword matching—a process where the system looks for exact string overlaps between a user's query and a document. While efficient, keyword search is notoriously brittle; it fails to understand intent, synonyms, or the conceptual relationships between terms. If a user searches for "automobile maintenance" but the document uses the term "car repair," a traditional keyword system might return nothing at all.
Semantic search changes this paradigm by focusing on the meaning behind the words. By representing text as high-dimensional vectors (embeddings), semantic search systems can identify that "automobile" and "car" are conceptually identical. However, semantic search is not a silver bullet. It often struggles with specific technical jargon, rare product IDs, or acronyms that are highly specific to a niche domain. This is where hybrid search emerges as the industry standard. By combining the precision of keyword-based retrieval (lexical search) with the nuance of semantic search, developers can build systems that are both conceptually aware and factually precise.
This lesson explores how to design, implement, and optimize these pipelines. We will move beyond theory to look at the architecture of vector databases, the mechanics of sparse and dense retrieval, and the strategies for merging these results into a unified, coherent pipeline.
The Components of Modern Retrieval
To understand how to build a search pipeline, we must first break down the two primary retrieval methods. Understanding the strengths and weaknesses of each is critical for determining when to weight one over the other.
1. Keyword-Based (Lexical) Retrieval
Lexical search relies on inverted indices. When you index a document, the system breaks the text into tokens, removes common "stop words," and creates a map of which words appear in which documents. Common algorithms used here include BM25 (Best Matching 25) and TF-IDF (Term Frequency-Inverse Document Frequency).
- Strengths: Extremely fast, highly precise for exact matches (names, serial numbers, specific acronyms), and explainable.
- Weaknesses: Fails on synonyms, does not understand context, and requires exact spelling.
2. Semantic (Dense) Retrieval
Semantic search uses machine learning models, usually Transformer-based architectures like BERT or specialized embedding models, to convert text into numerical vectors. These vectors live in a high-dimensional space where similar concepts are mathematically closer together.
- Strengths: Understands intent, handles synonyms, and captures the "vibe" or context of a query even if the exact keywords are missing.
- Weaknesses: Computationally expensive to generate embeddings, can be "over-general" (returning vaguely relevant but factually incorrect results), and struggles with very specific entities.
Callout: Dense vs. Sparse Vectors In retrieval terminology, dense vectors are the output of embedding models—a long list of floats (e.g., 768 dimensions) where every value contributes to the meaning. Sparse vectors, used in keyword search, are mostly zeros with a few non-zero entries representing the frequency of specific words in the vocabulary. Hybrid search is the process of querying both of these representations simultaneously.
Architecting the Hybrid Search Pipeline
A hybrid search pipeline is not just about running two queries; it is about normalization and fusion. Because BM25 scores (keyword) and Cosine Similarity scores (semantic) operate on different scales, you cannot simply add them together. You must normalize the scores before merging them.
Step 1: Document Preprocessing and Indexing
Before any searching happens, your documents must be indexed in a way that supports both retrieval types. Most modern vector databases (like Weaviate, Pinecone, or Qdrant) allow you to store both the raw text (for keyword search) and the embedding (for semantic search) in the same record.
Step 2: The Retrieval Query
When a user submits a query, your application must perform two parallel actions:
- Lexical Search: Send the raw query string to the BM25 engine.
- Semantic Search: Pass the query string through an embedding model (e.g., OpenAI's
text-embedding-3-smallor an open-source model likebge-large) to generate a query vector, then search the vector database.
Step 3: Reciprocal Rank Fusion (RRF)
This is the standard industry approach for merging results. Instead of using the raw scores (which are incomparable), RRF looks at the rank of the document in each result set. A document that appears at position #1 in both lists is ranked higher than a document that appears at position #1 in one and #50 in the other.
Practical Implementation: A Python Example
Let’s look at how one might implement a simplified hybrid search logic using a standard vector database client pattern.
# Conceptual implementation of a hybrid retrieval function
def hybrid_search(query, vector_db, alpha=0.5):
"""
alpha: Controls the weight between semantic and keyword search.
alpha=1.0 is pure semantic, alpha=0.0 is pure keyword.
"""
# 1. Generate the embedding for the query
query_vector = embedder.encode(query)
# 2. Execute hybrid query using the database's built-in fusion
# Most modern databases handle the scaling internally if configured
results = vector_db.search(
query_vector=query_vector,
query_text=query,
alpha=alpha,
top_k=10
)
return results
# Example usage
results = hybrid_search("How to fix a leaking faucet", db, alpha=0.7)
for doc in results:
print(f"Match: {doc.content} | Score: {doc.score}")
Note: The
alphaparameter is your most important "knob" for tuning. If your domain is highly technical, you might want a lower alpha (favoring keywords). If your domain is conversational or creative, you might want a higher alpha (favoring semantic meaning).
Best Practices for Information Extraction Pipelines
1. Chunking Strategy
The quality of your search is only as good as the quality of your chunks. If your chunks are too large, the semantic embedding becomes "diluted" because it tries to capture too much information in one vector. If chunks are too small, you lose the context necessary for the embedding model to understand the content.
- Best Practice: Use overlapping chunks (e.g., 500 characters with 50-character overlap) to ensure that context at the boundaries of a chunk is preserved.
2. Metadata Filtering
Never rely on search alone for filtering. If your data has attributes like date, user_id, or document_type, use metadata filtering in your vector database. This reduces the search space significantly and prevents the system from returning irrelevant documents from the wrong context.
3. Handling Out-of-Vocabulary (OOV) Terms
Semantic models are trained on general internet text. If your business uses proprietary product codes like "XJ-99-PRO," the embedding model might treat this as noise.
- Solution: Always supplement semantic retrieval with a strong keyword-based fallback for these specific identifiers.
4. Normalization of Scores
If you are building a custom fusion logic rather than using a built-in database feature, you must normalize scores. Min-max normalization is the most common approach:
normalized_score = (score - min) / (max - min)
This ensures that your semantic scores (usually between 0 and 1) and your keyword scores (which can be any positive number) are on a comparable scale before you apply a weighted average.
Common Pitfalls and How to Avoid Them
Pitfall 1: Ignoring the "Curse of Dimensionality"
Many developers assume that "more dimensions equals better." In reality, very high-dimensional vectors can lead to a phenomenon where the distance between all points becomes similar, making retrieval inaccurate. Stick to standard, well-tested embedding models (like 768 or 1536 dimensions) rather than trying to force-fit custom, massive embeddings.
Pitfall 2: Neglecting Query Preprocessing
Users often type queries that are messy. They include typos, conversational filler, or grammatical errors.
- Fix: Before sending a query to your retrieval pipeline, perform basic cleanup. Remove stop words for the keyword search part, but keep them for the semantic part (as models like BERT actually use those words to understand syntax).
Pitfall 3: The "Black Box" Problem
One of the biggest issues with semantic search is that it is hard to explain why a document was returned. If a user asks why a specific document appeared, a keyword system can point to the matching word. A semantic system cannot.
- Strategy: Always log the top 3-5 keywords extracted from the document that contributed to the match if you are using a hybrid approach. This provides a "sanity check" for your retrieval quality.
Warning: Data Leakage When fine-tuning or selecting embedding models, ensure that your evaluation dataset does not overlap with your training data. If your retrieval pipeline is evaluated on the same documents used to train the embedding model, your performance metrics will be artificially high and will fail in production.
Comparison: Retrieval Strategies
| Feature | Keyword (BM25) | Semantic (Dense) | Hybrid Search |
|---|---|---|---|
| Speed | Extremely Fast | Fast (with ANN) | Moderate |
| Synonyms | No | Yes | Yes |
| Exact Matches | Excellent | Poor | Excellent |
| Context Awareness | None | High | High |
| Configuration | Low | High (requires model) | High (requires tuning) |
Step-by-Step: Setting Up a Robust Retrieval Pipeline
- Define your Schema: Identify which fields need to be searchable via text (keywords) and which need to be vectorized. Ensure your vector database is configured to index both.
- Select an Embedding Model: Choose a model based on your language requirements and deployment environment. If you are in a high-security environment, choose a self-hosted model like
e5-large. If you need maximum performance, use a hosted API. - Implement Hybrid Fusion: Use Reciprocal Rank Fusion (RRF) as your default approach. It is mathematically robust and handles the scale differences between BM25 and vector similarity natively.
- Evaluate with "Golden Sets": Create a set of 50-100 questions and the "ideal" document IDs that should be returned. Run your pipeline against this set and calculate the Mean Reciprocal Rank (MRR).
- Iterate on Alpha: Once your pipeline is running, perform an A/B test by varying the
alphaparameter. Measure how the change affects the MRR on your golden set. - Add Re-ranking: If you have the latency budget, add a "Cross-Encoder" re-ranker as the final step. The initial hybrid search retrieves the top 50 documents, and the re-ranker (which is slower but much more accurate) scores those 50 to find the definitive top 5.
Deep Dive: The Role of Re-rankers
While hybrid search is excellent for retrieving a candidate set of documents, it is often not precise enough to pick the absolute best document. This is where the Re-ranking stage comes into play. A re-ranker is a specialized model that takes a query and a document and outputs a relevance score. Unlike a standard embedding model, which encodes the document and query separately, a re-ranker processes them together.
Because the re-ranker sees the query and the document simultaneously, it can identify subtle nuances that the vector model missed. For example, if the query is "How to prevent a leak" and the document is "How to fix a leak," a vector model might rank them as highly similar (because "fix" and "prevent" are often used in similar contexts). A re-ranker, however, will recognize that "prevent" and "fix" have different meanings and will rank the document lower.
Implementation Workflow:
- Retrieval: Use hybrid search to fetch the top 50-100 documents.
- Ranking: Pass these 100 documents through a Cross-Encoder (re-ranker).
- Final Output: Present the top 3-5 results to the user or the LLM.
This two-stage process is the gold standard for high-accuracy information extraction. It balances efficiency (retrieval) with precision (re-ranking).
Addressing Complexity in Information Extraction
When building information extraction solutions, the search pipeline is often just the beginning. The retrieved content must then be grounded. Grounding ensures that the extracted information is cited and verified.
Ensuring Grounding
Even with perfect search, the LLM might hallucinate. To mitigate this:
- Require Citations: Force the LLM to output which specific document chunk provided the information.
- Verify against Source: If the LLM cannot find the answer in the provided context, instruct it to return "I do not have enough information" rather than guessing.
- Strict Context Windowing: Never provide more context than necessary. If you retrieve 20 documents but only 2 are relevant, the LLM will get distracted by the "noise" of the other 18.
Callout: The "Lost in the Middle" Phenomenon Research shows that LLMs are better at paying attention to information at the very beginning and very end of a prompt. If you retrieve 10 chunks, the LLM may ignore the information in the middle. Always place the most relevant documents at the start or end of your prompt context.
Common Questions (FAQ)
Q: Can I use hybrid search without a vector database? A: Yes, you can use traditional search engines like Elasticsearch or OpenSearch, which have built-in support for both BM25 and vector fields. You do not strictly need a dedicated "vector database" if your current infrastructure supports vector indexing.
Q: How do I choose between OpenAI embeddings and open-source models? A: OpenAI embeddings are excellent for general-purpose applications and require zero maintenance. Open-source models (like those on HuggingFace) are necessary if you have strict data privacy requirements or need to fine-tune the model on domain-specific terminology (e.g., medical or legal documents).
Q: Does hybrid search double my storage costs? A: Not necessarily. You are storing the original text and the vector. Most databases are highly optimized for this. The storage cost is usually negligible compared to the computational cost of generating the embeddings during the indexing phase.
Q: How often should I re-index my data? A: This depends on the volatility of your data. If your information changes daily, you need a streaming ingestion pipeline. If your data is static, a batch process once a week is sufficient. Always ensure your search index is updated before you start testing new search configurations.
Key Takeaways
- Hybrid is Essential: Neither keyword nor semantic search is sufficient on its own. Hybrid search provides the best balance of exact-match precision and conceptual understanding.
- Normalization is the Secret Sauce: When merging keyword and semantic results, use Reciprocal Rank Fusion (RRF) to avoid the pitfalls of combining non-comparable score types.
- The Two-Stage Pattern: Use a broad hybrid search for initial retrieval, followed by a cross-encoder re-ranker to achieve maximum precision. This is the industry-standard architecture for high-performance retrieval.
- Chunking Matters: Your retrieval quality is highly dependent on how you break down your source documents. Use overlapping chunks to maintain context across boundaries.
- Metadata Filtering: Always use metadata filters (dates, categories, status) to narrow down the search space before performing heavy-duty vector arithmetic.
- Mind the Context Window: Do not overload your LLM with too much context. Use the re-ranking stage to trim the number of documents passed to the generative model to only the most relevant few.
- Measure with Golden Sets: Do not rely on intuition. Build a small, high-quality evaluation dataset (golden set) and use metrics like MRR to objectively measure the impact of any changes to your retrieval pipeline.
By following these principles, you will move from basic keyword search to a sophisticated information extraction system that is capable of handling complex, real-world queries with high accuracy and reliability. Remember that the goal of these pipelines is not just to find documents, but to provide the LLM with the precise information needed to generate accurate, grounded, and useful answers.
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles your daily points limit so you progress twice as fast, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× daily points limit
- ✓ Distraction-free lessons