~/antonio-paya
ES
CasesSearch

// 20 · architecture

Search and catalogue

A denormalised search index, fed by change data capture from the database, that delivers relevance, facets, autocomplete and hybrid vector search without becoming a second source of truth.

Code and layout on GitHub ↗

What it must guarantee

  • The database is the source of truth; the index can be rebuilt from it in full at any time.
  • A database change shows up in results within seconds or minutes, as agreed, without dual writes from the application.
  • Changing the index schema or analysers never leaves search down or half-populated.
  • Results honour permissions and visibility (tenant, published, in stock) exactly as the application does.
  • Queries, zero-result searches and clicks are recorded to measure and tune relevance.
UsersSearch box · filtersSearch box · filtersSearch box · filtersSearch box · filtersSearch APIKubernetes (RKE2/k3s)ECS FargateContainer AppsCloud RunSource-of-truth DBPostgreSQL + PatroniAurora PostgreSQLAzure DB for PostgreSQLCloud SQL (PostgreSQL)Query cacheValkeyElastiCacheAzure Managed RedisMemorystoreSearch engineOpenSearch / MeilisearchOpenSearch ServiceAzure AI SearchAgent SearchCDC + indexerDebezium + workerOpenSearch IngestionWorker (push API)Cloud Run workerSearch analyticsOpenSearch DashboardsOpenSearch DashboardsApplication InsightsSearch analyticschangesreindexes

On-premises The API on Kubernetes queries OpenSearch (or Meilisearch for small catalogues), Debezium reads the PostgreSQL WAL and a worker indexes; Valkey caches queries and OpenSearch Dashboards shows the analytics.

  • OpenSearch is Apache 2.0 and governed by the OpenSearch Software Foundation; Elasticsearch once again has an open source licence option (AGPL) alongside Elastic's own. Choose on features and operations, not licence alone.
  • Debezium needs a logical replication slot: if the connector stops, PostgreSQL retains WAL until the disk fills. Watch slot lag and cap retained WAL.
  • Meilisearch and Typesense give typo tolerance, facets and good results with almost no tuning; in exchange, less control over analysers and more limited horizontal scaling than OpenSearch.
  • A serious OpenSearch cluster is three cluster-manager-eligible nodes with fast disks; snapshots to S3 (Ceph RGW) are your backup.
  • The cost is memory and disk: the index with replicas and vectors can take several times the size of the source table.

AWS The API on ECS Fargate queries OpenSearch Service; OpenSearch Ingestion reads Aurora PostgreSQL via logical replication and keeps the index current; ElastiCache caches and OpenSearch Dashboards shows the analytics.

  • OpenSearch Ingestion supports Aurora PostgreSQL and RDS for PostgreSQL as sources: it exports an initial snapshot to S3 and then follows changes. Logical replication must be enabled in the parameter group.
  • OpenSearch Service (domains) bills per instance-hour and storage; Serverless bills per compute unit (OCU) with an always-on minimum. Serverless is not cheaper for a small, steady index.
  • Use three zones with dedicated manager nodes in production; in development a single small node is enough.
  • Access through the VPC with IAM or fine-grained access control; a domain with a public endpoint and an open policy is a data leak waiting to happen.
  • OpenSearch supports hybrid search (BM25 plus k-NN) with normalisation processors; embeddings can be computed at ingest with connectors to Amazon Bedrock.

Azure The API on Container Apps queries Azure AI Search, which Microsoft now brands as Foundry IQ; a worker pushes PostgreSQL changes into the index, Azure Managed Redis caches and Application Insights records the analytics.

  • Azure AI Search indexers do not support Azure Database for PostgreSQL as a source (they do support Azure SQL, Cosmos DB, Blob, OneLake and MySQL in preview). With PostgreSQL, use the push API from a worker fed by logical replication or Debezium.
  • Indexers can be scheduled at most every five minutes; if you need lower latency, the push model is required anyway.
  • Dedicated tiers (Basic, S1–S3, S3 HD, L1–L2) are billed per search unit per hour, replicas times partitions; the Serverless tier has been billed on consumption since September 2026 and cannot be migrated to or from other tiers.
  • The query SLA requires at least two replicas, and the read-write SLA three. One replica is a test environment.
  • The semantic ranker and integrated vectorisation at ingest cover hybrid search without custom code, at an extra usage-based cost.

Google Cloud The API on Cloud Run queries Agent Search (formerly Vertex AI Search, now part of Gemini Enterprise Agent Platform); Datastream and Dataflow carry Cloud SQL changes into the data store and Memorystore caches.

  • Google renamed Vertex AI to Gemini Enterprise Agent Platform in April 2026 and its search product is now called Agent Search; for e-commerce there is AI Commerce Search (formerly Vertex AI Search for commerce).
  • Agent Search imports from BigQuery with periodic sync measured in days, from Cloud SQL or Cloud Storage, or document by document through the API. For changes within minutes, write through the API from the CDC pipeline.
  • You pay per query and for indexed storage, with no cluster to size; in exchange you get less control over analysers, mappings and scoring than with OpenSearch or Elasticsearch.
  • If you need fine-grained relevance control, Elastic Cloud from the Marketplace or OpenSearch on GKE are the usual alternatives on GCP.
  • Datastream carries PostgreSQL into BigQuery with CDC; Dataflow helps if you transform, but for a simple index a Cloud Run worker reading the changes is cheaper.

Options by cost and scope

From the cheapest option to the most resilient. The "How it is built" column follows the selected platform.

OptionCostOperationsHow it is builtWhat you give up
PostgreSQL as the search engineA catalogue of thousands to hundreds of thousands of rows, one language and a team that does not want another database.€€€€On-premisesPostgreSQL with `tsvector`, GIN indexes, `unaccent` and `pg_trgm` for typos; pgvector if semantics are needed. No CDC: the column is updated in the same transaction.AWSAurora PostgreSQL with full-text search and pgvector, no extra service.AzureAzure Database for PostgreSQL with full-text search and the vector extension.Google CloudCloud SQL for PostgreSQL with full-text search and pgvector.Facets, synonyms and autocomplete must be hand-built, and search competes for CPU with transactions.
Dedicated engine with CDCA shop or portal with facets, autocomplete and synonyms, updates within seconds and thousands of searches per minute.€€€€On-premisesA three-node OpenSearch cluster (or single-node Meilisearch for mid-sized catalogues), Debezium with Kafka or NATS and an indexing worker.AWSOpenSearch Service across two or three zones with OpenSearch Ingestion reading Aurora PostgreSQL.AzureAzure AI Search S1 with two replicas and a worker pushing changes from PostgreSQL.Google CloudAgent Search with a Cloud Run worker writing changes through the API, or Elastic Cloud from the Marketplace.One more system to run and pay for; eventual consistency between the database and what search shows.
Scale and hybridCatalogues of millions of documents, several languages, semantic vector search and high, spiky traffic.€€€€On-premisesOpenSearch with dedicated manager nodes, data nodes split by index, embeddings computed on your own GPUs and index replicas in a second data centre.AWSOpenSearch Service with dedicated manager nodes in three zones, memory-optimised instances for vectors and embeddings via Bedrock.AzureAzure AI Search S2 or S3 with three replicas, integrated vectorisation and the semantic ranker.Google CloudAgent Search or AI Commerce Search with personalisation from user events, or Elastic Cloud sized for vectors.Vectors multiply memory and cost; relevance can no longer be tuned by eye and needs evaluation against query sets.

Extensions

Pieces you add to the base design when you need them.

ExtensionWhenServiceCost impact
Hybrid vector searchWhen users describe what they want in their own words and lexical matching fails.On-premisesOpenSearch k-NN or pgvectorAWSOpenSearch k-NN + BedrockAzureVectors + semantic rankerGoogle CloudAgent Search (built-in semantics)Medium
Event-based personalisationCatalogues where ranking should depend on history and popularity, not just text.On-premisesOpenSearch Learning to RankAWSAmazon PersonalizeAzureScoring profiles with own signalsGoogle CloudAI Commerce SearchMedium
Cache for frequent queriesWhen autocomplete and category pages repeat the same queries thousands of times.On-premisesValkeyAWSElastiCacheAzureAzure Managed RedisGoogle CloudMemorystoreLow
Index replica in another regionWhen search is critical to revenue and cannot depend on one region.On-premisesCross-cluster replicationAWSCross-cluster replicationAzureSecond service + dual indexingGoogle CloudAgent Search multi-region locationHigh
Queue between CDC and indexerTo absorb bulk reindexing and not lose changes when the engine is slow or under maintenance.On-premisesKafka or NATS JetStreamAWSAmazon MSK or SQSAzureEvent HubsGoogle CloudPub/SubLow

How to save

  • Start with PostgreSQL full-text search and add an engine only when you need facets, synonyms or good autocomplete: it is a whole service fewer.
  • Index only the fields that are searched, filtered or shown in results; storing the full document in the index doubles storage and memory.
  • On OpenSearch Service use Graviton instances and reserve the production domain's instances; on Azure AI Search, scale replicas on a schedule if traffic is daytime-only.
  • Reduce dimensions or quantise vectors (OpenSearch and Azure AI Search support compression): memory is the biggest cost in hybrid search.
  • Cache autocomplete and category pages with a short TTL: on services that charge per query, such as Agent Search, every cache hit is money saved.
  • For a small, stable index, an OpenSearch Service domain with one small instance costs less than the always-on minimum of Serverless.

Equivalents by platform

PieceOn-premisesAWSAzureGoogle Cloud
Search APIKubernetes (RKE2/k3s)ECS FargateContainer AppsCloud Run
Source-of-truth DBPostgreSQL + PatroniAurora PostgreSQLAzure DB for PostgreSQLCloud SQL (PostgreSQL)
Query cacheValkeyElastiCacheAzure Managed RedisMemorystore
Search engineOpenSearch / MeilisearchOpenSearch ServiceAzure AI SearchAgent Search
CDC + indexerDebezium + workerOpenSearch IngestionWorker (push API)Cloud Run worker
Search analyticsOpenSearch DashboardsOpenSearch DashboardsApplication InsightsSearch analytics

In depth

When you need it

When LIKE '%text%' no longer cuts it: users type with typos, without accents, in plural or with synonyms (‘laptop’ and ‘notebook’), want to filter by brand, price and size while seeing how many results each option has, and expect suggestions as they type. Also when the catalogue grows and search queries start competing for CPU with transactions. This case moves search reads into a specialised index without losing the database as the source of truth.

Scenarios

Small or internal product

A case-file search or a catalogue of a few thousand items. PostgreSQL with tsvector, a language dictionary, unaccent and pg_trgm covers 80% of it with no new infrastructure and no eventual consistency. Meilisearch or Typesense are the next step if you want typo tolerance and facets without tuning analysers.

E-commerce with peaks

Sales and campaigns multiply searches. The index scales out with read replicas, the cache absorbs autocomplete and category pages, and the indexer is decoupled by a queue so a bulk price update does not slow down queries. Facets and ordering by stock or margin matter as much as the text.

Multi-tenant SaaS

Each customer may see only its own documents. Either one index per tenant (simple isolation, too many indexes with thousands of customers) or a shared index with a mandatory tenant_id filter applied in the API, never from the client. Azure AI Search S3 HD is designed precisely for many small indexes, one per customer.

Semantic search over documentation

Users ask ‘how do I cancel an order that has already shipped’ instead of typing keywords. Hybrid search: BM25 for exact matches (codes, references) and vectors for meaning, combined with rank fusion. It is the same foundation as RAG, without the generative model at the end.

How it fits together

The user types into the search box and applies filters; the API turns that into an engine query, always adding the permission and visibility filters. First it checks the cache: the most repeated queries (autocomplete prefixes, categories) are served from there with a short TTL.

The application does not write the index. Changes in the source-of-truth database are captured via CDC (PostgreSQL logical replication) and the indexer turns them into denormalised documents: the product with its brand name, category, current price and stock, ready to search and filter without joins. If the indexer goes down, it resumes from its position in the change stream. This avoids dual writes, which always drift out of sync.

Schema or analyser changes are made with zero-downtime reindexing: create a new index, fill it from a database snapshot, apply the changes accumulated meanwhile, compare, and switch the alias to the new index atomically. The old one is kept for a few days to roll back.

The engine sends queries, zero-result searches and clicks to analytics. That data drives synonyms, field weights and business rules, and exposes the searches that find nothing.

Deployment: Docker, Kubernetes or serverless

Docker Compose on one host. The API, PostgreSQL, single-node Meilisearch or OpenSearch with its volume, Valkey and, if you use CDC, Debezium Server writing straight to the indexer or to NATS. Index snapshots are optional if you can rebuild from the database in acceptable time; measure that time.

Kubernetes on-premises. k3s or RKE2 with the OpenSearch Kubernetes Operator (or ECK if you choose Elasticsearch), CloudNativePG with logical replication enabled, Strimzi for Kafka and Kafka Connect with Debezium, and KEDA scaling the indexer on consumer lag. Use anti-affinity to spread data nodes across hosts and fast local storage.

Cloud without Kubernetes. The API on Fargate, Container Apps or Cloud Run, the managed engine (OpenSearch Service, Azure AI Search, Agent Search) and managed CDC where it exists (OpenSearch Ingestion) or a small worker where it does not.

Managed Kubernetes. EKS, AKS or GKE pay off if you want the same OpenSearch or Elasticsearch as on-premises, control over versions and plugins, or to avoid the cost of a managed search service at high volume. In exchange, you run a stateful system.

Option Fixed cost Operations Scaling When
Docker Compose One host Medium Vertical Small or mid-sized catalogue
Kubernetes on-premises Three or more nodes High Nodes and replicas On-premises requirement
Managed engine Instances or consumption Low Replicas or automatic Default choice in the cloud
EKS, AKS, GKE Control plane and nodes High Operator Full control of the engine

Key decisions

  • PostgreSQL or a dedicated engine. While search is a secondary feature, PostgreSQL avoids eventual consistency and another system. An engine is justified by relevance, facets, languages and volume.
  • CDC or domain events. CDC captures every change, including those that bypass the application; domain events carry intent and pre-shaped data. Many teams combine both: CDC as the guarantee and events to enrich.
  • High-level managed service or configurable engine. Agent Search or Azure AI Search with the semantic ranker deliver good results fast; OpenSearch or Elasticsearch give full control over analysers, scoring and cost in exchange for work.
  • Per-tenant or shared index. Isolation and easy deletion versus index count and fixed cost.
  • Vectors or not. They add semantic understanding, but also memory, latency and an embedding model to version. Start lexical, measure, and add vectors where it fails.

Common pitfalls

  • Writing to the database and the index from the application: after the first partial failure they stay out of sync forever.
  • Treating the index as the source of truth and being unable to rebuild it.
  • Changing mappings on the live index instead of reindexing behind an alias.
  • Applying permission filters in the frontend.
  • An abandoned logical replication slot filling the PostgreSQL disk.
  • Tuning relevance without a reference query set: every improvement breaks another search and nobody notices.
  • Ignoring zero-result searches, which are the to-do list for synonyms and the catalogue.

These are starting points, not recipes: size, cost and compliance requirements change the choice.