📖 Scenario: You are building a smart search feature for a small document collection. Users want to find documents by typing keywords or by meaning (semantic search). Combining both methods gives better results.
🎯 Goal: Create a hybrid search system using Langchain that first filters documents by keyword, then ranks them by semantic similarity.
📋 What You'll Learn
Create a list of documents with exact text content
Define a keyword to filter documents
Use Langchain's embedding model to get semantic vectors
Combine keyword filtering and semantic similarity ranking
Return the top matching documents
💡 Why This Matters
🌍 Real World
Hybrid search is used in apps like document search, customer support, and knowledge bases to find relevant info quickly by combining exact word matches and meaning.
💼 Career
Understanding hybrid search with Langchain is valuable for roles in AI, data science, and software development focused on search engines and natural language processing.
Progress0 / 4 steps
1
Create the document list
Create a list called documents with these exact strings: 'Langchain is a framework for building applications', 'Semantic search finds meaning', 'Keyword search matches exact words', 'Hybrid search combines both methods'.
LangChain
Hint
Use a Python list with the exact strings given.
2
Set the keyword filter
Create a variable called keyword and set it to the string 'search' to filter documents containing this word.
LangChain
Hint
Assign the exact string 'search' to the variable keyword.
3
Filter documents by keyword and embed
Import OpenAIEmbeddings from langchain.embeddings. Create an instance called embedding_model. Filter documents to keep only those containing keyword (case-insensitive) into filtered_docs. Then create a list embedded_docs by applying embedding_model.embed_query() to each document in filtered_docs.
LangChain
Hint
Use list comprehension to filter and embed documents. Remember to import the embedding class first.
4
Rank filtered documents by semantic similarity
Import cosine_similarity from sklearn.metrics.pairwise. Create a variable query_embedding by embedding the string 'hybrid search' using embedding_model.embed_query(). Compute a list similarities by calculating cosine similarity between query_embedding and each vector in embedded_docs. Create a list ranked_docs by sorting filtered_docs in descending order of similarity using similarities. The final code should combine keyword filtering and semantic ranking.
LangChain
Hint
Use cosine similarity to rank documents by meaning. Sort documents by similarity descending.
Practice
(1/5)
1. What is the main advantage of using hybrid search (keyword + semantic) in Langchain?
easy
A. It combines exact word matching with understanding related meanings for better results.
B. It only uses keyword matching for faster searches.
C. It replaces semantic search completely with keyword search.
D. It ignores the meaning of words and focuses on spelling.
Solution
Step 1: Understand keyword search
Keyword search finds exact matches of words typed by the user.
Step 2: Understand semantic search
Semantic search finds results related in meaning, even if words differ.
Final Answer:
It combines exact word matching with understanding related meanings for better results. -> Option A
Quick Check:
Hybrid search = keyword + semantic [OK]
Hint: Remember hybrid means mixing two methods for best results [OK]
Common Mistakes:
Thinking hybrid search only uses keywords
Believing semantic search ignores meaning
Confusing hybrid search with only semantic search
2. Which of the following is the correct way to initialize a hybrid search in Langchain combining keyword and semantic search?
easy
A. HybridSearch(semantic_searcher, keyword_searcher)
B. HybridSearch(keyword_searcher, semantic_searcher)
C. HybridSearch(keyword_searcher + semantic_searcher)
D. HybridSearch(keyword_searcher * semantic_searcher)
Solution
Step 1: Check constructor parameters
HybridSearch expects two separate searcher objects as arguments.
Step 2: Confirm order and syntax
The correct syntax is HybridSearch(keyword_searcher, semantic_searcher) with comma separation.
Final Answer:
HybridSearch(keyword_searcher, semantic_searcher) -> Option B
A. Missing comma between keyword_searcher and semantic_searcher; add a comma.
B. search method does not exist; rename to find.
C. HybridSearch cannot take two arguments; combine them first.
D. The string 'banana' should be a list; change to ['banana'].
Solution
Step 1: Identify syntax error in constructor
There is no comma between keyword_searcher and semantic_searcher, causing a syntax error.
Step 2: Fix by adding comma
Insert a comma to separate the two arguments correctly.
Final Answer:
Missing comma between keyword_searcher and semantic_searcher; add a comma. -> Option A
Quick Check:
Arguments must be comma-separated [OK]
Hint: Check commas between arguments to avoid syntax errors [OK]
Common Mistakes:
Forgetting commas between function arguments
Assuming method name is wrong
Changing input type unnecessarily
5. You want to improve search results by weighting keyword matches twice as much as semantic similarity in Langchain's hybrid search. Which approach correctly applies this weighting?
hard
A. Use only keyword_searcher to get stronger keyword matches.
B. Multiply semantic_searcher results by 2 before combining.
C. Swap keyword_searcher and semantic_searcher order in HybridSearch constructor.
D. Use HybridSearch(keyword_searcher, semantic_searcher, weights=[2,1]) to set weights.
Solution
Step 1: Understand weighting in hybrid search
HybridSearch can accept weights to prioritize one search type over another.