AI / WhatsApp CRM

Hybrid Search for WhatsApp CRM: pgvector, Postgres FTS, pg_trgm

Never answer about the wrong customer — hybrid search that handles exact, semantic, keyword, and typo queries.

November 20269 min read

Hybrid Search for WhatsApp CRM: pgvector, Postgres FTS, pg_trgm

A WhatsApp CRM AI agent must never answer about the wrong customer. If a user asks about "order INV-10452", the agent must not confuse it with "INV-1045" or a similar invoice from another customer.

Typical semantic search alone fails here — vector similarity might return a loosely related document instead of the exact record. The fix is hybrid search: four search modes merged into one answer.

The four search modes

| Mode | Best for | Technology | |------|----------|------------| | Exact | Order, invoice, ticket numbers | Direct equality lookup | | Semantic | "How do I reset my password?" | pgvector embeddings | | Keyword | "refund policy shipping" | PostgreSQL full-text search | | Typo | "refnd policcy" | pg_trgm trigram similarity |

Exact number lookup — the guardrail

Order, invoice, and ticket numbers are always looked up with exact equality. Never fuzzy-matched. This guarantees the agent never discusses the wrong record:

```sql
SELECT * FROM orders WHERE order_number = :exact_number;
```

If an exact match exists, it takes priority over all other results.

Semantic search with pgvector

For natural-language questions, pgvector stores embeddings of knowledge-base articles and returns the most similar:

```sql
SELECT *, embedding <=> :query_embedding AS distance
FROM knowledge_base
ORDER BY distance
LIMIT 5;
```

This handles questions like "how do I change my plan?" where the words don't match the article title exactly.

PostgreSQL full-text search

For keyword queries ("refund policy"), Postgres FTS with `to_tsvector` and `ts_rank` ranks documents by term frequency and proximity:

```sql
SELECT *, ts_rank_cd(document, query) AS rank
FROM knowledge_base, plainto_tsquery('english', :query) AS query
WHERE document @@ query
ORDER BY rank DESC
LIMIT 5;
```

Typo tolerance with pg_trgm

Customers mistype: "refnd", "policcy", "shpping". pg_trgm trigram similarity catches these:

```sql
SELECT *, similarity(name, :query) AS sim
FROM knowledge_base
WHERE similarity(name, :query) > 0.3
ORDER BY sim DESC
LIMIT 5;
```

Merging results

All four searches run in parallel. Results are merged with this priority:

1. Exact number match (highest — always wins if present). 2. Semantic search results. 3. Full-text search results. 4. Trigram similarity results (lowest).

Duplicate articles are deduplicated by ID. The top 3 results are passed to the LLM as context.

Leak guard

Before the agent replies, a leak guard scans the answer for:

- API keys and secrets. - Internal instructions or system prompts. - Other customers' data.

If a leak is detected, the reply is blocked and the chat is handed to a human agent.

Results

WACRM's AI reply agent uses this hybrid search across a team knowledge base. Agents never confuse order records, handle typos gracefully, and never leak internal information.

Bottom line

Semantic search alone is not enough for business-critical data. Combine exact lookup, vector search, full-text search, and typo tolerance — with exact match as the guardrail.

Building a WhatsApp CRM or AI agent? [Get in touch](/contact).

Related articles

Want similar work?

I build RAG systems, FastAPI backends, AI agents, and licensing systems for teams worldwide. Let’s discuss your project.

Get in touch