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.
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.