1

I have just upgraded to Magento ver. 2.3.3 (from 2.2.8) with Elasticsearch and I am having an issue with my custom module regarding the sort by options.

Elasticsearch information:

{
  "name" : "yWDvGWs",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "0L1pnmTiQBaWL21J5p5iPw",
  "version" : {
    "number" : "5.6.12",
    "build_hash" : "cfe3d9f",
    "build_date" : "2018-09-10T20:12:43.732Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.1"
  },
  "tagline" : "You Know, for Search"
}

My module contains two plugins as following (di.xml):

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
        <plugin name="custom_custom_block_toolbar" type="Vendor\Module\Plugin\Catalog\Block\Toolbar" />
    </type>

    <type name="Magento\Catalog\Model\Config">
        <plugin name="custom_catalog_model_config" type="Vendor\Module\Plugin\Catalog\Model\Config" />
    </type>

</config>

Here is the plugin for the Magento\Catalog\Block\Product\ProductList\Toolbar (Toolbar.php):

<?php
namespace Vendor\Module\Plugin\Catalog\Block;

class Toolbar
{
    public function aroundSetCollection(
    \Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
    \Closure $proceed,
    $collection
    ) {
    $currentOrder = $subject->getCurrentOrder();
    $result = $proceed($collection);
    if ($currentOrder) {
        if ($currentOrder == 'price_high_to_low') {
            $subject->getCollection()->setOrder('price', 'desc');
        } elseif ($currentOrder == 'price') {
            $subject->getCollection()->setOrder('price', 'asc');
        } elseif ($currentOrder == 'name') {
            $subject->getCollection()->setOrder('name', 'asc');
        } elseif ($currentOrder == 'name_z_a') {
            $subject->getCollection()->setOrder('name', 'desc');
        } elseif ($currentOrder == 'news_to_date'){
            $subject->getCollection()->setOrder('news_to_date', 'desc');
        } elseif ($currentOrder == 'newest_news_to_date'){
            $subject->getCollection()->setOrder('news_to_date', 'asc');
        } elseif ($currentOrder == 'popularity_by_rating'){
            $subject->getCollection()->setOrder('popularity_by_rating', 'desc');
        }
    }
    return $result;
    }
}

And here is my plugin for Magento\Catalog\Model\Config (Config.php):

<?php

namespace Vendor\Module\Plugin\Catalog\Model;

class Config
{
    public function afterGetAttributeUsedForSortByArray(
    \Magento\Catalog\Model\Config $catalogConfig,
    $options
    ) {
        $options['price'] = __('Price : Low - High');
        $options['price_high_to_low'] = __('Price : High - Low');
        $options['news_to_date'] = __('Date : Newest');
        $options['newest_news_to_date'] = __('Date : Oldest');
        $options['name'] = __('Name : A-Z');
        $options['name_z_a'] = __('Name : Z-A');
        $options['position'] = __('Our Favourites');
        asort($options);
        return $options;

    }

}

The module was working as expected on my Magento ver. 2.2.8 store. After the upgrade, I get this error message on the frontend when trying to filter by the newly added sort by's:

Sorry, search engine is currently unavailable

I also see this error in the debug.log log file:

[2019-11-21 11:39:25] main.ERROR: {"error":{"root_cause":[{"type":"query_shard_exception","reason":"No mapping found for [price_high_to_low] in order to sort on","index_uuid":"NJTUipntQBKzkKSBSWTvxg","index":"magento22_product_1_v113"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"magento22_product_1_v113","node":"yWDvGWsDR2qYSsNUNvZsKg","reason":{"type":"query_shard_exception","reason":"No mapping found for [price_high_to_low] in order to sort on","index_uuid":"NJTUipntQBKzkKSBSWTvxg","index":"magento22_product_1_v113"}}]},"status":400} [] []

Any help towards fix will be appreciated.


UPDATE:

Based on the answer, I have upgraded the Elasticsearch to the latest version. This hasn't fixed the problem. This issue still persists. Current Elasticsearch information:

{
  "name" : "6WK1xO7",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "5vL1wHvHQt29HWV-xlQ2XQ",
  "version" : {
    "number" : "6.8.4",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "bca0c8d",
    "build_date" : "2019-10-16T06:19:49.319352Z",
    "build_snapshot" : false,
    "lucene_version" : "7.7.2",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}
5
  • Are you using Elasticsearch? – verheesj Nov 21 '19 at 12:29
  • I am using Elasticsearch. Edited the question with this additional information. – Greg Nov 21 '19 at 12:34
  • 1
    please run bin/magento indexer:reindex and let me know if you get any errors please – verheesj Nov 21 '19 at 12:53
  • Indexing hasn't fixed the problem. – Greg Nov 21 '19 at 13:03
  • I have more or less the same problem. I have the feeling that it is related to if the sorting type is an attribute or not. Like I have a sort filter called "beststellers", but there is no attribute for bestsellers in my store, my filter grabs it right from the database. And I also get this exception.... – hallleron Dec 16 '20 at 9:56
0

I suggest first updating your Elasticsearch, then seeing if this resolves it as Magento claims to support 6.x, and does not recommend versions 2.x and 5.x (albeit providing support).

[...]

Magento Commerce version 2.3.x supports the following Elasticsearch versions:

  • Magento 2.3 uses Elasticsearch PHP client version 6.1.

[...]

Please see: Technologies Magento can use

2
  • I will update the Elasticsearch version and update you here. Thanks, useful suggestion. – Greg Nov 21 '19 at 13:04
  • I have updated the Elasticsearch, however, I still get the same problem. – Greg Nov 21 '19 at 13:37
0

Custom Sort with ElasticSearch in Magento issue can be fixed by following these simple steps:

  • Navigate to Stores->Attributes->Product in Admin.
  • Add New Attribute.
  • In Properties->Attribute Properties:
    • Add Default Label (Eg: Price: Low to High)
    • Select the Catalog Input Type for Store Owner (Eg: Price) (In other cases Text Field is enough).
  • In Properties->Advanced Attribute Properties:
    • Add Attribute Code (Eg: price_asc).
  • In Storefront Properties->Storefront Properties:
    • Select User for Sorting in Product Listing to Yes

Once done with the above steps run the following commands,

bin/magento indexer:reindex catalogsearch_fulltext

bin/magento indexer:reindex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.