3

We have installed Elastic Search 7 on a server running a Magento 2.3.5-p1 store and we can test it is successful (both in Magento 2 backend and on CLI) but when it is enabled, it changes the default product sort position in categories so that the oldest products appear first and the newest last.

When it is disabled (i.e. search set to MySQL), the default sort position is correct with newest products listed first.

This is when the sort position is set to the same for a range of products.

So if 6 products in a category all have the default position of 0 set, with Elastic Search enabled, the oldest of those 6 products is appearing at the top for some reason...

Is this normal and what is meant to happen with Elastic Search? Bear in mind, I'm not even on a search page, this is just default view in categories.

Any idea why this is being changed and what needs to be done to show newest products first like would be expected? I am not really sure where to begin with this, whether a change needs to be made or whether this doesn't sound right.

3

3 Answers 3

3

Not the actual answer but you can identify some ways.

Add two plugins one is for GetLoadedProductCollection and second is for what ElasticSearch returning ids.

File: di.xml

<type name="Magento\Catalog\Block\Product\ListProduct">
        <plugin name="VendorModule::logListProductQuery" type="Vendor\Module\Plugin\Catalog\Product\PluginListProduct"/>
</type>

File : PluginListProduct.php

namespace Vendor\Module\Plugin\Catalog\Product;
    
use Psr\Log\LoggerInterface;

class PluginListProduct
{
     
    public function __construct(
        LoggerInterface $logger
    )
    {
        $this->logger = $logger;

    }

     
    public function afterGetLoadedProductCollection(MageListProduct $subject, $result)
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/PluginListProductQuery.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        
        $logger->info($result->getSelect()->__toString());      

        return $result;
    }
}

Also, add a custom plugin it will help you to identify which ids ElasticSearch returning. This will only useful if ElasticSearch is using as Catalog Search engine.

File: di.xml

<type name="Magento\Elasticsearch\SearchAdapter\ResponseFactory">
        <plugin name="Vendor_Module::responseCheckFromElasticSearch"
                type="Vendor\Module\Plugin\ResponseCheck"
                disabled="false"/>
    </type>

File: ResponseCheck.php

<?php
namespace Vendor\Module\Plugin;

use Psr\Log\LoggerInterface;


class ResponseCheck
{   
    protected $logger;
 
    public function __construct(
        LoggerInterface $logger
    )
    {
        $this->logger = $logger;

    }


    public function beforeCreate(\Magento\Elasticsearch\SearchAdapter\ResponseFactory $subject, $result)
    {
        $docRaw = $docRawScore = array();
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/ElasticResponseCheck.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        if (!is_array($result) || empty($result)) return false;
        foreach ($result['documents'] as $rawDocument) {
            $docRaw[] = $rawDocument['_id'];
            $docRawScore[] = $rawDocument['_score'];
            
        }

        $logger->info('ID: ' . implode(',', $docRaw));
        $logger->info('SCORE: ' . implode(',', $docRawScore));
        $logger->info('*************************************** END *********************************************');

    }


}

Probably you will get following order by in PluginListProductQuery.log and compare these ids with what is returning in ElasticResponseCheck.log

  • With ElasticSearch as a search engine possible order by for Elastic search would be - ORDER BY FIELD(e.entity_id,103,102,101)

  • With MySQL as a search engine possible order by - ORDER BY cat_index.position asc, e.entity_id DESC LIMIT 4

Hope this helps.

0
0

I had this issue as well, so I assume it's normal. You can set the Default Product Listing Sort but the default Magento options are Position, Product Name, and Price; which doesn't really help.

Those settings can be found per category under the categories Display Settings tab in Catalog > Category > Category Name > Display Settings (Default Product Listing Sort By)

Or you can change it for all products in Stores > Configuration > Catalog > Catalog > Storefront (Product Listing Sort By)

1
  • Yeh this doesn't help because store is using default sort position across entire website and all categories. Default sort position is by 'Position' and these are all default 0 or 1. Enabling Elastic Search has just reversed the entire collection in every category for those set with the same 0 or 1 position. We need a solution that will allow us to retain the newest products first with Elastic Search enabled and none of the default Magento sort options can provide this.
    – zigojacko
    Commented Nov 25, 2020 at 14:44
0

I've finally been able to solve this issue. We need to change the Toolbar.php and you can find it here /public_html/vendor/magento/module-catalog/Block/Product/ProductList/.

There's a need to change the setCollection() function:

public function setCollection($collection)
{
    $this->_collection = $collection;

    $this->_collection->setCurPage($this->getCurrentPage());

    // we need to set pagination only if passed value integer and more that 0
    $limit = (int)$this->getLimit();
    if ($limit) {
        $this->_collection->setPageSize($limit);
    }
    if ($this->getCurrentOrder()) {
        if (($this->getCurrentOrder()) == 'position') {
            $this->_collection->setOrder($this->getCurrentOrder(),'desc');
        } else {
            $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
        }
    }
    return $this;
}

This way you'll be able to set their order. In mine website, I only order the products by desc, I don't give the other sort option (asc)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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