How to patch the critical ElasticSearch bug in the latest Magento 2.3.3 release

With the latest Magento 2.3.3 version release comes a critical ElasticSearch bug, pagination is completely broken when using the recommended ElasticSearch engine instead of MySQL (which is now deprecated). Read on to see how to fix this issue with the emergency patch Magento have released.

- Chris Pook, 12th October 2019

This is a frustrating issue. In what should have been a security fixes release we have a major breaking change which has slipped through all testing and also doesn't appear to be related to a security fix.

With ElasticSearch enabled as the search backend in Magento admin, clicking beyond the first page of search results will give you a "no products found" message on the front-end.

Elasticsearch pagination broken in Magento 2.3.3

Magento have released an emergency hotfix patch (see below) to address this, which we delve in to below and show you how to cleanly apply it as part of your Composer workflow.

Prepare the patch file

To cleanly apply the patch using Composer you'll first need to add the patch file to your repository. We suggest adding the file to the root of the repo, in a "patches" directory. Save the file with a relevant name and a ".patch" extension. For example "patches/elasticsearch-pagination.patch".

Once you have done this, you'll likely need to modify the patch file. Due to the way Magento keep their core codebase in one single GitHub repository and then split it out to separate repos for people to install via Composer, you will need to modify the file paths in the patch file. You will need to change all of the file path's in the patch to be relevant to the affected module's directory. Essentially removing app/code/<vendor>/<package> or vendor/<vendor>/<package> from the file.

Applying the patch with Composer

Installing Magento via Composer is the best-practise method advocated by Absolute Commerce, so lets cover that off first. Obviously you can't just apply the patch to the core code you have in the vendor directory as this is not checked in to version control.

What you need is to have Composer record that a patch has been applied and therefore to apply that same patch on all subsequent installs of the code. The application of patches is not something Composer does out of the box, however there is a useful add-on that does facilitate this.

To install the add-on run the following in your Magento root directory:

composer require cweagans/composer-patches

Composer will now download and add the add-on to your build, updating the composer.lock file in doing so. Next you can move on to actually applying the patch. Update the the "extra" node of the composer.json file to look as follows

"extra": {
    "magento-force": "override",
    "patches": {
        "magento/module-elasticsearch": {
            "Elasticsearch pagination bug 2.3.3": "patches/elasticsearch-pagination.patch"

Now running a composer update as normal will remove the standard magento/module-elasticsearch dependency, re-install it and apply the supplied patch. You should see the following output:

$ composer update
Gathering patches for root package.
Removing package magento/module-elasticsearch so that it can be re-installed and re-patched.
  - Removing magento/module-elasticsearch (100.3.3)
Gathering patches for root package.
Gathering patches for dependencies. This might take a minute.
  - Installing magento/module-elasticsearch (100.3.3): Loading from cache
  - Applying patches for magento/module-elasticsearch
    patches/elasticsearch-pagination.patch (Elasticsearch Pagination bug 2.3.3)
Writing lock file
Generating autoload files

The final step then is to commit everything, don't forget to add the new patches directory, the composer.json file and the composer.lock file.

Applying the patch on command line

If you're not using Composer to install your site, or you need to make a very quick hotfix to a live site before your next deployment, you can apply the patch directly.

First copy the patch file to your Magento root directory. As with the Composer method above you will need to modify the patch file contents to make all file paths relevant to the location of the files in your Magento codebase. If you are using Composer use the vendor/ path, otherwise use the app/code path.

With the file in place and the file paths corrected you can now run the following command to apply the patch:

patch < elasticsearch-pagination.patch

The patch will now either be applied, or you'll be met with a File to patch: prompt. In this case, enter the file path of the relevant file which it is listing (you can copy and paste) the patch from the output in to the prompt.

This should result in the following output:

$ patch < elasticsearch-pagination.patch
can't find file to patch at input line 5
Perhaps you should have used the -p or --strip option?
The text leading up to this was:
|diff --git a/vendor/magento/module-elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php b/vendor/magento/module-elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php
|index b15d99dcc2d..3ae2d384782 100644
|--- a/vendor/magento/module-elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php
|+++ b/vendor/magento/module-elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php
File to patch: vendor/magento/module-elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php
patching file vendor/magento/module-elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php

If you need to you can now commit the file modification in to your repository for subsequent deploys.

If you're having trouble with this, or any other issue please don't hesitate to get in touch!
location icon

Visit us

Mainyard Studios
35 Bow Rd, London E3 2AD