Video in progress

Staff 🌱 Spring

A product catalog needs full-text search. Is PostgreSQL full-text search enough, or do you add Elasticsearch/OpenSearch?

For a few million records, simple relevance and a single language, PostgreSQL FTS and pg_trgm are often enough and remove the sync problem. With facets, complex relevance, multiple languages, typo tolerance and heavy load, you need a separate search system and a reliable indexing pipeline.

📖 Answer

Adding a separate search cluster means a new copy of the data. With it comes a whole class of problems: "it's in the database but not in search". So I first check what the database can do.

With PostgreSQL:

ALTER TABLE product ADD COLUMN search tsvector
  GENERATED ALWAYS AS (to_tsvector('simple', coalesce(name,'') || ' ' || coalesce(brand,''))) STORED;
CREATE INDEX ix_product_search ON product USING gin(search);
CREATE INDEX ix_product_name_trgm ON product USING gin(name gin_trgm_ops); -- for misspelled entries

The upsides: it is transactional (once written, it is found immediately), there is no extra infrastructure, and filters and search go in one query. But there is no ready-made stemmer for Uzbek (the simple configuration), tuning relevance is limited, faceted search gets heavy at large volume, and the search load lands on the main database.

When to use Elasticsearch/OpenSearch:

  • facets ("brand: 12, price range: ..."), autocomplete, synonyms, multilingual analyzers;
  • the business tunes relevance (popularity, availability, boosts);
  • search traffic is putting pressure on the database.

If you choose a separate system, the main work is synchronization:

  • "Write to the database, then write to ES" inside the application is bad: if the second step fails, the difference stays forever.
  • Outbox or CDC (Debezium) → Kafka → an indexing service. Make it idempotent: write the document by id, and use a version so an old one doesn't overwrite a new one.
  • Have a path for a full reindex: build a new index and swap the alias (blue/green). You'll need it when the mapping changes and when differences pile up.
  • Monitoring: indexing lag and a periodic comparison with the database (counts, random samples).

On the Spring side: Spring Data Elasticsearch is convenient, but for complex queries I often use the official Java client directly, because the abstraction hides the finer search settings.

The decision: starting with PostgreSQL and splitting search out only when a clearly measured need appears is often the cheapest path.