Posted in:

A Complete Magento 2 Elasticsearch Configuration Tutorial

Until version 2.3, Magento Open Source (OS) users relied on the database to manage catalog search queries out of the box. However, the default database search offered limited functionality and wasn’t scalable.

Merchants seeking advanced catalog search features were forced to rely on third-party services such as Apache Solr, Sphinx, or address the development agencies for custom Magento extension development.

The Magento 2 Elasticsearch integration in version 2.3 addressed this issue and delivered several performance and functionality benefits to Magento OS users. Realizing its potential, Adobe deprecated the database search with the release of version 2.4 and made Elasticsearch the default catalog search engine for Magento stores.

In this tutorial, we’ll teach you how to install and configure Magento 2 Elasticsearch for your store so that you can offer powerful catalog search functionality to your customers.

Install and configure Magento 2 Elasticsearch

When you’re using Magento 2 with Elasticsearch, you can install and host Elasticsearch on the same server as your Magento installation or separately.

Adobe recommends running Elasticsearch and Magento on the same host because it offers greater security benefits. It makes intercepting communication between the applications virtually impossible.

Therefore, in this tutorial, we’ll describe how to install and configure Magento 2 Elasticsearch on the same host.

Note: Unless specified otherwise, all commands in this tutorial must be executed as a root user.

Step 1: Install Java Development Kit 1.8

Elasticsearch is developed in Java and requires the Java Development Kit (JDK) to operate. Let’s start by installing JDK 1.8 on our server.

  1. Update the existing repositories on your server by running the following command:

<code>

$ sudo apt-get update

</code>

  1. To install JDK 1.8, run the following command in your terminal:

<code>

$ sudo apt-get install openjdk-8-jdk -y

</code>

Note: The “-y” flag automatically responds “Yes” to all confirmation prompts. If you prefer reviewing prompts before responding to them, you can omit the “-y” flag.

  1. Once the installation is complete, run the following command to verify JDK is installed:

<code>

$ sudo java -version

</code>

You should see an output similar to:

<code>

openjdk version “1.8.0_252”

OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1~16.04-b09)

OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)

</code>

That’s it; we’ve successfully installed JDK 1.8 and can now proceed with installing Elasticsearch.

Step 2: Install Elasticsearch

Now, it’s time to download the installation file from Elasticsearch, verify its integrity, install it, and configure it to autorun on boot. You can do this using different methods, as demonstrated in the official documentation.

We’ll download and install Elasticsearch using the manual Debian package method.

Note: Check Adobe’s documentation to ensure you download the Elasticsearch version compatible with your Magento version.

  1. Navigate to your home directory to download the Elasticsearch installation file over there instead of in the Magento filesystem or any other location on your server. Execute the following command to navigate to the home directory of your user:

<code>

$ cd ~

</code>

  1. Download the Debian package for the Elasticsearch version compatible with your Magento store by running the command below and replacing the version number in the command. The following command downloads Elasticsearch 7.6.0, which is compatible with Magento 2.3.5 and 2.4.0.

<code>

$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.0-amd64.deb && wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.0-amd64.deb.sha512

</code>

  1. Verify the integrity of the installation file by comparing the Secure Hash Algorithm (SHA) of the files using the following command:

<code>

$ shasum -a 512 -c elasticsearch-7.6.0-amd64.deb.sha512

</code>

You should see an output similar to:

<code>

elasticsearch-7.6.0-amd64.deb: OK

</code>

  1. Once you’ve verified the integrity, add the installation file to your operating system’s package manager by running the following command:

<code>

$ sudo dpkg -i elasticsearch-7.6.0-amd64.deb

</code>

  1. Next, reload the system, add Elasticsearch to the system startup applications, and start it by running the following commands:

<code>

$ sudo /bin/systemctl daemon-reload

$ sudo /bin/systemctl enable elasticsearch.service

$ sudo systemctl start elasticsearch

</code>

Note: It may take the system approximately 1 to 2 minutes to initialize after the last command.

  1. Finally, check if Elasticsearch is working by running the following command:

<code>

$ curl -XGET ‘http://localhost:9200/_cat/health?v&pretty’

</code>

You should see an output similar to:

<code>

epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks
1519701563 0.138461 elasticsearch green 1 1 0 0 0 0 0 0

</code>

That’s it. You’ve installed Elasticsearch on your server. Now, it’s time to configure and optimize it.

Step 3: Configure Elasticsearch on your server

The default Elasticsearch configuration settings are adequate for most stores. However, you can tweak them a bit further and optimize them for your store. We’ll edit the default Elasticsearch configuration file to do this.

  1. Execute the following command to edit the Elasticsearch configuration file in your preferred text editor. We’ll be using nano:

<code>

$ sudo nano /etc/elasticsearch/elasticsearch.yml

</code>

  1. Locate “cluster.name” in the configuration file. Delete the preceding “#” and replace the default “my-application” with something a bit more descriptive like “Primary Cluster.”
  2. Next, find “node.name”, delete the preceding “#,” and replace the default entry with your store name.
  3. Finally, locate “network.host,” delete the preceding “#,” and replace the default entry with “localhost.”

Changing the cluster and node names prevents any unwanted nodes on the same network as your Elasticsearch server from being discovered and associated with your installation. Setting the network host to localhost ensures that Elasticsearch doesn’t communicate with any external IP addresses.

These are the bare minimum changes you should make to ensure your Elasticsearch server operates smoothly. You can make several other changes to fine-tune the configuration; however, those are beyond the scope of this tutorial.

If you’d rather have professionals deal with the technicalities of optimizing your store, you can reach out to Staylime’s team for assistance.

Step 4: Configure Magento to use Elasticsearch

Now that we’ve installed Elasticsearch, let’s configure Magento 2 to use Elasticsearch.

We’ve broken this step down into four sub-steps to make them easier to follow.

Step 4A: Configure general search options

  1. Navigate to Stores > Settings > Configuration > Catalog > Catalog in the Magento admin panel and expand the “Catalog Search” section.
  2. Set the minimal and maximum query length, i.e., the word count for searches on your store. The default values are 3 and 128. You can replace these with any values between 2 and 300.

Note: Setting the minimal query length under 3 can impact store performance. And if you replace them with custom values, make sure you replace them on your Elasticsearch server too.

  1. Set the number of popular search terms you want to cache on your store. You can leave the default value, enter your preferred quantity, or enter “0” to cache all searches.
  2. The Product EAV indexer is enabled by default for optimal performance. This setting restricts third-party extensions from using the indexer. If you’re using an extension that relies on the Product EAV indexer, make sure you change “Enable EAV Indexer” to “No.”
  3. Finally, set the number of results you wish to display for auto-complete when customers type search queries on your website. Limiting this to a smaller number or leaving it as the default “8” mitigates potential impacts on website performance.

Step 4B: Configure Elasticsearch connection settings

If you’re using Magento version 2.3.x, you can configure the Elasticsearch connection settings by following these instructions:

  1. From the “Search Engine” dropdown, choose your version of Elasticsearch.
  2. Leave the default values for “Elasticsearch Server Hostname” and “Elasticsearch Server Port” intact.
  3. You can enter a prefix to identify the Elasticsearch index. The default entry is “magento2”. If you’re using a single instance of Elasticsearch for multiple stores or Magento installations, you can use this to identify the index for the current store.
  4. Since we’ve installed Elasticsearch on the same host as our web server, we’ll set “Enable Elasticsearch HTTP Auth” to “No.”
  5. You can leave the default value for the “Elasticsearch Server Timeout” as “15.”
  6. Finally, click on “Test Connection” to check if Magento can communicate with Elasticsearch. If the connection is successful and everything is working as expected, you should see “Successful! Test again?”

Step 4C: Configure search suggestions and recommendations

You can offer customers a better search experience by using recommendations and suggestions. Here’s how you can configure it for optimal performance.

  1. To offer recommendations, ensure you’ve set “Enable Search Recommendations” to “Yes.” Additionally, you can also do the following:
  2. a) Set the number of recommendations to offer for each search result manually or leave it as the default value.
  3. b) If you wish to show the number of results available for each recommendation, set “Show Results Count for Each Recommendation” to “Yes.”
  4. To offer search suggestions, set “Enable Search Suggestions” to “Yes.” Additionally, you can optimize it as follows:
  5. a) Set the number of search suggestions to offer by entering a value for “Search Suggestions Count” or leave it as the default.
  6. b) To display the number of results available for each suggestion, set “Show Results for Each Suggestion” to “Yes.”

Note: Although these can help improve the search functionality on your website, be careful when setting values higher than the default as they may impact server performance.

Step 4D: Configure minimum terms to match

You can further optimize search results by specifying a value for “Minimum Terms to Match.” It lets you choose the number of clauses that should match for a search result.

Once you’ve made all the above changes, click on “Save Config” to save them.

Note: This setting requires familiarity with the “minimum_should_match parameter” in Elasticsearch. If you’re unfamiliar with this parameter, leave it blank. 

Step 5: Reindex catalog search

Finally, it’s time to reindex the catalog search index and refresh the full page cache to update the changes on your website.

You can clear the website cache from the Magento admin panel by following these steps:

  1. Navigate to System > Cache Management.
  2. Select the checkbox beside “Page Cache.”
  3. From the “Actions” dropdown, select “Refresh” and click on “Submit.”

Alternatively, you can use the “magento cache:clean” command to clear the cache via the Magento command-line interface.

You can reindex the Catalog Search indexers from the Magento command line by entering the following command as the Magento filesystem owner in the terminal:

<code>

$ bin/magento indexer:reindex catalogsearch_fulltext

</code>

Wrapping it up

The Magento 2 Elasticsearch functionality allows you to improve your website shopping experience while boosting website performance simultaneously.

You can even set up search terms, search synonyms, and generate search reports to analyze customer queries. The possibilities to optimize your store performance with Magento 2 are endless.

___________________________________________________________________________

Author: Jan Guardian

Jan is the Chief Business Development Officer at Staylime, a Magento design and development company headquartered in Redwood City, California. He is a Member of the Magento Association and an Adobe Sales Accredited Magento Commerce professional. Jan is responsible for developing and leading the sales and digital marketing strategies of the company. He is passionate about ecommerce and Magento in particular — throughout the years his articles have been featured on Retail Dive, Hacker Noon, Chief Marketer, Mobile Marketer, TMCnet, and many others.