ElasticSearch and Magento 2
ElasticSearch in Magento 2 is not only about search, as the name implies.
It actually speeds up the display of things throughout Magento, e.g. anywhere you browse the catalog, like category pages.
So, for a supporting Magento version, and, if you have adequate RAM available, installing and using ElasticSearch for Magento 2, is a must.
In this short tutorial, I review simple steps on how to set up ElasticSearch in a single-server scenario, on CentOS/RHEL 7 and CentOS/RHEL 8.
Installing ElasticSearch package
Set up RPM repository
ElasticSearch is critical software for the Magento stack, so it needs to be continuously updated for security reasons.
This is why you must use an RPM repository to install it. You can use the GetPageSpeed RPM repository to install it.
To set up the repository configuration, run:
sudo yum -y install https://extras.getpagespeed.com/release-latest.rpm
Pin required ElasticSearch version
The latest Magento, at the time of this writing, supports only the ElasticSearch 7.x series, so this is what we are going to install.
Furthermore, Magento 2.4 documentation recommends specific 7.x versions as being compatible, for different minor Magento versions.
So the first step is to find your exact Magento version. For this, run the following:
php bin/magento --version
The output will give you, e.g.:
Magento CLI 2.4.2-p1
Locate the required ElasticSearch version in the version table from the Magento documentation. For Magento 2.4.2 we need ElasticSearch 7.9.x.
So we are going to pin the repository to only install 7.9.x by adding the following to yum
configuration at /etc/yum.repos.d/getpagespeed-extras.repo
:
yum -y install yum-utils
yum-config-manager --setopt=getpagespeed-extras.includepkgs=elasticsearch-7.9.* --save
Installing ElasticSearch
To install ElasticSearch, run:
sudo yum -y install elasticsearch
Enable ElasticSearch service by simply running:
sudo systemctl enable --now elasticsearch
Verify it’s running:
curl -XGET 'localhost:9200/_cat/health?v&pretty'
Should yield:
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 1601396097 16:14:57 elasticsearch yellow 1 1 3 3 0 0 3 0 - 50.0%
Configure Elasticsearch within Magento
To configure Magento to use Elasticsearch:
- Log in to the Magento Admin as an administrator.
- Click Stores > Settings > Configuration > Catalog > Catalog > Catalog Search.
- From the Search Engine list, select your Elasticsearch version.
Now, reindex everything:
bin/magento indexer:reindex
Tips
If you want to reset ElasticSearch indexes, here’s a handy command:
curl -X DELETE 'http://localhost:9200/_all'