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


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).')';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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