cancel
Showing results for 
Search instead for 
Did you mean: 

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

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

The issue I have is that we had modified our site (details below), so that if you search for "black shoes" it would only bring up black shoes, but in the last update, it now brings up anything with "black" OR "shoes" in the title.  I have also noticed since the update if I search for part of a word in the title it does not bring up results, which it used to.  For example "fit" brings up no results, where "outfit" does bring up results.

 

Back when we installed Magento 2.3.5-p1, I used an instruction from this forum to make the search logic AND not OR, but after an upgrade to 2.3.7-p2 it has undone the change I made to the file, so I made the change again and it no longer fixes the issue.  This was the old instruction:

 

You have to extend vendor/magento/module-catalog-search/etc/search_request.xml file 
in custom module. 
and change line 16: <queryReference clause="should" ref="search" /> 
to <queryReference clause="must" ref="search" />

In 2.3.7 and above, is there another way to do this?

3 REPLIES 3

Re: How do I change magento 2 search from OR logic to AND?

Hello bigbob2,

 

I hope you are doing well!

 

Yes, there is also another way to do this on 2.3.7 and above Magento versions. You can go with the below steps to use AND search instead of OR Logic.

 

You will have to perform the below steps to change Magento 2 search from OR logic to AND.

 

Firstly, you have to create a file > search_request.xml in your module's etc folder ,

Now, you have to copy the entire node: <request query="quick_search_container" index="catalogsearch_fulltext"> node from the, vendor/Magento/CatalogSearch/etc/search_request.xml

and change it:

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

to this:

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

Also, do not forget to add a sequence in your module file.

<module name="YOURMODULE" setup_version="1.0.0">

      <sequence>      

         <module name="Magento_CatalogSearch"/>

      </sequence>

   </module>

 

In case you have installed your Module and added <sequence> after that, just change your module version and run setup: upgrade again.

 

2nd Method:

 

You have to create a file with > <VENDOR> \ <Module>\SearchAdapter\Query\Builder\Match.php, and you should copy the content from,

Magento\Elasticsearch\SearchAdapter\Query\Builder\Match

 

Now, in the buildQueries() function, you will have to change the condition building on line '157' to the following:

 

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,

                    ],

                ],

            ];

Note: The above solution works great for Magento 2.4.2 and above versions.

 

I hope the above Method or the information will help you in finding a solution for your issue.

 

-------------------

Regards,
Rex M

Re: How do I change magento 2 search from OR logic to AND?



Thanks so much Rex for the detailed info.  In my 'vendor/Magento' I have a 'module-catalog-search' but I don't have just a 'CatalogSearch'.  Would this mean I was in the wrong place or do I have the correct folder, just under a different name? 



Re: How do I change magento 2 search from OR logic to AND?

 

T