Using elasticsearch

Version

6.0.0 or newer

As soon as several thousand data sets are used in a project it makes sense to deal with Elasticsearch. Elasticsearch 7.1 or newer is required. The Elasticsearch integration for shopware is in the shopware/elasticsearch bundle. If this is not available in your project you can simply add it via composer require shopware/elasticsearch.

To activate Elasticsearch the following environment variables have to be set:

  • SHOPWARE_ES_HOSTS A comma separated list of Elasticsearch hosts. You can find the possible formats here
  • SHOPWARE_ES_ENABLED This variable activates the search via Elasticsearch. (Possible values 1 & 0)
  • SHOPWARE_ES_INDEXING_ENABLED This variable activates the indexing to Elasticsearch. (Possible values 1 & 0)

To start the indexing you have to execute the following commands:

  1. php bin/console dal:refresh:index
  2. php bin/console messenger:consume
  3. php bin/console es:create:alias [optional]

What happens is this:

  • php bin/console dal:refresh:index.
    • This command builds all indices for the DAL. This now also includes the Elasticsearch index. It initialized via the class Shopware\Elasticsearch\Framework\Indexing\EntityIndexer which iterates all entities that have to be indexed to Elasticsearch. Instead of selecting all data directly from the database and synchronizing them step by step to Elasticsearch, it puts \Shopware\Elasticsearch\Framework\Indexing\IndexingMessage into the \Symfony\Component\Messenger\MessageBus. These messages can then be processed by one or more workers at the same time.
  • php bin/console messenger:consume
    • This command starts a message worker which can process the \Shopware\Elasticsearch\Framework\Indexing\IndexingMessage, generated by the indexer. This is done via the \Shopware\Elasticsearch\Framework\Indexing\IndexingMessageHandler class. As soon as all data is synchronized to Elasticsearch, the aliases for the indices have to be created.
  • php bin/console es:create:alias [optional]
    • This command creates the aliases in Elasticsearch with which the DAL communicates. In a productive system this should be done via the scheduled task \Shopware\Elasticsearch\Framework\Indexing\CreateAliasTask. This task checks every 5 minutes whether a new index has been created for which an alias is to be created. However, during development you can use this command to create the aliases directly to bypass the 5 minute interval.

Was this article helpful?