0

I am using Magento 2.4.1 search engine is elasticsearch7 selected in catalog config. I wanted to add custom sorting options on the category page like sort by price low to high and newest etc. Now I am using the below code which is working for the price but not with the newest.

<?php

namespace [Vendor]\[Module]\Block\Product\ProductList;

class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar
{

    /**
     * Set collection to pager
     *
     * @param \Magento\Framework\Data\Collection $collection
     * @return \Magento\Catalog\Block\Product\ProductList\Toolbar
     */
    public function setCollection($collection)
    {

        $this->_collection = $collection;

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

        
        $limit = (int)$this->getLimit();
        if ($limit) {
            $this->_collection->setPageSize($limit);
        }
        if ($this->getCurrentOrder()) {
            if (($this->getCurrentOrder()) == 'position') {
                $this->_collection->addAttributeToSort(
                    $this->getCurrentOrder(),
                    $this->getCurrentDirection()
                );
            } elseif ($this->getCurrentOrder() == 'price_desc') {
                $this->_collection->setOrder('price', 'desc');
            } elseif ($this->getCurrentOrder() == 'price_asc') {
                $this->_collection->setOrder('price', 'asc');
            } elseif ($this->getCurrentOrder() == 'newest') {
                $this->_collection->setOrder('created_at', 'desc');
                //NOT WORKING
            }
        }
        return $this;
    }

}

Giving me this error in exception.log

main.CRITICAL: {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"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 [created_at] in order to load field data by uninverting the inverted index. Note that this can use significant memory."}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"magento2_product_1_v84","node":"YYnx_-CIRyOYDAqvIIU_BQ","reason":{"type":"illegal_argument_exception","reason":"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 [created_at] in order to load field data by uninverting the inverted index. Note that this can use significant memory."}}],"caused_by":{"type":"illegal_argument_exception","reason":"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 [created_at] in order to load field data by uninverting the inverted index. Note that this can use significant memory.","caused_by":{"type":"illegal_argument_exception","reason":"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 [created_at] in order to load field data by uninverting the inverted index. Note that this can use significant memory."}}},"status":400} {"exception":"[object] (Elasticsearch\Common\Exceptions\BadRequest400Exception(code: 400):

Any help would be great.

4
  • Check this answer for a simple way to enable created_at sorting in frontend magento.stackexchange.com/a/287793/7863 – paj Dec 30 '20 at 15:06
  • Yes, that works but Should I change the value of the database table OR there is a better way. That's why I am waiting if someone knows any other way – Klaus Mikaelson Dec 30 '20 at 15:14
  • 1
    This sounds like it is related to the officially confirmed bug #31043. – zigojacko Feb 4 at 12:54
  • Yes because maybe when I used addAttributeToSort I was not getting any error but the product list was staying as it is when I try to sort by newest. If you find a correct solution please post your answer here so It can help others. – Klaus Mikaelson Feb 4 at 13:28
0

I don't know this is the correct way to do this or not. I was waiting for someone to give a better answer but not getting one. So I am using this for now.

# Get the attribute_id of 'created_at'
select attribute_id from eav_attribute where attribute_code = 'created_at' and entity_type_id=4;

# Set frontend_label
update eav_attribute set frontend_label = 'Created At' where attribute_id=112;

# Set used_for_sort_by
update catalog_eav_attribute set used_for_sort_by = 1 where attribute_id=112;
1
  • This does not seem to be working in the current 2.4 version.. Is this all you did on the MySQL end? No other file changes? – A. Lal Mar 8 at 13:40
0

https://marketplace.magento.com/sparsh-magento-2-advanced-sorting-extension.html

This works with 2.4.1 - Makes the sort by functionality actually work as expected.

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.