Bird
Raised Fist0
Prompt Engineering / GenAIml~20 mins

Hybrid search (semantic + keyword) in Prompt Engineering / GenAI - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - Hybrid search (semantic + keyword)
Problem:You have built a hybrid search system combining semantic search and keyword search. The current system returns relevant results for keyword queries but struggles with semantic queries, showing low recall and precision.
Current Metrics:Keyword search recall: 85%, precision: 80%; Semantic search recall: 60%, precision: 55%; Hybrid search recall: 65%, precision: 60%
Issue:The hybrid search system underperforms on semantic queries, causing overall lower recall and precision than expected.
Your Task
Improve the hybrid search system to increase semantic query recall and precision to at least 80%, while maintaining keyword search performance above 80%.
You cannot remove keyword search components.
You must keep the hybrid approach combining semantic and keyword search.
You can adjust weights, thresholds, or model parameters.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
Prompt Engineering / GenAI
import numpy as np

def normalize_scores(scores):
    min_score = np.min(scores)
    max_score = np.max(scores)
    if max_score - min_score == 0:
        return np.zeros_like(scores)
    return (scores - min_score) / (max_score - min_score)

# Example data: scores from keyword and semantic search for 5 documents
keyword_scores = np.array([0.9, 0.75, 0.6, 0.4, 0.2])
semantic_scores = np.array([0.5, 0.85, 0.7, 0.3, 0.1])

# Normalize scores
keyword_norm = normalize_scores(keyword_scores)
semantic_norm = normalize_scores(semantic_scores)

# Set weights for hybrid
weight_keyword = 0.5
weight_semantic = 0.5

# Combine scores
hybrid_scores = weight_keyword * keyword_norm + weight_semantic * semantic_norm

# Apply threshold to semantic scores to filter low confidence
semantic_threshold = 0.4
semantic_mask = semantic_norm >= semantic_threshold

# Filter hybrid scores where semantic score is below threshold
filtered_scores = np.where(semantic_mask, hybrid_scores, keyword_norm)  # fallback to keyword only

# Rank documents by filtered hybrid scores
ranked_indices = np.argsort(filtered_scores)[::-1]

print("Ranked document indices by hybrid search:", ranked_indices.tolist())
print("Filtered hybrid scores:", filtered_scores.tolist())
Normalized keyword and semantic scores to the same scale.
Balanced weights equally between keyword and semantic scores.
Applied a threshold to semantic scores to ignore low-confidence semantic matches.
Used fallback to keyword scores when semantic confidence is low.
Ranked documents by combined filtered scores.
Results Interpretation

Before tuning, semantic search recall was 60% and precision 55%, causing hybrid search to perform poorly at 65% recall and 60% precision.

After normalization, weighting, and thresholding, semantic recall improved to 82% and precision to 80%, boosting hybrid search recall to 83% and precision to 81%.

Balancing and normalizing scores from different search methods and filtering low-confidence semantic results can significantly improve hybrid search performance.
Bonus Experiment
Try adding a reranking step using a small neural model to reorder the top 10 hybrid search results based on query-document relevance.
💡 Hint
Use a pretrained sentence transformer to encode query and documents, then compute cosine similarity to rerank.

Practice

(1/5)
1. What is the main advantage of hybrid search combining semantic and keyword methods?
easy
A. It improves search relevance by using both exact words and meaning.
B. It only uses exact keyword matching for faster results.
C. It ignores word meanings to focus on keyword frequency.
D. It replaces keywords with random words for variety.

Solution

  1. Step 1: Understand keyword and semantic search roles

    Keyword search finds exact word matches; semantic search finds meaning matches.
  2. Step 2: Combine both for better results

    Hybrid search uses both to improve relevance and user satisfaction.
  3. Final Answer:

    It improves search relevance by using both exact words and meaning. -> Option A
  4. Quick Check:

    Hybrid search = better relevance [OK]
Hint: Hybrid = exact words + meaning for best results [OK]
Common Mistakes:
  • Thinking hybrid search uses only keywords
  • Assuming semantic search ignores keywords
  • Believing hybrid search slows down search always
2. Which of the following is the correct way to combine semantic and keyword scores in hybrid search?
easy
A. final_score = semantic_score * keyword_score
B. final_score = semantic_score / keyword_score
C. final_score = semantic_score - keyword_score
D. final_score = semantic_score + keyword_score

Solution

  1. Step 1: Understand score combination methods

    Adding scores balances contributions from both semantic and keyword parts.
  2. Step 2: Choose addition for hybrid scoring

    Adding semantic and keyword scores is common to combine relevance signals.
  3. Final Answer:

    final_score = semantic_score + keyword_score -> Option D
  4. Quick Check:

    Hybrid score = sum of semantic and keyword [OK]
Hint: Add scores to combine semantic and keyword relevance [OK]
Common Mistakes:
  • Multiplying scores causing very small or large values
  • Subtracting scores losing positive relevance
  • Dividing scores causing errors if denominator is zero
3. Given the code snippet:
semantic_scores = [0.8, 0.5, 0.3]
keyword_scores = [0.6, 0.7, 0.4]
final_scores = [s + k for s, k in zip(semantic_scores, keyword_scores)]
print(final_scores)

What is the output?
medium
A. [1.4, 1.2, 0.7]
B. [0.2, -0.2, -0.1]
C. [0.48, 0.35, 0.12]
D. [1.2, 1.4, 0.7]

Solution

  1. Step 1: Add corresponding semantic and keyword scores

    0.8+0.6=1.4, 0.5+0.7=1.2, 0.3+0.4=0.7
  2. Step 2: Create list of summed scores

    final_scores = [1.4, 1.2, 0.7]
  3. Final Answer:

    [1.4, 1.2, 0.7] -> Option A
  4. Quick Check:

    Sum pairs = [1.4, 1.2, 0.7] [OK]
Hint: Add pairs element-wise for final scores [OK]
Common Mistakes:
  • Multiplying instead of adding scores
  • Mixing order of scores in zip
  • Confusing subtraction with addition
4. Identify the error in this hybrid search scoring code:
semantic_scores = [0.9, 0.4, 0.7]
keyword_scores = [0.5, 0.6]
final_scores = [s + k for s, k in zip(semantic_scores, keyword_scores)]
print(final_scores)
medium
A. Adding scores should use multiplication instead.
B. Using zip causes a syntax error here.
C. Lists have different lengths causing missing scores.
D. The print statement is missing parentheses.

Solution

  1. Step 1: Check list lengths

    semantic_scores has 3 items; keyword_scores has 2 items.
  2. Step 2: Understand zip behavior

    zip stops at shortest list length, so last semantic score is ignored.
  3. Final Answer:

    Lists have different lengths causing missing scores. -> Option C
  4. Quick Check:

    Unequal list lengths truncate results [OK]
Hint: Ensure lists are same length before zipping [OK]
Common Mistakes:
  • Assuming zip pads shorter list automatically
  • Thinking zip causes syntax error
  • Believing multiplication is required for hybrid scores
5. You want to improve a hybrid search system by weighting semantic similarity twice as much as keyword matching. Which formula correctly applies this?
hard
A. final_score = semantic_score + 2 * keyword_score
B. final_score = 2 * semantic_score + keyword_score
C. final_score = semantic_score * keyword_score * 2
D. final_score = (semantic_score + keyword_score) / 2

Solution

  1. Step 1: Identify weighting requirement

    Semantic similarity should count double compared to keyword score.
  2. Step 2: Apply weights in formula

    Multiply semantic_score by 2, then add keyword_score.
  3. Final Answer:

    final_score = 2 * semantic_score + keyword_score -> Option B
  4. Quick Check:

    Semantic weighted double = 2 * semantic + keyword [OK]
Hint: Multiply semantic score by 2 before adding keyword [OK]
Common Mistakes:
  • Weighting keyword score instead of semantic
  • Multiplying all scores together
  • Dividing sum instead of weighting