Chapter 11

The Smart Clerk: Building World-Class Search

Subhash Choudhary · 9 min read

Our platform was becoming more robust by the day. We had solved stability, conquered scale, and tamed complexity. Our systems were a testament to modern engineering. But a powerful engine is useless if the steering wheel is broken. And for our users, the search bar-one of the most critical tools for e-commerce-was fundamentally broken.

This wasn't a story of servers crashing, but of customer frustration and lost sales. It's the story of how we replaced our "dumb" search bar with an intelligent discovery engine, and in the process, learned that a great platform isn't just about what you build, but how you help your users find it.

Part 1: The Frustration of a Broken Search

The problem didn't appear as an alert on our dashboards. It appeared as a frustrated email from one of our fastest-growing sellers. He owned a large online shoe store on Dukaan and was running a major campaign for the Diwali festival sale. His ad traffic was high, but his sales were mysteriously low.

"My customers are complaining," he wrote. "They search for 'running shoes' and get zero results. I have over 200 different kinds of running shoes! They search for 'sneakers' and find nothing. My bestselling product is the 'Nike Air Zoom Pegasus 38 Men's Running Shoe,' but if someone just types 'Nike Pegasus running shoes,' it doesn't show up. This is a disaster. You're losing me money."

Suumit saw the business impact immediately. We dove into our analytics. The data was damning. The conversion rate for users who used our search bar was a fraction of the rate for users who browsed manually. The search bar wasn't helping users find products; it was actively discouraging them and leading them to dead ends.

Identifying the Problem: The "Dumb" Clerk

The root cause was the simple, naive way we had implemented search in our hurry to build the MVP. When a user typed a query, we were running a basic SQL command against our PostgreSQL database that looked something like this:

SELECT * FROM products WHERE name ILIKE '%running shoes%';

The ILIKE command in Postgres performs a case-insensitive, substring search. It asks the database, "Are the letters 'r-u-n-n-i-n-g- -s-h-o-e-s' present, in that exact order, somewhere inside the product name?"

For a system designed to be a reliable record of transactions, this was a perfectly reasonable query. For a customer trying to find a product, it was a hopelessly "dumb" way to look for things.

Technical Deep Dive: Why a Simple Database Search Fails for E-commerce

Our "dumb clerk" was failing our users in four critical ways:

Furthermore, these LIKE '%...%' queries are notoriously slow and inefficient for a database. They can't use standard indexes effectively and often force the database to perform a "full table scan"-literally reading the name of every single product to find a match. As our sellers' catalogs grew into the thousands, our search was getting slower and slower, putting unnecessary strain on our read replicas.

We came to a clear conclusion. Our search feature wasn't something we could optimize or improve with a few tweaks. The entire approach was fundamentally wrong for an e-commerce platform. We couldn't train our dumb clerk to be smarter. We had to hire a genius.

Part 2: The Genius Clerk

Our old search system was a liability. We couldn't fix it with a few clever SQL tricks; the foundation itself was wrong. We didn't need to retrain our dumb clerk; we needed to fire them and hire a world-renowned, genius research assistant. After a deep dive into the world of search technology, the choice was obvious. We needed Elasticsearch.

Elasticsearch is an open-source, distributed search and analytics engine. It's not a replacement for our PostgreSQL database; it's a highly specialized tool, like a Formula 1 car, designed and built for one purpose: to be blindingly fast and incredibly intelligent at searching through massive amounts of text.

Technical Deep Dive: The Superpowers of Elasticsearch

To understand why Elasticsearch is so good at e-commerce search, you need to understand its core superpowers.

Here's how the genius assistant works its magic:

Superpower #1: The Inverted Index (The Speed Secret) A traditional database, when you search for a word, has to read through every single product description to find matches. Elasticsearch does the opposite. It uses an inverted index.

Before you ever search, Elasticsearch reads all your product data and builds a map that looks like a textbook index. It maps every single word to a list of all the products that contain that word.

"nike" -> [Product 1, Product 5, Product 88]
"running" -> [Product 1, Product 7, Product 23, Product 88]
"shoes" -> [Product 1, Product 23, Product 55, Product 88]

When you search for "nike running shoes," Elasticsearch doesn't read the products. It just looks at its index, finds the lists for "nike," "running," and "shoes," and instantly finds the products that are common to all three lists (in this case, Product 1 and Product 88). This is why it can find matches in millions of documents in milliseconds.

Superpower #2: Advanced Text Analysis (The Intelligence) Elasticsearch doesn't just store the text; it analyzes it to understand its meaning. When we index a product named "Nike's Best Running Shoes," it performs a series of steps:

Superpower #3: Relevance Scoring (The Ranking) This is what separates a good search from a great one. Elasticsearch doesn't just find results; it ranks them by relevance. It uses a sophisticated algorithm (like BM25) that acts like a smart salesperson. It knows that a match in the product_title field is far more important than a match in the product_description. It gives more weight to rarer keywords. The result is that the products the user is most likely to buy always appear at the top.

Superpower #4: Fuzzy Matching (The Typo Forgiveness) Finally, Elasticsearch is built for humans. It can be configured to handle typos and spelling mistakes. If a user searches for "runing sheos," Elasticsearch can be tuned to recognize that this is probably a misspelling of "running shoes" (it measures the "edit distance" between words) and return the correct results. For users typing quickly on a mobile phone, this is a game-changer.

The decision was made. Elasticsearch was the genius clerk we needed to hire. We would build a new, dedicated microservice just for search.

The only remaining question was a familiar one: how do we keep this new, specialized engine perfectly in sync with our main database? The answer, once again, lay in our powerful central nervous system: Kafka.

.

Part 3: The Implementation (Powered by Kafka)

We had found our genius clerk in Elasticsearch. The final piece of the puzzle was to build its office and ensure it received a real-time feed of every single update happening in our main library. We needed to keep the search index perfectly synchronized with our master database.

A few chapters ago, this would have been a complex engineering challenge, requiring a custom-built, fragile pipeline. But now, thanks to our investment in building a central nervous system with Kafka, the solution was incredibly simple and elegant.

The Architecture: A New Listener

Our Kafka and Debezium setup was already the "source of truth" for all changes happening on our platform. We didn't need to build a new data pipeline; we just needed to have our new search service subscribe to the existing one.

That was the entire architectural design. We were plugging a new appliance into the power grid we had already built.

The Data Flow: One Event, Multiple Actions

The new, complete data flow for a product update was a perfect illustration of the power of a decoupled, event-driven architecture.

This system was beautiful. The monolith application, which handled the seller's initial action, had zero knowledge that a search engine or a cache even existed. Its only job was to save data to the database. The downstream systems-caching, search, and in the future, analytics or a recommendation engine-could all independently subscribe to the stream of events and react accordingly. We could add new features powered by these events without ever touching our core application code.

The result was a transformative upgrade to the Dukaan platform. The new search was typo-tolerant, context-aware, and returned highly relevant results in milliseconds. Our sellers were overjoyed; their products were now easily discoverable, and we saw a direct and significant increase in sales originating from the search bar. We had turned a critical weakness into a powerful, best-in-class feature.

Key Takeaways

  • A simple database query (like SQL LIKE) is not a substitute for a real search engine. For a good e-commerce experience, you need a specialized tool.
  • Elasticsearch is a powerful solution that provides the critical features-relevance ranking, typo tolerance, and language analysis-that drive conversions and improve user experience.
  • An event-driven architecture using a tool like Kafka is an incredibly powerful and elegant way to keep disparate systems (like a primary database, a cache, and a search index) perfectly in sync.
  • The "one-to-many" or "fan-out" pattern, where a single event from a producer triggers actions in multiple, independent consumers, is a cornerstone of building a scalable and decoupled microservices architecture.