How to improve search results within Magento Admin Global Records Search

In my previous post I started to research the problems of default search of records in Magento administrative panel.

We have discussed the problem of product name related search in Manage Products Grid and offered our own solution which allows to search products using random word order in a search request.

In this post I will continue to dwell in the topic of records search. This time we will take a close look at the problems that arise while using Global Record Search.

Magento administrative panel allows integrated comprehensive search through all the sections (Global Record Search), i.e. one can search for products, orders and clients in one search field.

Now we are trying to solve the problem of a products search. When random word order is used in the search request, Global Records Search will return no results just as it will not return any when product search is made according to its name in Manage Products Grid.

With global search action globalSearchAction() of Mage_Adminhtml_IndexController class is called.

This, in its turn, calls for search methods of Mage_Adminhtml_Model_Search_Catalog, Mage_Adminhtml_Model_Search_Customer and Mage_Adminhtml_Model_Search_Order::setStart models.

The call has the following form:

Mage_Adminhtml_Model_Search_*->setStart($start)
                              ->setLimit($limit)
                              ->setQuery($query)
                              ->load()
                              ->getResults();

Search results are combined into one array.

We need to improve the performance of the search mechanism (load() method) of each of them. We will start with Mage_Adminhtml_Model_Search_Catalog where the following process takes place:

$collection = Mage::helper('catalogsearch')->getQuery()->getSearchCollection()
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('description')
            ->addSearchFilter($this->getQuery())
            ->setCurPage($this->getStart())
            ->setPageSize($this->getLimit())
            ->load();

We will research  _getSearchEntityIdsSql() method being particularly interested in the following lines of code:

145:       ->where($resHelper->getCILike($attributeCode, $this->_searchQuery, $likeOptions));
165:       ->where($resHelper->getCILike($ifValueId, $this->_searchQuery, $likeOptions));
$resHelper = Mage::getResourceHelper('core');

We need to rewrite getCILike method. We will create a rewrite for Mage_Core_Model_Resource_Helper_Abstract in app/code/local/

Here we are interested in getCILike method.

and replace its whole content with:

public function getCILike($field, $value, $options = array())
{
    $quotedField = $this->_getReadAdapter()->quoteIdentifier($field);
$sql = "";
$x = $value;
$termsArr = preg_split('/\s+/',(string)$x);
foreach ($termsArr as $term){
$term = preg_replace('/%/','',$term);
$term = preg_replace("/'/",'',$term);
if (empty($sql)){
$sql = " ($quotedField LIKE " . $this->addLikeEscape($term, $options) . ")";
}else{
$sql .= " AND ($quotedField LIKE " . $this->addLikeEscape($term, $options) . ")";
}
}

$res = new Zend_Db_Expr($sql);
    return $res;
}

Now it's time to test it.

Stay tuned. Comment and share this article.