Configure Catalog Search Engine with Magento CLI

Since we installed Magento 2.3 we started receiving a notice that Catalog Search using MySQL will go away as it is deprecated. This is ok as we are going to use Elasticsearch anyway, but we have to setup Magento to connect to the correct hostname when the container starts and not manually in the admin or using CLI, but the CLI is helpful as we can run scripts after the container is up and running.

If you want more information on how to do via the admin you will find the tutorial in the Magento Docs: https://devdocs.magento.com/guides/v2.3/config-guide/elasticsearch/configure-magento.html

You just really need to configure two settings via the CLI and they are:

magento config:set catalog/search/engine 'elasticsearch6'
magento config:set catalog/search/elasticsearch6_server_hostname 'elasticsearch-hostname.env'

The first option is the engine which it can be: mysql, elasticsearch, elasticsearch5 or elasticsearch6.
The second option is the server hostname. You can either use IP or a DNS. We have Magento set up on a Kubernetes cluster, so elasticsearch has its own container running and the hostname is based on the store name, so you can also use variables:

magento config:set catalog/search/elasticsearch6_server_hostname "elasticsearch-$MY_VARIABLE.env"

Just pay attention that now it needs double quotes. You can also set other configurations:

magento config:set catalog/search/elasticsearch6_server_port '9200'
magento config:set catalog/search/elasticsearch6_index_prefix 'magento2'
magento config:set catalog/search/elasticsearch6_enable_auth '0'
magento config:set catalog/search/elasticsearch6_server_timeout '15'

These are the default settings for elasticsearch and elasticsearch6, so change what you need to.

I hope you find this article helpful and see you on the next one.