Magento 2 Elasticsearch: How To Install And Configure

how to install and configure elasticsearch in magento 2

Configuring and installing Elasticsearch in Magento 2 is a critical step for optimizing catalog searches and enhancing the overall performance of your e-commerce store. Elasticsearch, a powerful search engine, offers rapid search results, scalability, and full-text search capabilities, making it an indispensable component for Magento 2.

In this guide, we will walk you through the process of setting up Elasticsearch in Magento 2, empowering you to unlock the full potential of this dynamic search engine and take your online store to new heights. Let’s check it out!

What Is Elasticsearch?

Elasticsearch is a powerful open-source search and analytics engine using Apache Lucene. It is able to handle a large number of use cases, support multiple tenancies, and full-text search on the HTTP web interface. In simple words, Elasticsearch helps to store and map all documents effectively in order to optimize the search and retrieval function.

Elasticsearch also supports various uses and offers a user-friendly search experience on the web interface. Organizations can leverage Elasticsearch’s capabilities to analyze different kinds of data and make informed decisions. Whether it’s for applications, performance monitoring, log analysis, or improving website search, Elasticsearch’s flexibility and performance make it a popular choice for various search and analytics needs.

Magento 2.4 and onwards have made Elasticsearch the default catalog search engine, which significantly benefits both customers and store owners, and drives business conversion rates.

How does Elasticsearch work?

When different types of data, like logs, system metrics, and web applications, are fed into Elasticsearch, the data is processed and indexed. An index in Elasticsearch is like a collection of similar documents that helps organize related information, making it easy to find. This indexing process enables quick and efficient data retrieval, allowing users to perform complex searches and get detailed summaries using aggregations.

In the case of Magento Elasticsearch, the system relies on multiple data sources, including logs and a database. Once the data is parsed, it is added to the Elasticsearch index. This indexing feature empowers Magento store owners to run advanced searches and provide precise and relevant information in response to user searches. Elasticsearch’s powerful capabilities, such as quick text search, search suggestions, auto-completion, spelling/mistyping checking, and geolocation search, greatly enhance the search experience for Magento users.

Why Should We Use Elasticsearch In Magento 2?

In all of the e-commerce websites including the ones built on Magento 2, the search is no doubt one of the most pivotal functionalities. An effective search will help the shoppers to find their needed products in the store at ease, which might result in a better customer experience and stimulated purchases. On the other hand, a poor search tool can slow down the process of looking for the items and even be the reason for the high buyers’ bounce rates. Additionally, Elasticsearch also has some outstanding features that can streamline your store’s operations:

  • Search optimization
  • Search autocomplete
  • Thesaurus management (synonyms, expansions)
  • Advanced price slider
  • Multi-select in layered navigation & swatches
  • Redirect to the product page if there is only one result
  • Auto spell checking & stopwords detection
  • Customizable filters
  • Search under various languages
  • Real-time data and analysis

Therefore, it’s essential to enhance the search feature in Magento 2 stores, and applying Elasticsearch is one of the best ways to do that. Unfortunately, Magento only integrates Elasticsearch in Magento Open Source 2.3.0 and Magento Commerce edition. For Magento Open Source 2.2.x, you need to install a proper module to have Elasticsearch’s powerful features. Now, there are several options when it comes to Magento 2 Elasticsearch module, both free and paid. In this post, we will show you the guide to installing a free one.

What To Do Before Setting Up Elasticsearch

Before setting up Elasticsearch for your Magento store, you should ensure that you meet the following prerequisites:

  1. Upgrade Magento Version: As mentioned earlier, if you are using an older version of Magento (2.3.x or below), you should consider upgrading to Magento 2.4.x. This is because Elasticsearch is a required component for Magento 2.4.x and above.
  2. Install Elasticsearch: Make sure to install Elasticsearch before upgrading to Magento 2.4.x. The recommended Elasticsearch version for Magento 2.4.x is 7.6.x. This version is known to be compatible and stable with the latest Magento release.
  3. In addition to a compatible Magento Elasticsearch version, you should also have:
  • Web Server (Apache or Nginx)
  • MySQL
  • Java Software Development Kit (JDK)
  • Firewall and SELinux

Before you proceed with the Elasticsearch setup, it’s essential to check the official documentation for Magento and Elasticsearch for any specific instructions or updates regarding the installation and configuration process. Following the guidelines provided by Magento and Elasticsearch will help ensure a smooth integration and functioning of Elasticsearch with your Magento store.

How To Install Elasticsearch In Ubuntu

As you might know, Elasticsearch is built on Java, and it requires at least Java 8 (1.8.0_131 or later) to run properly. Therefore, first and foremost, we need to install Java 8 on all the nodes in the cluster.

  • Step 1: Updating your system: sudo apt-get update
  • Step 2: Installing Java with sudo apt-get install default-jre
  • Step 3: Checking your Java version the and you will get the output similar to the following:
magento 2 elasticsearch tutorial
  • Step 4: Installing Elasticsearch

– First, you must add Elasticsearch’s signing key to verify the downloaded package (or skip this step if you’ve already installed the packages from Elasticsearch):
wget -qO – | sudo apt-key add –

– Second, you need to install the apt-transport-https package:
sudo apt-get install apt-transport-https

– Third, you need to add the repository definition to your system:
echo “deb stable main” | sudo tee -a /etc/apt/sources.list.d/elastic-6.x.list

– Fourth, updating your repositories and install Elasticsearch:
sudo apt-get update
sudo apt-get install elasticsearch

– Fifth, updating elasticsearch library:
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin install analysis-icu
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin install analysis-phonetic

  • Step 5:  Activating Elasticsearch:

sudo -i service elasticsearch start

  • Step 6: Configuring Elasticsearch memory:

sudo gedit /etc/elasticsearch/jvm.options

Notes: Deleting 2 lines Xms4g and Xmx4g

  • Step 7:  Fixing too_many_clauses error

sudo gedit /etc/elasticsearch/elasticsearch.yml

Adding the code “indices.query.bool.max_clause_count: 10024” on the bottom of the page.

  • Step 8: Testing how Elasticsearch works

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

Like the index of a book, the indexes here are made out of letters. Owing to that, when the customers do the search, the Elasticsearch will produce matches as soon as the query string is entered.

How To Uninstall Elasticsearch In Ubuntu

In order to remove the Elasticsearch, you can run the following commands:

sudo apt-get –purge autoremove elasticsearch
sudo rm -rf /var/lib/elasticsearch/
sudo rm -rf /etc/elasticsearch

How To Install Magento 2 Elasticsearch Module

To begin with, you must select a proper Elasticsearch version for your Magento store, and then run the correlative composer require:

(Notes: Here we are using the free ElasticSuite)

  • Magento 2.0.x Open Source/ Commerce => ElasticSuite 2.1.x latest release: composer require smile/elasticsuite ~2.1.0
  • Magento 2.1.x Open Source/ Commerce => ElasticSuite 2.3.x latest release : composer require smile/elasticsuite ~2.3.0
  • Magento 2.2.x Open Source/ Commerce => ElasticSuite 2.6.x latest release : composer require smile/elasticsuite ~2.6.0
  • Magento 2.3.x Open Source/ Commerce => ElasticSuite 2.7.x latest release : composer require smile/elasticsuite ^2.7.0

Then, you must run composer update to update the Magento library.

Next, you need to run the command: php bin/magento setup:upgrade to update the Elasticsearch module.

Finally, don’t forget running the command: php bin/magento indexer:reindex to synchronize all products in your catalog with Elasticsearch.

How To Configure Elasticsearch in Magento 2

Step 1: Log in to your Magento 2 backend.

Step 2: Navigate to STORES, click on Configuration (under Settings)

Step 3: Expand the CATALOG section and click on Catalog, choose Catalog Search on the right menu

Step 4: Complete Magento 2 Elasticsearch related fields

  • Enter the Minimum Query Length (length of entered text on the search bar) at which the store starts using the Elasticsearch.
  • Enter the Maximum Query Length (length of entered text on the search bar), beyond that the Elasticsearch is disabled.
  • Number of top search results to cache means the number of popular searches that should be stored.
  • Set Enable EAV Indexer to Yes for better indexation speed.
  • Set Search Engine to Elasticsearch or Elasticsearch 5.0+ (more recommended).
  • Enter Autocomplete Limit, the maximum number of search results displayed by the search autocomplete function.
  • Type your hostname or your server’s IP address, where the Elasticsearch is running, in Elasticsearch Server Hostname.
  • Enter Elasticsearch Server Port. Leave Elasticsearch Index Prefix field by default or set a unique one.
  • Set Enable Elasticsearch HTTP Auth to Yes, and then enter the username and password.
  • Choose specific Elasticsearch Server Timeout in seconds.
  • Change Enable Search Suggestions to Yes to display search suggestions.
  • Set Search Suggestions Count, the number of suggestions displayed with each search.
  • Set Show Results Count for Each Suggestion to Yes to display the count for each suggestion.
  • Set Enable Search Recommendations to Yes to display the recommendations for search strings.
  • Specify Search Recommendations Count.
  • Set Show Results Count for Each Recommendation to Yes to display the count for results with suggestions.

How to Fix Elastic Search Error After Updating To Magento 2.4.5-p1

Upgrading Magento to version 2.4.5-p1 is crucial for the smooth operation of your online store. However, during the upgrade process, some users may encounter an Elasticsearch error that affects catalog features like product filtering and sorting. This error is often caused by certain product attributes being incorrectly set as filterable in search. By following these steps, you can ensure that your Magento store functions optimally after the upgrade.

Elasticsearch Common Error After Updating To Magento 2.4.5-p1

Elasticsearch is a crucial requirement for Magento 2.4.x. When upgrading Magento from 2.3.x to 2.4.5-p1, it is essential to note that certain catalog features may not function correctly, such as product filtering or sorting. In case you encounter issues, you can refer to the exception.log file, where you might find Elasticsearch-related errors. Here’s an example:

Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [activity] in order to load field data by uninverting the inverted index. Note that this can use significant memory

During the investigation, it was discovered that the mentioned issue is caused by the upgrade process. Specifically, certain product attributes that are of text/varchar type are incorrectly set as filterable in search, leading to errors like the one mentioned (e.g., activity). To resolve this, it is necessary to search for these attributes in your database and update their statuses accordingly.

How to Resolve Elastic Search Error after updating to Magento 2.4.5-p1

Here are the queries that can help you identify these attributes:

select * from catalog_eav_attribute cea
inner join eav_attribute ea on ea.attribute_id = cea.attribute_id
where (cea.is_searchable = 1 or cea.is_searchable = 2 or is_filterable = 1 or is_filterable_in_search = 1) and (ea.backend_type = 'text' or ea.backend_type = 'varchar')

After obtaining the list of attribute IDs, the next step is to modify the attribute data for the affected attributes, specifically the is_filterable_in_search and is_filterable properties. Here’s how you can proceed:

Start by backing up your database to ensure data integrity.

Use the following query to update the attribute data:

UPDATE catalog_eav_attribute SET is_filterable_in_search = 0, is_filterable = 0 WHERE attribute_id IN (attribute_id_1, attribute_id_2, ...);

Replace attribute_id_1, attribute_id_2, and so on with the actual attribute IDs obtained from the previous step. This query sets the is_filterable_in_search and is_filterable properties to 0, effectively disabling the filterable status for these attributes in search.

Or you can use this combine query:

UPDATE `{$connection->getTableName('catalog_eav_attribute')}`
SET is_filterable_in_search = 0, is_filterable = 0
WHERE attribute_id IN (
    SELECT attribute_id FROM `{$connection->getTableName('eav_attribute')}` WHERE entity_type_id = 4 AND (backend_type = 'varchar' OR backend_type = 'text')
AND (is_filterable_in_search = 1 OR is_filterable = 1)

After executing the query, it’s essential to clear the cache and reindex the affected data using the Magento command line interface. Run the following commands in your terminal or command prompt:

php bin/magento cache:clean
php bin/magento indexer:reindex

These commands will ensure that the changes take effect and the updated attribute settings are reflected in your Magento installation.

By following these steps, you can successfully resolve the issue related to incorrectly set filterable attributes in the search functionality of your Magento store.

Wrap Up

We have shown detailed instructions to install Elasticsearch in Ubuntu, install Magento 2 Elasticsearch module, and configure Elasticsearch on Magento 2 backend. Configuring Elasticsearch in Magento 2 allows you to leverage the power of this robust search engine for faster and more efficient catalog searches. By meeting the prerequisites and following the installation steps, you can seamlessly integrate Elasticsearch into your Magento 2 store, providing a better shopping experience for your customers and improving overall store performance. If you are not clear on anything, just leave a comment so we can help!

Read More:

How To Add Quantity Increment Buttons To Product Page In Magento 2?

How To Add Custom Validate Field In Magento 2 Form?

How To Display Order Information In Checkout Success Page In Magento 2?

How To Add Contact Form To CMS Page In Magento 2?

How To Customize Magento 2 Checkout Shipping Address Form

Leave a Reply

Your email address will not be published. Required fields are marked *