7

How do I change Magento 2 mini search logic from OR to AND?

Right now if I search for 'Shoe laces', it returns all products with terms either 'shoe' or 'laces'.

I need a Fulltext search so that only products with full-term 'Shoe laces' are returned. How do I achieve this?

4 Answers 4

6

I found the solution , create a file search_request.xml in your module's etc folder ,

copy the entire node from vendor/Magento/CatalogSearch/etc/search_request.xml

and change

<queryReference clause="**should**" ref="search" />

to this

<queryReference clause="**must**" ref="search" />

do not forget to add sequence in your module file.

in case you installed your module and added after that, just change your module version and run setup:upgrade again , thanks

3
  • Please read your answer after posting it. The code samples haven't been visible. I changed that now. Jun 21, 2018 at 14:17
  • Please check this link eddiemay.me.uk/2019/06/20/magento-2-and-or-search-operators
    – lavb
    Feb 20, 2020 at 17:26
  • Hi, I followed this and now the search synonyms are not working. If I add synonyms: read,rid then searching read or rid comes up with no result. Please help.!
    – ishu
    Feb 25, 2020 at 6:29
3

For anyone who is looking into a working solution for Magento >2.4.0.

  1. First extend Magento\Elasticsearch\SearchAdapter\Query\Builder\Match by adding this to your di.xml (<VENDOR>/<Module>/etc/frontend/di.xml)
<preference for="Magento\Elasticsearch\SearchAdapter\Query\Builder\Match"
                type="<VENDOR>\<Module>\SearchAdapter\Query\Builder\Match" />
  1. Create the file <VENDOR>\<Module>\SearchAdapter\Query\Builder\Match.php and copy the content from Magento\Elasticsearch\SearchAdapter\Query\Builder\Match

In the buildQueries() function change the condition building (line 157) to this:

if($matchCondition != 'match_phrase_prefix'){
                $field = [
                    'query' => $transformedValue,
                    'boost' => $match['boost'] ?? 1,
                    'operator' => 'and'
                ];
            } else {
                $field = [
                    'query' => $transformedValue,
                    'boost' => $match['boost'] ?? 1
                ];
            }
            $conditions[] = [
                'condition' => $queryValue['condition'],
                'body' => [
                    $matchCondition => [
                        $resolvedField => $field,
                    ],
                ],
            ];

I created a GIST for clarification https://gist.github.com/JEreth/dd415dfc796fb6a3782f38cab022e607

Thanks for the answers above as they helped me to find this solution.

1
  • 1
    I can approve that solution works for Magento 2.4.2. Very nice. Many thanks! Nov 26, 2021 at 12:50
1

you have to set AND operator in your elastic search JSON queries, Override the module-elasticsearch/SearchAdapter/Query/Builder/Match.php. file in your custom module and then The code you have to edit in the build function in line no 75

if($matchKey != 'match_phrase_prefix'){$matchQuery['operator'] = "AND";}

Also, make sure you are not setting the operator in match_phrase_prefix.

After this, you'll get exact matches in the catalog search.

This has been tested well for Magento CC 2.4.1-p1

2
  • This is not working with M2.4.1 & M2.4.2, I am usingg M2.4.2 any suggestion what is the issue
    – Yogita
    Aug 18, 2021 at 5:52
  • what exact error you're getting and which file are you overriding? Aug 18, 2021 at 18:19
0

those settings will not work.

make it like below:

Magento\Elasticsearch\SearchAdapter\Query\Builder\Match.php

$conditions[] = [ 'condition' => $queryValue['condition'], 'body' => [ $condition => [ $resolvedField => [ 'query' => $value, 'boost' => isset($match['boost']) ? $match['boost'] : 1, 'operator' => 'and', ], ], ], ];

this will give you exact results.

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.