Magento - Exclude Search Term

Magento - Exclude Search Term

September 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Exclude Search Term

Excluding Certain Words from Magento Search

In Magento, you can exclude specific search terms using a combination of custom code and configuration changes. This is particularly useful for preventing common stop words (like “the”, “and”, etc.) or product categories (like “hats”) from cluttering your search results.

Understanding the Problem

Let’s say you have an e-commerce store that sells various types of hats. If a customer searches for “black hats”, Magento would return all black products, which is usually what you want. However, if you add a product called “Baseball Hats” and someone searches for just “hats”, your search will now include this new category as well.

The goal is to exclude the word “hats” from the search term itself, so that only the color (“black”) is considered during the search. This ensures that results are more relevant.

The Solution: Overriding Magento’s Search Functionality

To achieve this exclusion, you’ll need to override Magento’s default search functionality with a custom module. Here’s how:

Step 1: Create a Custom Module

In your project, create a new folder under app/code/. Name it something like YourCompany_HatsSearch.

Step 2: Override the Search Term

Open the VendorName.php file in this new directory and extend the Magento\Framework\Search\Request\QueryInterface:

namespace YourCompany\HatsSearch\Model;

class Query extends \Magento\Framework\Search\Request\Query
{
    public function getTerm()
    {
        $term = parent::getTerm();

        // Exclude the word 'hats' from the search term
        if (strpos($term, ' hats') !== false) {
            return str_replace(' hats', '', $term);
        }

        return $term;
    }
}

This code checks if the search term contains the string ” hats”. If it does, the function removes this part before returning.

Step 3: Declare Your Module

In your app/code/YourCompany/HatsSearch/etc/module.xml, add the following:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="YourCompany_HatsSearch"/>
</config>

Step 4: Reindex and Test

After saving these changes, reindex your search index using the command:

php bin/magento indexer:reindex

Then test the search functionality. When a user searches for “black hats”, only products with the color “black” should appear.

Best Practices and Considerations

Remember to thoroughly test this solution in a staging environment before deploying it on your live site. Also, exclude stop words responsibly—too many exclusions can limit the ability of users to find relevant products.

For more information about Magento’s search functionality and customization options, I recommend checking out the official documentation at https://laravelcompany.com/magento-developers-guide/search.html.

Conclusion

By overriding Magento’s search query function and implementing a simple string exclusion logic, you can effectively filter out certain words from your search results. This technique is particularly useful for excluding common stop words or product categories that aren’t necessary for relevant searches.

While this solution requires some development expertise, it provides a powerful way to fine-tune the Magento search experience for your specific use case. As with any customization in Magento, always ensure you have a backup before making changes and keep your code up-to-date with Magento’s latest security patches.

Happy coding! Let me know if you have any other questions about Magento development or related topics at https://laravelcompany.com.

Catalog Database Extensions Configuration

Our network