2

This question already has an answer here:

If a customer searches for "black dress" my current search results includes products with word "black" OR "dress" OR "black dress".

How can I only show products that have both words: "black" AND "dress"?

I tried to change the search type: like, fulltext, combine. Neither worked.

Thanks!

marked as duplicate by Fabian Schmengler, 7ochem, Marius Jul 17 '15 at 13:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Bear in mind that Magento's search is lame. You can either override the search controller to your likings or use a 3rd party extension for research. Either Lucene or Solr – Julien Lachal Jul 17 '15 at 13:09
5

In the method Mage_CatalogSearch_Model_Resource_Fulltext::prepareResult there is this code that splits the query string into words and generates a where condition, but it works only in like or combine mode.

        if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_LIKE
            || $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE
        ) {
            $helper = Mage::getResourceHelper('core');
            $words = Mage::helper('core/string')->splitWords($queryText, true, $query->getMaxQueryWords());
            foreach ($words as $word) {
                $like[] = $helper->getCILike('s.data_index', $word, array('position' => 'any'));
            }
            if ($like) {
                $likeCond = '(' . join(' OR ', $like) . ')';
            }
        }

Notice this line:

$likeCond = '(' . join(' OR ', $like) . ')';

You can rewrite the class and method and replace that line with

 $likeCond = '(' . join(' AND ', $like) . ')';
  • 1
    37s Marius! I'll beat you next time! – Richard Nov 25 '14 at 15:05
  • @Richard...Never...(insert evil laugh here). :) And a +1 from be for confirming my suspicions. – Marius Nov 25 '14 at 15:06
  • This is brilliant @Marius. I've been using the Catalog Search Refinement plugin by Activo for 2 years and have decided to test upgrading my store to version 1.9.3.4 - unfortunately, they haven't made the plugin compatible with this version of Magento so this quick fix has helped me a lot. – Liam McArthur Sep 1 '17 at 8:33
  • @Marius But sir it will not work for me – Amaresh Tiwari Sep 6 '17 at 10:15
3

app/code/core/Mage/CatalogSearch/Model/Resource/Fulltext.php has a function called prepareResult() that has a line that looks a bit like $likeCond = '(' . join(' OR ', $like) . ')'; You need that OR to be AND if you want both terms to be required. You could

  1. BAD edit app/code/core/Mage/CatalogSearch/Model/Resource/Fulltext.php to change it
  2. LESS BAD copy it to app/code/local/Mage/CatalogSearch/Model/Resource/Fulltext.php and change it there
  3. EVEN LESS BAD Extend Mage_CatalogSearch_Model_Resource_Fulltext with your own class and rewrite the class magento uses

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