Bird
Raised Fist0
LangChainframework~10 mins

Hybrid search (keyword + semantic) in LangChain - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Hybrid search (keyword + semantic)
User Query Input
↓
Keyword Search
↓
Semantic Search
↓
Combine Results
↓
Rank & Return Final Results
The user query is processed by both keyword and semantic search, then results are combined and ranked before showing.
Execution Sample
LangChain
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings
from langchain.retrievers import BM25Retriever

# Assume bm25 (BM25Retriever) and vectorstore (FAISS) are initialized
# Perform keyword and semantic search
results_keyword = bm25.get_relevant_documents(query)
results_semantic = vectorstore.similarity_search(query, k=3)
This code runs keyword and semantic searches on the same query and collects top 3 results from each.
Execution Table
StepActionInputOutputNotes
1Receive user query"climate change impact"Query storedStart with raw user input
2Run keyword search"climate change impact"Top 3 docs matching keywordsFind docs with exact words
3Run semantic search"climate change impact"Top 3 docs by meaningFind docs with similar meaning
4Combine resultsKeyword + Semantic results6 docs combinedMerge both result sets
5Rank combined results6 docsFinal ranked listSort by relevance score
6Return resultsFinal ranked listDisplayed to userShow best matches
7ExitN/ASearch completeProcess ends
💡 All steps complete, final ranked results returned to user
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
query"climate change impact""climate change impact""climate change impact""climate change impact""climate change impact""climate change impact"
results_keywordNone[Doc1, Doc2, Doc3][Doc1, Doc2, Doc3][Doc1, Doc2, Doc3][Doc1, Doc2, Doc3][Doc1, Doc2, Doc3]
results_semanticNoneNone[Doc4, Doc5, Doc6][Doc4, Doc5, Doc6][Doc4, Doc5, Doc6][Doc4, Doc5, Doc6]
combined_resultsNoneNoneNone[Doc1, Doc2, Doc3, Doc4, Doc5, Doc6][Doc1, Doc2, Doc3, Doc4, Doc5, Doc6][Doc1, Doc2, Doc3, Doc4, Doc5, Doc6]
final_resultsNoneNoneNoneNone[Doc2, Doc4, Doc1, Doc5, Doc3, Doc6][Doc2, Doc4, Doc1, Doc5, Doc3, Doc6]
Key Moments - 3 Insights
Why do we run both keyword and semantic searches instead of just one?
Because keyword search finds exact word matches (see Step 2 in execution_table), while semantic search finds documents with similar meaning even if words differ (Step 3). Combining both gives better coverage.
How are the combined results ranked before returning?
After merging keyword and semantic results (Step 4), they are ranked by relevance scores that consider both keyword matches and semantic similarity (Step 5). This ensures the best overall matches appear first.
What happens if the same document appears in both keyword and semantic results?
Duplicates are removed during combination (Step 4), so each document appears only once in the final ranked list.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after Step 3?
ATop 3 docs matching keywords
BTop 3 docs by meaning
CFinal ranked list
DCombined 6 docs
💡 Hint
Check the Output column for Step 3 in execution_table
At which step are the keyword and semantic results combined?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the action 'Combine results' in execution_table
If the query changes, which variable in variable_tracker updates first?
Aresults_keyword
Bcombined_results
Cquery
Dfinal_results
💡 Hint
See variable_tracker, 'query' is set at Start and remains constant
Concept Snapshot
Hybrid search combines keyword and semantic search.
Keyword search finds exact word matches.
Semantic search finds meaning-based matches.
Results are merged and ranked by relevance.
This approach improves search accuracy and recall.
Full Transcript
Hybrid search in Langchain means using both keyword and semantic search on the same user query. First, the query is taken as input. Then keyword search finds documents containing the exact words. Semantic search finds documents with similar meaning even if words differ. Both result sets are combined and duplicates removed. The combined list is ranked by relevance scores considering both methods. Finally, the best results are returned to the user. This method improves finding relevant documents by covering both exact matches and related meanings.

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

  1. Step 1: Understand keyword search

    Keyword search finds exact matches of words typed by the user.
  2. Step 2: Understand semantic search

    Semantic search finds results related in meaning, even if words differ.
  3. Final Answer:

    It combines exact word matching with understanding related meanings for better results. -> Option A
  4. 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

  1. Step 1: Check constructor parameters

    HybridSearch expects two separate searcher objects as arguments.
  2. Step 2: Confirm order and syntax

    The correct syntax is HybridSearch(keyword_searcher, semantic_searcher) with comma separation.
  3. Final Answer:

    HybridSearch(keyword_searcher, semantic_searcher) -> Option B
  4. Quick Check:

    Correct syntax uses comma-separated searchers [OK]
Hint: Use commas to separate searchers, not operators [OK]
Common Mistakes:
  • Using + or * instead of comma to combine searchers
  • Swapping the order of keyword and semantic searchers
  • Passing a single combined object instead of two
3. Given the following code snippet in Langchain, what will be the output type of results?
hybrid_search = HybridSearch(keyword_searcher, semantic_searcher)
results = hybrid_search.search('apple')
medium
A. A single document matching the keyword exactly
B. A boolean indicating if 'apple' exists in the database
C. A list of documents ranked by combined keyword and semantic relevance
D. An error because HybridSearch has no search method

Solution

  1. Step 1: Understand HybridSearch.search()

    The search method returns multiple documents ranked by relevance.
  2. Step 2: Combine keyword and semantic results

    Hybrid search merges keyword and semantic scores to rank documents.
  3. Final Answer:

    A list of documents ranked by combined keyword and semantic relevance -> Option C
  4. Quick Check:

    Hybrid search returns ranked document list [OK]
Hint: Hybrid search returns ranked lists, not single items [OK]
Common Mistakes:
  • Expecting a single document instead of a list
  • Thinking it returns a boolean
  • Assuming HybridSearch lacks a search method
4. You wrote this code but get an error:
hybrid_search = HybridSearch(keyword_searcher semantic_searcher)
results = hybrid_search.search('banana')
What is the error and how to fix it?
medium
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

  1. Step 1: Identify syntax error in constructor

    There is no comma between keyword_searcher and semantic_searcher, causing a syntax error.
  2. Step 2: Fix by adding comma

    Insert a comma to separate the two arguments correctly.
  3. Final Answer:

    Missing comma between keyword_searcher and semantic_searcher; add a comma. -> Option A
  4. 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

  1. Step 1: Understand weighting in hybrid search

    HybridSearch can accept weights to prioritize one search type over another.
  2. Step 2: Apply weights parameter correctly

    Passing weights=[2,1] gives keyword search double importance compared to semantic search.
  3. Final Answer:

    Use HybridSearch(keyword_searcher, semantic_searcher, weights=[2,1]) to set weights. -> Option D
  4. Quick Check:

    Weights parameter controls search importance [OK]
Hint: Use weights parameter to prioritize keyword or semantic search [OK]
Common Mistakes:
  • Trying to multiply results manually before combining
  • Swapping searchers without weighting
  • Using only one searcher loses hybrid benefits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard