1

I´m in the middle of building a search module for Magento, and I´m currently facing a little bit of trouble, specifically regarding applying "Search Types" (Like, Fulltext or Combine) according to the admin configuration.

Here is my working query code:

$searchQuery = 'my search';
$storeId = '1';
$collection = Mage::getResourceModel("catalog/product_collection")->addAttributeToSelect("*");
$query = Mage::helper("catalogSearch")->getQuery();
$query->setStoreId($storeId);
$query->setQueryText($searchQuery);
$collection = $query->getSearchCollection();
$collection->addSearchFilter($searchQuery);
$collection->addAttributeToSelect("*");
$collection->addCategoryIds();
$collection->addFinalPrice();
$collection->addUrlRewrite();
$collection->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$collection->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$collection->setPageSize(20);

The only thing I need is to somehow define "Search Types" so it´s either "like", "fulltext" or "combine" depending on the shop configuration.

In other words, if the shop owner has set "Search Type" to "Combine(Like and Fulltext)" - then my query must also be search type "combine".

My problem is that I don´t know where to apply search type in the query.

I hope someone out there can help.

P.S. I normally work in Joomla, so I´m a complete novice when it comes to the Magento framework..

2

I finally found the correct solution. It turned out I was using the wrong approach regarding the query.

I managed to modify my query according to Ron´s answer to this thread: What's the proper way to call CatalogSearch from a custom Block in Magento?

Here is my final query, which performs a search according to the general Magento catalog search settings in admin:

$searchQuery = 'my search';
$shopidajax = '1';

$appEmulation = Mage::getSingleton('core/app_emulation');
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($shopidajax);

$searchText = $searchQuery;
$query = Mage::getModel('catalogsearch/query')->setQueryText($searchText)->prepare();
$fulltextResource = Mage::getResourceModel('catalogsearch/fulltext')->prepareResult(
    Mage::getModel('catalogsearch/fulltext'), 
    $searchText, 
    $query
);

$collection = Mage::getResourceModel('catalog/product_collection');
$collection->getSelect()->joinInner(
    array('search_result' => $collection->getTable('catalogsearch/result')),
    $collection->getConnection()->quoteInto(
        'search_result.product_id=e.entity_id AND search_result.query_id=?',
        $query->getId()
    ),
    array('relevance' => 'relevance')
);

$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);

$collection->setStore($storeId);
$collection->addAttributeToSelect('*');
$collection->addMinimalPrice();
$collection->addFinalPrice();
$collection->addTaxPercents();
$collection->addStoreFilter();
$collection->addUrlRewrite();
$collection->addCategoryIds();
$collection->setPageSize(20);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);

Note that $shopidajax should be the current active shop ID and $searchQuery should of course be the actual search query string.

0

In my case results are different from native search and from above methods I used filters like in layers (app\code\core\Mage\CatalogSearch\Model\Layer.php F:prepareProductCollection) and also add following filter for same results as on web:

$collection->getSelect()->order('relevance DESC');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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