How do I change magento 2 minisearch 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 fulltext search, so that only product with full term ' Shoe laces' are returned. How do I achieve this?
Solved! Go to Solution.
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" />
I found the solution , create a file search_request.xml in your module's etc folder ,
copy the entire <request query="quick_search_container" index="catalogsearch_fulltext"> 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.
<module name="YOURMODULE" setup_version="1.0.0">
<sequence>
<module name="Magento_CatalogSearch"/>
</sequence>
</module>
in case you installed your module and added <sequence> after that, just change your module version and run setup:upgrade again , thanks
I did this , my module has etc/search_request.xml and I changed, clause="should" to clause="must" as you menitoned but nothing changed, it still takes the core vendor/magento/module-catalog-search/etc/search_request.xml only. How do I override the core vendor/magento/module-catalog-search/etc/search_request.xml. In my module.xml, I added this,
<sequence>
<module name="Magento_CatalogSearch"/>
</sequence>
but my search_request.xml is ignored, it takes the core search_request.xml only
I found the solution , create a file search_request.xml in your module's etc folder ,
copy the entire <request query="quick_search_container" index="catalogsearch_fulltext"> 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.
<module name="YOURMODULE" setup_version="1.0.0">
<sequence>
<module name="Magento_CatalogSearch"/>
</sequence>
</module>
in case you installed your module and added <sequence> after that, just change your module version and run setup:upgrade again , thanks
sorry I added sequence after installing the module, that is why it did not work, after upgrading the module version it worked like a charm
I implemnented this and it works. However for every keyword that a search synonym exists for NO results wll be found any longer. It seems like the adapted query now searches for products that matches 'keyword' AND 'synonym' => which ofcourse will never give any result. Any solution?
@rickert did you find a solution to this?
No I'm afraid not. My developer created additional custom code to create M1-like search functionality. Which took a lot of time (and money).
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.!