Magento 2顶部搜索框结果按商品名称排序而非相关性实现问询
Hey Steve, I’ve dealt with this exact headache in Magento 2—trying to swap the default "Relevance" sort for the top bar search with product name, only to have results vanish? Yeah, that’s because the full-text search relies on specific indexing logic, and brute-forcing the sort field without handling the underlying context breaks things. Let’s walk through two solid solutions that work without breaking search functionality.
Why Directly Modifying "Relevance" Breaks Results
The top bar search uses the catalogsearch_fulltext index, where "Relevance" is calculated based on search term match scores across indexed fields. When you overwrite the sort field directly without adjusting how the search request is built, Magento tries to sort on a field that isn’t properly included in the search context, leading to empty results.
Method 1: Use a Plugin to Modify the Search Request Builder
This is the cleanest, most upgrade-safe way to override the default sort order. We’ll intercept the search request before it’s executed and swap the sort parameter.
Create the Plugin Class
In your custom module (create one if you don’t have it), add this file atapp/code/YourVendor/YourModule/Plugin/Search/Model/SearchRequestBuilder.php:<?php namespace YourVendor\YourModule\Plugin\Search\Model; use Magento\Framework\Search\Request\SortInterface; use Magento\Search\Model\SearchRequestBuilder; class SearchRequestBuilderPlugin { public function afterBuild(SearchRequestBuilder $subject, $result) { // Check if this is the fulltext search (top bar search uses this) if ($result->getIndex() === 'catalogsearch_fulltext') { $sorts = $result->getSorts(); // Remove the default Relevance sort unset($sorts['relevance']); // Add Product Name as the default sort (ascending order) $sorts['name'] = [ 'direction' => SortInterface::DIRECTION_ASC, 'field' => 'name', 'type' => SortInterface::TYPE_TEXT, ]; $result->setSorts($sorts); } return $result; } }Register the Plugin
Add this to your module’setc/di.xmlfile:<?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\Search\Model\SearchRequestBuilder"> <plugin name="yourvendor_yourmodule_search_request_builder" type="YourVendor\YourModule\Plugin\Search\Model\SearchRequestBuilderPlugin"/> </type> </config>Flush Cache & Reindex
Run these commands in your terminal:bin/magento cache:flush bin/magento indexer:reindex catalogsearch_fulltext
Method 2: Override the Search Result Block
If you prefer working with block overrides instead of plugins, you can change the default sort order directly in the search result block.
Override the Result Block
Create this file in your custom module atapp/code/YourVendor/YourModule/Block/CatalogSearch/Result.php:<?php namespace YourVendor\YourModule\Block\CatalogSearch; use Magento\CatalogSearch\Block\Result as CoreResult; class Result extends CoreResult { protected function _construct() { parent::_construct(); // Set default sort to product name (ascending) $this->setDefaultOrder('name'); $this->setDefaultDirection('asc'); } // Optional: If you want to remove "Relevance" from the sort options entirely public function getAvailableOrders() { $orders = parent::getAvailableOrders(); unset($orders['relevance']); return $orders; } }Register the Block Override
Add this to your module’setc/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"> <preference for="Magento\CatalogSearch\Block\Result" type="YourVendor\YourModule\Block\CatalogSearch\Result"/> </config>Flush Cache & Test
Runbin/magento cache:flushand test the top bar search—results should now default to sorting by product name.
Key Notes to Avoid Issues
- Index Check: Ensure the
namefield is included in thecatalogsearch_fulltextindex. You can verify this in Admin > Stores > Attributes > Product, edit the "Name" attribute, and check that "Use in Search" and "Used in Fulltext Search" are set to Yes. - Multi-Language Stores: If you have multiple store views, the
namefield will automatically respect the current store’s locale, so sorting will be correct for each language. - Upgrade Safety: Plugins are preferred over direct class overrides because they’re less likely to break during Magento version updates.
内容的提问来源于stack exchange,提问作者Steve

