Magento 2 (Elasticsearch as default search engine) uses "OR" condition to get search results when inputting two or more query words.

In some cases, we need to change the match mode from "OR" to "AND" in order to get more exact results.

Here is a simple method to implement it as below:

1. Change the match mode for all searchable fields

- Path: \Magento\Elasticsearch\SearchAdapter\Query\Builder\Match.php

- Description: add a new line ('operator' => 'and',) under 163 line in buildQueries function.

 

2. Change the match mode for specific fields

- Path: \Magento\Elasticsearch\SearchAdapter\Query\Builder\Match.php

- Description: add a new line ('operator' => ($match['field'] == 'name') ? 'or' : 'and',) under 163 line in buildQueries function. This code will change the match mode from "OR" to "AND" for product name field only.

Copyright ┬ę 2016-2022 azaleasoft.com. All rights reserved.