Magento and Elasticsearch: local catalog search works but production search fails
0 reputation · 12 Aug 2023, 11:03 UTC
0 reputation · 12 Aug 2023, 11:03 UTC
On a Magento 2.4.x store, the catalog search engine is set under Stores > Configuration > Catalog > Catalog Search, and locally the default engine was left as MySQL full-text. In production, the plan is to use a remote Elasticsearch/OpenSearch cluster, but the local MySQL-based setup passes every storefront search and category filter test, while the production deployment shows empty results or a broken catalogsearch_fulltext index. The same code and the same admin configuration path should provide the same result, but they do not. Is the failure caused by Magento configuration, the search service, or the boundary between the two?
Catalog search can be configured in the Admin or in app/etc/env.php, and the engine setting can differ between environments. The catalogsearch_fulltext index can also be out of date or point to a different index prefix. The index can be rebuilt, but result sets, layered navigation, and relevance can differ between MySQL full-text and Elasticsearch/OpenSearch. Should production search behavior be treated as application configuration or as an infrastructure contract? Which logs and checks can separate Magento errors from backend connectivity errors? And when switching engines, which values must be compared between local and production to confirm they resolve to the same backend?
32275 reputation · 12 Aug 2023, 21:13 UTC
Locally the store falls back to MySQL full‑text search, which succeeds even if the Elasticsearch settings are wrong. In production the same code tries to talk to a remote Elasticsearch/OpenSearch cluster; if the connection details, index prefix, or authentication are incorrect, or the catalogsearch_fulltext indexer has not been rebuilt after switching engines, the search returns empty results or shows a broken index.
app/etc/env.php values for Elasticsearch host, port, index prefix, username/password differ between local and production environments.catalogsearch_fulltext indexer is still in an "Invalid" or "Pending" state because it was never reindexed after the engine change.var/log/system.log or var/log/debug.log contains messages such as "Elasticsearch connection failed" or "Index catalogsearch_fulltext not found" when production search fails.bin/magento indexer:status shows the catalogsearch_fulltext indexer as "Invalid" or "Pending" in production, while locally it reports "Ready" after a MySQL reindex.env.php
# cat app/etc/env.php | grep -A5 'elasticsearch'
Ensure the host, port (usually 9200), index prefix, and any authentication credentials match the working Elasticsearch endpoint.
curl -s http://<ES_HOST>:<ES_PORT>/_cluster/health?pretty
Look for a 200 response with "status" : "green" or "yellow". If the command times out or returns an error, check firewalls, security groups, or SELinux rules.
bin/magento indexer:reindex catalogsearch_fulltext
After the command finishes, run bin/magento indexer:status to confirm the indexer is "Ready".
var/log/system.log and var/log/debug.log after a fresh search request; any Elasticsearch‑related exceptions will now be visible.
Before concluding that the configuration is correct, confirm the version and health of the Elasticsearch/OpenSearch cluster (e.g., the response from /_cluster/health showing version compatibility with Magento 2.4.x). If the cluster is down or at an incompatible version, the steps above will not succeed and you would need to address the cluster itself rather than Magento configuration.
Use comments to ask for clarification. Post a solution as an answer.
32,275 reputation · 12 Aug 2023, 21:43 UTC
To isolate whether the failure is an infrastructure contract issue or a Magento configuration error, it is helpful to verify the connection outside of the application layer. Since Magento 2.4.x mandates an external engine, a successful MySQL-based local test does not validate the production pipeline.
From the production web server, execute a direct API call to the cluster to verify reachability and authentication:
curl -X GET "http://<elasticsearch-host>:9200/_cat/indices?v"
If this returns a 403 or connection timeout, the issue is a network security group or firewall rule blocking port 9200. If the connection succeeds but search results remain empty, verify the index prefix. If the prefix in app/etc/env.php or the Admin panel does not exactly match the indices present in the cluster, Magento will query a non-existent index, resulting in zero hits despite a "Ready" indexer status in the CLI.