nabeel shahzad

Sphinx and CakePHP

with 14 comments

For a project, I’ve decided to use the Sphinx search engine, and was looking for behaviors for CakePHP, to just make it much easier to implement. Since I’m using Cake 2.0, I could only find something that was for < Cake 1.3. So I decided to update it for use with Cake2.0, and it’s working beautifully with pagination.

It’s located in my github site:

The usage is exactly the same as the original (the link to it is above). The only thing is that it’s assuming you have the sphinxapi.php (which comes with the Sphinx source) extracted into Vendor/sphinxapi/sphinxapi.php (that’s where App::import() will look for it).

Be Sociable, Share!

Written by Nabeel

January 3rd, 2012 at 10:01 am

Posted in CakePHP,phpVMS

  • mohamed ali

    نبيل الشحات يبدو من اسمك انك مصري وعندك خبرة ممتازة بالفريم ورك كيك بي اتش بي
    لماذا لا تعمل مدونة بالعربي لكي نستفيد مما علمك الله :)

  • Anonymous

    Google Translate leads me to believe this is Arabic – which I don’t know :)

  • fiveninesixtwosix

    Hello Nabeel, thanks for this code. I got a very simple question for you. If I have these on my controller:

    $sphinx = array(‘matchMode’ => SPH_MATCH_ALL, ‘sortMode’ => array(SPH_SORT_EXTENDED => ‘@relevance DESC’));
    $results = $this->Film->find(‘all’, array(‘search’ => ‘search string here’, ‘sphinx’ => $sphinx));

    How would I pass/display the $result on my View (index.ctp)? Can you give me a sample code please? I would really really appreciate it. Thanks.


  • Anonymous

    Hi Lyman, you would do it as you normally do with a model find, just set it to the view and iterate through it. It returns the same results as a normal find call, just filtered to the search.

  • lyman castro

    i think i know what you meant. One more question if you don’t mind please. I followed the steps on this website in installing Sphinx: and made it work. I can search through command prompt, however, I get this error when I try to search from my CakePHP application: Notice (1024): Search query failed: connection to localhost:3312 failed (errno=10061, msg=No connection could be made because the target machine actively refused it.) [APPmodelsbehaviorssphinx.php. Do you have any idea why? Thanks.


  • Anonymous

    In that link, it looks like they configure it on port 9312 instead of 3312 (not sure why they changed it). I would change that port in the config, and restart searchd and try again.

  • lyman castro

    You’re a life saver Nabeel. I’ve been trying to figure this out for the past couple of weeks. Thank you so much. Take care my friend.

  • Anonymous

    No problem, glad it worked out!

  • Thomas Goeminne

    Hi Does this work with 2.3 ? I am trying to use it . The basic query work fine.

    $this->layout = ‘my_layout’;

    $sphinx = array(‘matchMode’ => ‘SPH_MATCH_ALL’, ‘sortMode’ => array(‘SPH_SORT_EXTENDED’ => ‘@relevance DESC’));

    $products = $this->Product->find(‘all’, array(‘search’ => ‘searchterm here’, ‘sphinx’ => $sphinx,’limit’ => 20,’page’ => 1));


    But when I try use it for paging results, I have this:

    $this->layout = ‘my_layout’;

    $pagination['Product'] = array(

    ‘limit’ => 20,

    ‘order’ => ‘hits DESC’);

    $pagination['Product']['sphinx'] = array(

    ‘matchMode’ => ‘SPH_MATCH_ALL’,

    ‘sortMode’ => array(‘SPH_SORT_EXTENDED’ => ‘@relevance DESC’));

    $pagination['Product']['search'] = ‘search term here’;

    $this->paginate = $pagination;

    $products = $this->paginate();


    This is also working, but very slow and when I look at the debug query, I can see it is not correct.

    I also see that the count query is being executed, but it counts all items in products table.

    This is generated as where query it also seem to have problem:

    WHERE `Product`.`id` IN (74022, 74081, 74102, 74159, 74276, 74292, 74301, 74398, 74469, 74479, 74500, 74511, 74543, 74618, 74886, 75053, 75066, 75073, 75074, 75145) ORDER BY FIND_IN_SET(`Product`.`id`, ’74022,74081,74102,74159,74276,74292,74301,74398,74469,74479,74500,74511,74543,74618,74886,75053,75066,75073,75074,75145′) ASC LIMIT 20

    And this is count query which seem to have problem:

    SELECT COUNT(*) AS `count` FROM `db`.`products` AS `Product` LEFT JOIN `db`.`advertisers` AS `Advertiser` ON (`Product`.`advertiser_id` = `Advertiser`.`id`) LEFT JOIN `db`.`pcategories` AS `Pcategory` ON (`Product`.`pcategory_id` = `Pcategory`.`id`) LEFT JOIN `db`.`brands` AS `Brand` ON (`Product`.`brand_id` = `Brand`.`id`) WHERE 1 = 1

    Can you send me a working example including model, controller and view?

  • nshahzad

    Hi Thomas,

    I haven’t used Cake in about 8 or 9 months or so, so I have not tested it with 2.3. What is incorrect about the generated queries? I’d imagine they’d be the same and that Cake would keep things backwards compatible for a minor version update.

    Do running those queries manually yield any results?

  • Thomas Goeminne

    seems like it is working but there is a query executed which counts all products in the database, and this is slowing down everything.

  • Thomas Goeminne

    The problem is it is counting all the records in the database, rather then returning the count of records in the search result. Which would be returned by sphinx

  • Thomas Goeminne


    I found out what was the problem. I had previously changed something and now is working….. Thanks

  • karmicdice


    This is cool!
    But, it is returning empty results?? Any ideas why? I have about 3k records!