Sub topic 5
Choosing a search engine
This chapter assumes you know basic SQL, REST APIs, and the idea of replication. It also assumes you have read the earlier chapters on inverted indexes, text analysis, and BM25. The question here is which kind of engine to use once you have decided search needs more than a plain LIKE query.
The three realistic options
Most designs choose among three options.
- Full-text search inside your database. PostgreSQL and MySQL both have it. You keep one datastore and no sync pipeline.
- A self-managed search engine. Elasticsearch, OpenSearch, and Apache Solr are built on Lucene, a library that builds and queries inverted indexes. Typesense and Meilisearch are lighter engines that aim for easy setup.
- A managed search service. Examples are Amazon OpenSearch Service, Elastic Cloud, and Algolia. The vendor runs the cluster, and you pay for that convenience.
Start with the database
Database full-text search often covers a first version. Here is PostgreSQL:
CREATE INDEX products_fts ON products
USING GIN (to_tsvector('english', name));
SELECT id, name
FROM products
WHERE to_tsvector('english', name) @@ plainto_tsquery('english', 'running shoes')
ORDER BY ts_rank(to_tsvector('english', name),
plainto_tsquery('english', 'running shoes')) DESC
LIMIT 10;The GIN index is an inverted index. Search results are always as fresh as the table, because there is no second copy of the data to keep in sync. For a few hundred thousand rows with simple queries, this is hard to beat.
Where databases fall short
A dedicated engine pays off when one or more of these needs appear:
- Relevance tuning beyond basic ranking, such as field boosts, synonyms, and custom scoring.
- Facets (counts per category or brand) computed over large result sets.
- Typo tolerance and autocomplete.
- Search load that would compete with your transactional queries.
- Data volume that needs the index spread across several machines.
A dedicated engine also costs you something. It is a second copy of your data, so you must build and monitor a sync pipeline and accept eventual consistency, as later chapters cover.
Comparing the options
| Factor | Database full-text | Self-managed engine | Managed service |
|---|---|---|---|
| Operational effort | None extra | High | Low |
| Data freshness | Immediate | Delayed by sync | Delayed by sync |
| Relevance control | Limited | Extensive | Varies by product |
| Facets and typo tolerance | Weak | Strong | Strong |
| Horizontal scaling | Limited | Built in | Built in |
| Cost profile | Cheapest | Staff time | Per-usage fees |
A decision path
Common mistakes
- Treating the search engine as the source of truth. Engines are optimized for queries, not for durable transactional storage. Keep the primary database authoritative and rebuild the index from it when needed.
- Choosing by popularity alone. A small catalog with simple queries does not need a multi-node cluster.
- Ignoring licensing. Elasticsearch and OpenSearch have different licenses and diverging features. Check which one your organization and cloud provider allow.
- Underestimating operations. Cluster upgrades, shard sizing, and monitoring are real work. If nobody owns them, prefer a managed service.
Exercise
A team runs a 200,000-row product table in PostgreSQL. They need keyword search on product names and have no one with search-cluster experience. Which option should they pick first, and what would make them change their mind?
Show answer
Start with PostgreSQL full-text search. It needs no sync pipeline, results are always fresh, and no one has to operate a cluster. They should move to a managed search service if they later need faceted counts, typo tolerance, heavy relevance tuning, or search load that slows down transactional queries.