magento2: Search result relevance sorting never occurs
Perform a catalog search in Magento. The default sorting will be by relevance. However, no actual sorting is applied to the result set. If you search by relevance descending or ascending the results will not change.
I tracked the problem down to the following file: \vendor\magento\module-catalog-search\Model\ResourceModel\Fulltext\Collection.php at Line 349 inside the function _renderFiltersBefore()
The problem is that _renderFiltersBefore is called before the call to setOrder(), and the check if ($this->order && 'relevance' === $this->order['field']) {
will always have $this->order as null at this point.
This is most easily reproduced/examined by simply debugging a search request with breakpoints at both _renderFiltersBefore and setOrder inside the Collection.php file. You’ll see that setOrder is called too late. For some reason the check if ($attribute != 'relevance')
is performed in setOrder which prevents the order call from being added to the collection. It’s almost as if it was intentionally ignoring relevance.
If I added the line $this->getSelect()->order('search_result.'. TemporaryStorage::FIELD_SCORE . ' ' . Select::SQL_DESC);
to the _renderFiltersBefore function, the results or properly sorted by relevance. This was only done as a test to see if my suspicions were true, and not intended to be a solution.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 6
- Comments: 19 (5 by maintainers)
@AirmanAJK thank you for your feedback. Please, format this issue according to the Issue reporting guidelines: with steps to reproduce, actual result and expected result. Please, also identify which version of Magento you are running.