Speeding Up Magento 2 Search with Elasticsearch 7.x: A Practical Guide
Magento 2.4+ ships with Elasticsearch 7.x as the default search engine. Learn how to enable it, configure indexers, and tune relevance to cut search latency by up to 30%—with a step‑by‑step example and clear trade‑offs.
19 Jul 2025, 14:54 UTC

Problem: Slow, Inaccurate Search in Magento 2 Stores
Many Magento 2 shops still rely on MySQL full‑text search. On medium‑to‑large catalogs it becomes sluggish and often misses relevant results. The default MySQL engine also lacks advanced faceting and relevance tuning that modern shoppers expect.
Thesis: Switch to Magento’s Default Elasticsearch 7.x Engine and Fine‑Tune It
Magento 2.4+ ships with Elasticsearch 7.x as the built‑in search engine. Enabling it, configuring the indexers, and tweaking relevance can cut search latency by up to 30% and dramatically improve hit‑rate. The following steps walk you through the process, with a concrete example and a clear trade‑off discussion.
1. Enable Elasticsearch in Magento 2
1.1. Verify Compatibility
Before making any changes, confirm that your Magento version supports the Elasticsearch major version you plan to use. For Magento 2.4.5, Elasticsearch 7.9 is supported; newer 7.x releases may break indexing. Check the Magento DevDocs for the exact mapping.
1.2. Configure the Connection
There are two common ways to set the connection details:
- Admin UI – Navigate to
Stores > Configuration > Catalog > Catalog Searchand fill in the host, port, and cluster name. - env.php – Edit
app/etc/env.phpto add theelasticsearcharray. This method is preferred for production because it keeps credentials out of the admin UI.
Example env.php snippet:
'search' => [
'engine' => 'elasticsearch7',
'elasticsearch7' => [
'host' => '10.0.0.5',
'port' => '9200',
'index_prefix' => 'magento2',
'cluster' => 'prod-cluster',
'user' => 'elastic',
'password' => 'secret',
],
],
After editing env.php, run bin/magento setup:upgrade to apply the changes.
1.3. Verify Connection
Use the cluster health API to ensure Magento can talk to Elasticsearch:
curl http://10.0.0.5:9200/_cluster/health?pretty
Expected output: status: "green". If you see status: "red" or connection errors, fix network or credentials first.
2. Configure Magento Indexers for Elasticsearch
2.1. Indexing Mode
Magento offers two modes: Update on Save (real‑time) and Update by Schedule (batch). For most shops, Update on Save keeps search fresh but increases write load. Use Update by Schedule during high‑traffic periods.
Set it via CLI:
bin/magento indexer:set-mode schedule search_catalog_product
Then schedule a cron job to run indexer:reindex every 15 minutes.
2.2. Dedicated Search Indexer
Magento automatically creates a catalogsearch_fulltext index. Verify its status:
bin/magento indexer:status
All indexes should report Ready. If any show Processing or Invalid, run:
bin/magento indexer:reindex catalogsearch_fulltext
3. Tune Relevance and Faceting
3.1. Custom Analyzers
Elasticsearch’s analyzers control how text is tokenized. Magento exposes a simple configuration for common analyzers:
standard
standard
my_custom
custom
standard
lowercase
stop
To apply a custom analyzer to a field, edit the etc/elastic.xml in a custom module or theme. This step is advanced and usually only needed for multilingual or niche catalogs.
3.2. Field Boosting
Boosting tells Elasticsearch to weight certain fields higher. In Magento, you can adjust the search_weight for attributes via the admin:
bin/magento catalog:attribute:set-visibility --attribute-code name --visibility 4
Alternatively, edit the catalogsearch.xml to set boost values per field.
3.3. Synonyms
Synonym files allow the search to treat different terms as equivalent. Place a synonyms.txt file in var/search and reference it in the analyzer configuration. Example entry:
color, colour, hue
After adding, reindex.
4. Practical Worked Example
Assume a store with 50,000 products and a dedicated Elasticsearch cluster at 10.0.0.5:9200. The steps below bring the search up to speed.
- Update
app/etc/env.phpwith the connection details shown earlier. - Run
bin/magento setup:upgradeandbin/magento setup:di:compileto apply changes. - Set indexing mode to schedule for
catalogsearch_fulltext: - Configure a cron job that runs
bin/magento indexer:reindex catalogsearch_fulltextevery 15 minutes. - Verify cluster health:
curl http://10.0.0.5:9200/_cluster/health?pretty→status: "green". - Check indexer status:
bin/magento indexer:status→ allReady. - Test search latency via the browser’s Network panel: before, a product search took ~1.2 s; after enabling Elasticsearch, it drops to ~0.8 s.
bin/magento indexer:set-mode schedule catalogsearch_fulltext
5. Trade‑Offs and Limitations
- Version Compatibility: Using Elasticsearch 8.x with Magento 2.4.x will break indexing. Stick to the supported 7.x range.
- Resource Overhead: Elasticsearch requires dedicated CPU, RAM, and disk I/O. Monitor
vm.max_map_countandmax_file_descriptorsper the official guide. - Indexing Load:
Update on Savekeeps search fresh but can spike CPU during bulk product uploads. PreferUpdate by Schedulefor large catalogs. - Complexity of Custom Analyzers: Tweaking analyzers can improve relevance but may introduce bugs if not thoroughly tested.
6. Actionable Checklist
- Confirm Magento‑Elasticsearch version compatibility.
- Set connection details in
env.phpor Admin UI. - Run
setup:upgradeand reindex. - Verify cluster health and indexer status.
- Measure search latency pre‑ and post‑configuration.
- Adjust indexing mode and analyzer settings based on performance data.
- Document the configuration for future maintenance.
By following these steps, you can transform Magento’s search from a bottleneck into a competitive advantage, delivering faster, more relevant results to your customers.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.