Precision Matters.

Yesterday, we killed the Cron Job and moved to real-time ingestion. Now, we have data flowing into our Vector DB instantly.

But we have a new problem. A user asks: "How do I fix error code XJ-900?"

Your RAG system returns a generic document about "System Troubleshooting," but it completely misses the exact manual page for "XJ-900."

Why? Because Vector Search (Semantic Search) is terrible at Keywords.

"XJ-900" has no semantic meaning in the English language. To an embedding model, it looks like noise. Today, we fix this by implementing Hybrid Search.

1. THE CONCEPT: Dense vs. Sparse

To build a production-grade retrieval system, you need two brains.

1. The Right Brain (Dense Vectors):

  • What it does: Understands meaning and intent.

  • Example: Query "My internet is dead" matches Document "Wifi router troubleshooting" (even though they share no words).

  • The Tech: OpenAI text-embedding-3-small (creates an array of floats).

2. The Left Brain (Sparse Vectors / BM25):

  • What it does: Matches exact keywords.

  • Example: Query "XJ-900" matches Document "XJ-900 Manual".

  • The Tech: BM25 (Term Frequency-Inverse Document Frequency).

The Trap:

Most tutorials only teach you Dense Vector search. This is why your bot feels "dreamy" but lacks precision.

You need Hybrid Search: A weighted combination of both.

2. THE CODE: Implementing Hybrid Search

Most modern Vector DBs (Pinecone, Weaviate, Qdrant) now support this natively. You don't need to manage a separate Elasticsearch cluster anymore.

Here is the pattern using the Pinecone syntax (conceptually similar for others):

# The "Alpha" slider controls the balance.
# alpha = 1.0 (Pure Semantic)
# alpha = 0.0 (Pure Keyword)
# alpha = 0.5 (Hybrid)

def search_knowledge_base(user_query, alpha=0.3):
    # 1. Generate Dense Vector (Semantic)
    dense_vector = openai.embeddings.create(input=user_query)
    
    # 2. Generate Sparse Vector (BM25 Keywords)
    # This tokenizes the query into specific keywords with weights
    sparse_vector = bm25_encoder.encode_queries(user_query)

    # 3. The Hybrid Query
    results = index.query(
        vector=dense_vector,
        sparse_vector=sparse_vector,
        top_k=5,
        include_metadata=True
    )
    
    return results

The Engineering Takeaway: If your users search for Part Numbers, SKUs, Acronyms, or Error Codes, and you are only using Vector Search, your system is broken. Dial down your alpha. Bring back the keywords.

3. THE CEREBRAL GYM: Solutions & Whiteboarding

Yesterday's Solution (The Zombie Transaction)

The Challenge: Your Postgres query hangs forever. You suspect a ghost lock. How do you find the PID?

The Command:

SELECT * FROM pg_stat_activity 
WHERE state = 'active' 
AND wait_event_type = 'Lock';

This view shows you exactly which queries are running, which are blocked, and the pid (Process ID) you need to pg_terminate_backend(pid) to kill them.

Today's Puzzle (Python Traps)

We are back to code.

The Scenario: You write a function to log user events. You want to use an empty list as a default argument if no events are passed.

def log_event(event, event_list=[]):
    event_list.append(event)
    return event_list

# User 1
print(log_event("Login")) 
# Output: ['Login'] -- Looks good.

# User 2
print(log_event("Logout"))
# Output: ['Login', 'Logout'] -- WAIT. WHAT?

The Question: Why did User 2 inherit User 1's data? And what is the standard "pythonic" pattern to fix this memory leak?

(Reply with the corrected code snippet!)

4. THE PULSE - Latest News and

  • Gambo AI → It turns your one-paragraph game idea into a playable game

  • SoftBank invests $40 billion in OpenAI: SoftBank has completed its investment in OpenAI, making a final payment of about $22–$22.5 billion and bringing its total commitment to roughly $41 billion for an estimated 11% stake. The funding will support OpenAI’s long-term infrastructure plans and AI partnerships.

  • Yume → A magical dream journal app

  • Nvidia’s AI Empire: A deep dive into Nvidia’s latest startup investments

5. THE LATENT SPACE

"History doesn't repeat itself, but it rhymes."

Mark Twain

In 2010, we used Solr/Elasticsearch for keywords (BM25). In 2023, we threw it all away for Vector Databases. In 2026, the hottest feature in Vector Databases is... BM25 support.

We are re-learning that "new" doesn't always mean "replacement." Sometimes it just means "augmentation." Don't be afraid to use "old" tech like keyword search. It works for a reason.

See you tomorrow.
Harsh Kathiriya - Query & Context

Keep Reading