Prompt
How do I add search that supports sorting, filters, and synonyms?
Latest observation
To add search with sorting, filters, and synonyms, the usual approach is to use a search engine rather than a basic database LIKE query.
Typical stack
Good options include:
- Elasticsearch / OpenSearch — most flexible and powerful
- Algolia — easiest managed option, great UX, built-in typo tolerance
- Meilisearch — simple, fast, developer-friendly
- PostgreSQL full-text search — good if you want to stay in your DB, but less advanced
Core pieces you need
1. Index your data
Store searchable fields in a search index, not just your database.
Example document:
{
"id": 123,
"title": "Wireless Headphones",
"description": "Noise cancelling over-ear headphones",
"category": "Electronics",
"brand": "Sony",
"price": 199.99,
"rating": 4.7,
"tags": ["audio", "wireless"]
}
2. Support search text
Use full-text search over fields like:
- title
- description
- tags
3. Add filters
Filters are for exact matches or ranges, like:
category = Electronicsbrand = Sonyprice between 100 and 300rating >= 4
These should be query parameters, not part of the text search.
4. Add sorting
Allow users to sort by fields such as:
- relevance
- price asc/desc
- rating desc
- newest first
Make sure the fields you sort on are stored in the index in a sortable form.
5. Add synonyms
Synonyms help search understand equivalent terms:
tv↔televisionlaptop↔notebookcellphone↔mobile phone
Most search engines let you define synonym lists or synonym sets.
Example API design
A single search endpoint might look like:
GET /search?q=headphones&category=Electronics&brand=Sony&minPrice=100&maxPrice=300&sort=price_asc
Or:
POST /search
{
"query": "headphones",
"filters": {
"category": ["Electronics"],
"brand": ["Sony"],
"price": { "min": 100, "max": 300 }
},
"sort": [
{ "field": "price", "direction": "asc" }
]
}
Recommended implementation pattern
If you want the easiest path:
Use Algolia or Meilisearch.
If you need maximum control:
Use Elasticsearch/OpenSearch.
If you want to stay in Postgres:
Use:
tsvector/tsquery- GIN indexes
- custom filtering/sorting in SQL
- synonym dictionaries or query expansion
Important considerations
Relevance tuning
You’ll likely need to adjust:
- field boosts
Example: title matches count more than description matches - typo tolerance
- synonyms
- ranking rules
Facets
If users need filter counts like:
- Electronics (124)
- Clothing (87)
then you want faceted search, which most search engines support well.
Performance
- index changes asynchronously if possible
- cache frequent queries
- avoid doing search directly on the primary OLTP database for large datasets
Simple recommendation
If you’re building this today:
- Small/medium app: Meilisearch
- SaaS / easiest UX: Algolia
- Large/complex / self-hosted: OpenSearch or Elasticsearch
- Already on Postgres and modest needs: PostgreSQL FTS + indexes
If you want, I can also show:
- a database schema + API design,
- an Elasticsearch example, or
- a Postgres-only implementation.