skip to Main Content

im trying to query a smarter search result, i checked online about Leveshtein and similiar_text but i believe Leveshtein algorithm is the best optionm how could i implement in my query the algorithm of LEvenshtein? I cant find any good resource on magento or even using this function on the attributeSelect, here is my initial code.

function queryBuy($string){
    $items = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect('name')
            ->addFieldToFilter(array(
                        array('attribute'=>'name','like'=> "%".$string."%"),
    ));

Should i make in Raw SQL? Does magento have any method available where i could use it?

2

Answers


  1. try it like this

    $items = Mage::getResourceModel('catalog/product_collection')
                ->addAttributeToSelect('name')
                ->addAttributeToFilter('name',
                            array('like'=> "% ".$string." %") //spaces on each side
        );
    

    you can also check this answer

    Login or Signup to reply.
  2. You can change any part of collection query with

    $collection->getSelect()
    

    Example

    $collection->getSelect()->where("some field LIKE %expression%"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search