Default database-based product search often falls short for e-commerce's needs (typo tolerance, relevance ranking, faceted filtering) — Elasticsearch provides a purpose-built solution. This guide covers integration.
Why Standard Database Search Falls Short
A basic LIKE '%query%' database search lacks relevance ranking, typo tolerance, and struggles with faceted filtering (by price range, category, attributes) at reasonable performance — Elasticsearch is purpose-built for exactly these e-commerce search requirements.
Step 1 — Install Elasticsearch on Your VPS
docker run -d --name elasticsearch -p 9200:9200 -e "discovery.type=single-node" -e "xpack.security.enabled=false" docker.elastic.co/elasticsearch/elasticsearch:8.13.0
For production use, enable security features and follow proper Elasticsearch hardening rather than the simplified single-node setup shown here for illustration.
Step 2 — Define a Product Index Mapping
PUT /products
{
"mappings": {
"properties": {
"name": {"type": "text"},
"description": {"type": "text"},
"price": {"type": "float"},
"category": {"type": "keyword"},
"in_stock": {"type": "boolean"}
}
}
}
The mapping defines how each field is indexed and searched — text fields support full-text search with relevance scoring; keyword fields are exact-match, appropriate for filtering (like category faceting).
Step 3 — Index Your Product Data
POST /products/_doc/1
{
"name": "Wireless Bluetooth Headphones",
"description": "Premium noise-canceling headphones",
"price": 89.99,
"category": "electronics",
"in_stock": true
}
Sync your product catalog into Elasticsearch, either via a one-time bulk import plus ongoing incremental updates as products change (similar sync considerations to How to Configure Inventory Sync Across Multiple Sales Channels).
Step 4 — Implement Search Queries
GET /products/_search
{
"query": {
"multi_match": {
"query": "bluetooth headphones",
"fields": ["name^2", "description"]
}
}
}
name^2 boosts the relevance weight of matches in the product name over the description — a common relevance tuning technique, reflecting that name matches are typically more relevant than description matches.
Adding Faceted Filtering
GET /products/_search
{
"query": {"multi_match": {"query": "headphones", "fields": ["name", "description"]}},
"aggs": {
"categories": {"terms": {"field": "category"}},
"price_ranges": {"range": {"field": "price", "ranges": [{"to": 50}, {"from": 50, "to": 100}, {"from": 100}]}}
}
}
Aggregations return facet counts alongside search results, powering the filter sidebar (category, price range) common in e-commerce search interfaces.
Handling Typo Tolerance
"query": {
"fuzzy": {"name": {"value": "headphnoes", "fuzziness": "AUTO"}}
}
Fuzzy matching handles common typos gracefully, improving search success rate for genuinely imperfect user input — a meaningful UX improvement over exact-match database search.
Keeping the Index Synchronized with Your Product Database
Establish a reliable sync process (webhook-triggered on product changes, or scheduled reindexing) ensuring Elasticsearch data doesn't drift stale relative to your actual product database — similar principle to the general search index freshness concern in How to Add a Search Feature to a Static Site (Client-Side Search).
Common Errors
Search results seem oddly ranked, unintuitive relevance — review your field boosting configuration and query structure; default relevance scoring may not match your intuitive expectations without deliberate tuning for your specific product catalog and use case.
Continue Reading
- How to Configure Inventory Sync Across Multiple Sales Channels
- How to Optimize Product Page Load Speed for Conversions
- How to Add a Search Feature to a Static Site (Client-Side Search)
Browse more articles in E-commerce Platform Deployment.