Search Engines vs Relational Databases
A shopper types "gardning for busy" into the bookshop's search box. A relational database with WHERE title = 'gardning for busy' finds nothing — the spelling is wrong and the words are incomplete. A search engine is built for exactly this: finding the most relevant matches, ranked, even when the question is messy.
The rule
A database answers "which rows match?"; a search engine answers "which match best?"
A relational database such as PostgreSQL is built around exact truth. Tables have a fixed schema, rows are linked by keys, and transactions guarantee that a payment and its order are saved together or not at all. A query either matches a row or it does not.
Elasticsearch is a search engine built on the Lucene library. It stores JSON documents and, when it saves text, it breaks it into words (lower-cased, split on spaces and punctuation) and records which documents contain each word. A search then scores every candidate document for relevance and returns them best first.
| Question | Relational DB | Elasticsearch |
|---|---|---|
| Unit of data | Row in a table | JSON document in an index |
| Text search | LIKE '%garden%', slow, no ranking |
Word lookup, fast, ranked by relevance |
| Transactions across records | Yes | No |
| Joins | Yes | Very limited |
| New data visible | Immediately after commit | After a refresh, about 1 second |
That last row matters: Elasticsearch is near real-time. A newly indexed book becomes searchable after the next refresh, which happens every second by default.
The shortcut
Ask one question about the feature: does it need correctness or relevance? Money, stock counts and user accounts need correctness — keep them in the relational database as the source of truth. Search boxes, log exploration and "similar items" need relevance — copy the data into Elasticsearch and search there.
Orders and payments
They must never be half-saved or lost. Correctness → relational database.
The trap is treating Elasticsearch as your only database. It can store documents safely, but it has no multi-document transactions, so it is a poor home for data where two changes must succeed together.
Drills
Answer fast — trust the rule.
A shopper searches "quiet gardens". Which system can naturally return "The Quiet Garden" ranked first?
Which job is the best fit for a relational database rather than Elasticsearch?
You index a new book and search for it immediately, but it is missing. Then, a second later, it appears. Why?
What does Elasticsearch store as its basic unit of data?
Remember
- A database answers "which rows match?"; a search engine answers "which match best?"
- Correctness → relational database; relevance → Elasticsearch.
- Elasticsearch is near real-time and has no multi-document transactions.
