Tell me, please, how can I programmatically override the search query? For example: the user enters "Any word" in the search field, and the search is performed using the "Custom word". And as a result on the search page we get the goods found on the query "Custom word".


2 Answers 2


Assuming you are using MySQL search provider.

Create a plugin and use the around Plugin Function technique.

class Match extends \Magento\Framework\Search\Adapter\Mysql\Query\Builder\Match {

In the aroundBuild method you can get the query value


And perform the queries you need there.

For much more information see these: https://webkul.com/blog/how-to-replace-magento2-default-search-engine/ http://www.ibnab.com/en/blog/magento-2/magento-2-get-one-search-result-by-sku

In Magento 2's default search provider, special symbols '-+~/\<>\'":*$#@()!,.?`=%&^' are replaced with an empty space ' '. ​This would lead to each space delimited search term being treated as "OR" conditions which I think is the thing you are trying to avoid. ​ If your looking to match exactly the ibnab in one of the attributes then the "get one search result by sku" may be a good starting place.

Do your query first for the exact match for a particular search query on a specific attribute id or column and if it has results set the match query to a SQL "in" condition on those id's?

$matchQuery = 'data_index IN('.implode(",", $result_id_array).')';
  1. Create a Custom Module: First, create a custom module in your Magento installation. You'll need to set up the necessary module files, including registration, configuration, and code files.

  2. Override Search Model: Create a plugin or interceptor for the search model to modify the search query. In Magento, the search query is typically handled by the Magento\Search\Model\Query class. You can use a plugin to intercept the getQueryText() method and replace the query with your "Custom word."

  3. Define the Plugin: In your module, define the plugin in your module's di.xml configuration file. This tells Magento which class and method to intercept.

    <!-- app/code/YourVendor/YourModule/etc/di.xml -->
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="Magento\Search\Model\Query">
            <plugin name="custom_search_query_override" type="YourVendor\YourModule\Plugin\Search\QueryPlugin" sortOrder="10" />
  4. Create the Plugin Class: Create the plugin class that will modify the search query.

    // app/code/YourVendor/YourModule/Plugin/Search/QueryPlugin.php
    namespace YourVendor\YourModule\Plugin\Search;
    class QueryPlugin
        public function beforeGetQueryText(\Magento\Search\Model\Query $subject)
            // Modify the search query here
            $customQuery = 'Custom word';
            return [$customQuery];
  5. Clear Cache: Clear the Magento cache to ensure your changes take effect.

With this setup, when a user enters any search term, your plugin will replace the entered query with "Custom word" before the actual search is performed. This approach allows you to programmatically control the search query without affecting the frontend user experience.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.