Prompt
How do I add full-text search to a SaaS product?
Latest observation
Adding full-text search to a SaaS product usually means choosing between:
- Database-native search for simplicity
- Dedicated search engine for better relevance, scale, and features
Here’s a practical way to think about it.
1) Decide what kind of search you need
Ask:
- How much data will be searched?
- Do users need typo tolerance, stemming, synonyms, filters, ranking?
- Is search global across many tenants, or per-tenant?
- Do you need real-time indexing or can it lag a few seconds/minutes?
- Do you need auditability, access control, and soft deletes?
If your search is basic and your dataset is small, PostgreSQL full-text search may be enough. If you need strong relevance, autocomplete, facets, or large-scale search, use Elasticsearch/OpenSearch, Meilisearch, Typesense, or Algolia.
2) Common architecture
Typical flow:
- Your app writes data to the main database
- A change event is emitted
- A search index is updated asynchronously
- Search queries hit the search index, not the main DB
This is often done with:
- Outbox pattern or change events
- A background worker/queue
- Periodic reindex jobs for recovery
3) Choose a search backend
Option A: PostgreSQL full-text search
Good for:
- MVPs
- Smaller SaaS apps
- Simpler ops
Pros:
- No extra infrastructure
- Easy consistency with your primary data
- Good enough for many admin/search pages
Cons:
- Limited ranking/relevance features compared to search engines
- Harder to do advanced autocomplete/facets at scale
Useful features:
tsvectorcolumns- GIN indexes
to_tsquery,plainto_tsquery,websearch_to_tsquery
Option B: Elasticsearch / OpenSearch
Good for:
- Advanced search relevance
- Large datasets
- Facets/aggregations
- Typo tolerance and complex ranking
Pros:
- Powerful querying
- Great for multi-field search and filtering
- Widely used for SaaS search
Cons:
- Operational overhead
- Index management complexity
- Eventual consistency
Option C: Hosted search APIs
Examples:
- Algolia
- Typesense Cloud
- Meilisearch Cloud
Pros:
- Fast to launch
- Less ops burden
- Good UX features out of the box
Cons:
- Ongoing cost
- Vendor lock-in
- Data residency/compliance concerns
4) Design the searchable document
Model what users search, not your raw database schema.
Example document:
{
"id": "ticket_123",
"tenant_id": "tenant_abc",
"title": "Login issue on mobile",
"body": "User cannot sign in after password reset",
"status": "open",
"tags": ["auth", "mobile"],
"owner": "Jane Doe",
"created_at": "2026-10-12T10:00:00Z"
}
Include:
- Searchable text fields
- Filter fields
- Sorting fields
- Tenant identifier
- Permissions/access metadata if needed
5) Multi-tenancy is critical
For SaaS, ensure search is tenant-aware.
Common approaches:
- Single shared index with
tenant_idfilter on every query - Separate index per tenant for isolation, but this can get expensive
Most SaaS apps use a shared index plus strict query filtering:
- Always filter by
tenant_id - If permissions vary within a tenant, also filter by user/group access
Never rely only on the UI to hide results.
6) Indexing strategy
You need a reliable way to keep the index in sync.
Typical patterns:
- Write DB first, then queue index update
- Use an outbox table to guarantee events aren’t lost
- Bulk reindex on deployment or schema changes
Important:
- Make indexing idempotent
- Handle deletes and updates
- Support partial failures and retries
- Monitor indexing lag
7) Relevance and ranking
Search quality often matters more than raw matching.
Common ranking signals:
- Exact match boost
- Title match higher than body match
- Recency boost
- Popularity/use frequency
- Field weighting
- Business-specific scoring
Example:
- Title matches = strong boost
- Tags matches = medium boost
- Body matches = lower boost
8) UX features users expect
Nice search experiences usually include:
- Autocomplete/suggestions
- Highlighted matches
- Filters/facets
- Sort by date/relevance
- Empty-state guidance
- “Did you mean” typo correction
- Search across multiple entities
9) Security and compliance
For SaaS search, pay attention to:
- Tenant isolation
- Row-level permissions
- Data retention/deletion
- PII handling
- Encryption in transit/at rest
- GDPR/CCPA delete propagation
If a user loses access, they must not see indexed content they shouldn’t.
10) Observability
Track:
- Search latency
- Indexing lag
- Query volume
- Zero-result queries
- Top queries
- Result click-through rate
- Failed indexing jobs
This helps improve relevance over time.
11) Practical recommendation
A simple decision guide:
- Small SaaS / early MVP: PostgreSQL full-text search
- Need excellent UX and scale: OpenSearch/Elasticsearch or hosted search
- Want fastest implementation with good UX: Algolia/Meilisearch/Typesense Cloud
12) A good implementation plan
- Define search use cases and fields
- Choose backend
- Add tenant-aware searchable documents
- Implement async indexing
- Build query API with filters and pagination
- Add ranking boosts
- Add monitoring and reindex tooling
- Test permissions and edge cases
If you want, I can give you:
- a PostgreSQL full-text search implementation
- an Elasticsearch/OpenSearch schema
- or a search architecture for multi-tenant SaaS